杭电2196Computer(求每个点的最长路)
来源:互联网 发布:python网络数据采集pdf 编辑:程序博客网 时间:2024/05/14 15:06
Description
Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
Input
Output
Sample Input
51 12 13 11 1
Sample Output
32344
求每个点的最长路
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>const int MAX=10000+10;const int MAXN=10000*2+10;using namespace std;struct Edge{int from,to,val,next;}edge[MAXN];int dist[MAX];int vis[MAX];int head[MAX];int top;void init(){top=0;memset(head,-1,sizeof(head));}void addedge(int u,int v,int w){Edge E={u,v,w,head[u]};edge[top]=E;head[u]=top++;}int bfs(int sx){queue<int>q;memset(dist,0,sizeof(dist));memset(vis,0,sizeof(vis));int node=sx;int ans=0;vis[sx]=1;q.push(sx);while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){Edge E=edge[i];if(!vis[E.to]){dist[E.to]=dist[u]+E.val;if(ans<dist[E.to]){ans=dist[E.to]; node=E.to;}vis[E.to]=1;q.push(E.to);}}}return node;}int main(){int n,a,b;int d1[MAX],d2[MAX];int S,T;while(~scanf("%d",&n)){init(); for(int i=2;i<=n;i++) {scanf("%d%d",&a,&b);addedge(i,a,b);addedge(a,i,b); } int S=bfs(1); int T=bfs(S); for(int i=1;i<=n;i++) d1[i]=dist[i]; bfs(T); for(int i=1;i<=n;i++) d2[i]=dist[i]; for(int i=1;i<=n;i++) printf("%d\n",d1[i]>d2[i]?d1[i]:d2[i]);}return 0;}
- 杭电2196Computer(求每个点的最长路)
- 杭电-2196 Computer(每个点的最大路径)
- hdoj 2196 Computer 【树的直径 求每个点的最长路】
- HDU 2196 --Computer 【树的直径 && 每个点的最长路 && 经典】
- HDU 2196 Computer(求树上每个节点到其它点的最远距离)
- HDU 2196 Computer(求树上每个节点到其它点的最远距离 树dp)
- hdu 2196 Computer 树形DP 求每个点的最远点距离 两次dfs的经典树形dp
- HDU 2196 Computer(求树任意点的最远点)
- HDU 2196 - Computer (树上每点最长距离 DP)
- 【杭电】[2196]Computer
- [SGU - 149 Computer Network] 树形DP 求带权树上每个节点的最长路长度
- hdu 2196 computer 求树上的任意最远点对 O(n)
- hdu2196树形dp求任一点的最长路
- HDU 2196 Computer (tree-DP 树的最长路 经典) #by Plato
- (HDU 2196) Computer <树型DP经典题目 & dfs> 求树上每个节点到最远的叶子结点的路径长度
- 杭电 HDU 2196 Computer Time (树形dp)
- hdu 2196 Computer(树上最长路)
- 求一个数列(数列中的每个数不相同)的最长不下降子序列。
- KMP算法中怎么求next数组
- 顺序表应用1:多余元素删除之移位算法
- 工作第三周 : 程序猿的出世与入世
- 冒泡排序与快速排序
- SSH
- 杭电2196Computer(求每个点的最长路)
- 超链接
- HDU 3342 Legal or Not(拓扑排序)
- HDU 1080 加权LCS
- java BufferedWriter
- Java集合--自定义泛型实例
- LeetCode | Reorder List
- 浅谈tomcat配置
- java线程同步与通信