博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
P4878 道路修建-美国
阅读量:4648 次
发布时间:2019-06-09

本文共 3981 字,大约阅读时间需要 13 分钟。

 

我想我经大神点拨后终于明白了。。。回学校再写吧

时间限制:1s

内存限制:256MB

【问题述】

    A国是一个商业高度发达的国家。它包含了n座城市,每座城商业都很发达。但不幸的是,A国的交通并没有像其商业那么发达,它仅仅保证了任意两座城市之间有路径存在,而且只存在唯一的一条!

   拥有雄厚经济实力的商人们决定集资修建一条路,但在修建方案上各个商人都希望新建成的道路对自己利益最大。最终他们决定造一条路,使得两个城市间所需经过道路的数量的最大值尽可能小。为此他们提出了很多修建方案,但他们并不知道每一方案新建道路后最远城市间的最大值为多少,他们有多种修建方案,你能告诉他们每一方案对应的最远城市间的最大值吗?

【输入】

输入文件名为road.in。

第一行两个数n,m(1<=n、m<=3,000),分别表示城市个数和方案个数

         接下来n-1行,每行两个数x、y,表示有一条道路连接x号城市和y号城市

         m下来m行,每行两个数a、b,表示一个修建方案对应的两个城市

【输出】

输出文件名为road.out。

对于每组数据输出一行,包含一个数,表示新建道路后,最远城市间所需经过的道路数量

 

【输入输出样例】

road.in

road.out

8 2

1 3

2 3

3 4

4 5

5 6

6 7

6 8

3 6

1 8

3

5 更正

 

【数据说明】

对于40%的数据,1<=n,m<=300;

对于另外20%的数据,数据呈一条链

对于100%的数据,1<=n,m<=3000

 

#include
#include
#include
#include
#include
#include
using namespace std;const int N=3009; int n,m;int h[N],nex[N*2],to[N*2],cnt;int f[N];bool vis[N];int dep[N],deep[N];int max_son[N];int maxn,root;void Add(int x,int y){ to[++cnt]=y,nex[cnt]=h[x],h[x]=cnt; return;}void dfs1(int x,int tot){ vis[x]=1; for(int i=h[x];i;i=nex[i]) if(!vis[to[i]]) dfs1(to[i],tot+1); if(tot>maxn) maxn=tot,root=x; return ;}int dfs2(int x,int tot,int last){ vis[x]=0;f[x]=last;dep[x]=tot; int sum=0; for(int i=h[x];i;i=nex[i]) if(vis[to[i]]) sum=max(sum,dfs2(to[i],tot+1,x)); sum=max(sum,tot); maxn=max(maxn,sum); max_son[x]=sum; return sum;}void work(int u,int v){ int t,len,ans=0; if(dep[u]>=dep[v]) t=u;else if(dep[v]>dep[u]) t=v; int minn=min(dep[v],dep[u]); ans=dep[f[t]]; ans=max(ans,(minn+1)); cout<
<

 

#include
#include
#include
#include
#include
#include
using namespace std;const int N=3009; int n,m;int h[N],nex[N*2],to[N*2],cnt;int f[N];bool vis[N];int dep[N],deep[N];int max_son[N];int maxn,root;void Add(int x,int y){ to[++cnt]=y,nex[cnt]=h[x],h[x]=cnt;}void dfs1(int x,int tot){ vis[x]=1; for(int i=h[x];i;i=nex[i]) if(!vis[to[i]]) dfs1(to[i],tot+1); if(tot>maxn) maxn=tot,root=x; return ;}int dfs2(int x,int tot,int last){ vis[x]=0;f[x]=last;dep[x]=tot; int sum=0; for(int i=h[x];i;i=nex[i]) if(vis[to[i]]) sum=max(sum,dfs2(to[i],tot+1,x)); sum=max(sum,tot); maxn=max(maxn,sum); max_son[x]=sum; return sum;}void work(int u,int v){ int t,len,ans=0,last; if(dep[u]>=dep[v]) t=u;else if(dep[v]>dep[u]) t=v; int minn=min(dep[v],dep[u]); /* if(max_son[t]!=maxn) { printf("%d\n",maxn); return; }else { int ans; ans=maxn-(dep[t]-minn)+1; ans=max(ans,minn+1+(dep[t]-minn)/2); printf("%d\n",ans); return ; } */ /* len=minn+1;last=t; ans=max_son[t]-(dep[t]-minn)+1; while(dep[t]>len) { for(int i=h[t];i;i=nex[i]) if((to[i]!=f[t])&&(to[i]!=last)) { ans=max(ans,max_son[to[i]]-(dep[t]-len)); } last=t;t=f[t];len++; } */ ans=dep[f[t]]; printf("%d\n",ans); return ;}int main(){ scanf("%d%d",&n,&m); for(int i=1,x,y;i

 

#include
#include
#include
#include
#include
#include
using namespace std;const int N=3009; int n,m;int h[N],nex[N*2],to[N*2],cnt;int f[N];bool vis[N];int dep[N],deep[N];int max_son[N];int maxn,root;void Add(int x,int y){ to[++cnt]=y,nex[cnt]=h[x],h[x]=cnt; return;}void dfs1(int x,int tot){ vis[x]=1; for(int i=h[x];i;i=nex[i]) if(!vis[to[i]]) dfs1(to[i],tot+1); if(tot>maxn) maxn=tot,root=x; return ;}int dfs2(int x,int tot,int last){ vis[x]=0;f[x]=last;dep[x]=tot; int sum=0; for(int i=h[x];i;i=nex[i]) if(vis[to[i]]) sum=max(sum,dfs2(to[i],tot+1,x)); sum=max(sum,tot); maxn=max(maxn,sum); max_son[x]=sum; return sum;}void work(int u,int v){ int t,len,ans=0; if(dep[u]>=dep[v]) t=u;else if(dep[v]>dep[u]) t=v; int minn=min(dep[v],dep[u]); len=minn+1; ans=minn+1; ans=max(ans,max_son[t]-(dep[t]-minn-1)); while(dep[t]>len) { ans=max(ans,max_son[t]-(dep[t]-len)); len++,t=f[t]; } return ;}int main(){ scanf("%d%d",&n,&m); for(int i=1,x,y;i

 

转载于:https://www.cnblogs.com/CLGYPYJ/p/7624242.html

你可能感兴趣的文章
JavaScript 第十章总结:first class functions
查看>>
微信公众号发送客服消息【文本、图片】
查看>>
iText简介(转)
查看>>
vue搭建后可以改下全局配置
查看>>
【Docker】Segmentation Fault or Critical Error encountered. Dumping core and abort
查看>>
字典树从第i个构造HDU2846
查看>>
SQL优化笔记(二)—CPU优化
查看>>
bzoj 1042 HAOI2008 硬币购物
查看>>
JS 心得总结
查看>>
WINDOWS 下安装boost
查看>>
Log4j(1)--hellloworld
查看>>
java中equals和 == 的区别
查看>>
greenDao 3.0基础
查看>>
CSS自学笔记(15):CSS3多列布局
查看>>
Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作...
查看>>
丹佛机场行李系统Postmortem
查看>>
好吧,如果一定要RESTFUL的DJANGO
查看>>
Java类的执行顺序
查看>>
Why ngx-uploader doesn't like to cooperate with .net core 2.x?
查看>>
iOS-Senior20-Map定位
查看>>