Farthest Nodes in a Tree (求树的直径)

题目链接,密码:hpu

Description

Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the tree whose distance is maximum amongst all nodes.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with an integer n (2 ≤ n ≤ 30000) denoting the total number of nodes in the tree. The nodes are numbered from 0 to n-1. Each of the next n-1 lines will contain three integers u v w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 10000) denoting that node u and v are connected by an edge whose weight is w. You can assume that the input will form a valid tree.

Output

For each case, print the case number and the maximum distance.

Sample Input

2

4

0 1 20

1 2 30

2 3 50

5

0 2 20

2 1 10

0 3 29

0 4 50

Sample Output

Case 1: 100

Case 2: 80

 1 #include<cstdio>
 2 #include<string.h>
 3 #include<algorithm>
 4 #define M 30010
 5 #include<queue>
 6 using namespace std;
 7 int a,b,c,head[M],ans,flag[M],sum[M],node,num,i,n;
 8 /*  head表示每个节点的头“指针”
 9     num表示总边数
10     ans记录最后的结果
11     flag[]标记访问过的节点
12     sum[]表示以该节点结尾的最长路
13     */
14
15 struct stu
16 {
17     int from,to,val,next;
18 }st[M*2];
19 void add_edge(int u,int v,int w)
20 {
21     st[num].from=u;
22     st[num].to=v;
23     st[num].val=w;
24     st[num].next=head[u];
25     head[u]=num++;
26 }
27 void bfs(int fir)
28 {
29     int u;
30     queue<int> que;
31     memset(flag,0,sizeof(flag));
32     memset(sum,0,sizeof(sum));
33     flag[fir]=1;
34     que.push(fir);
35     ans=0;
36     while(!que.empty())
37     {
38         u=que.front();
39         que.pop();
40         for(i = head[u] ; i != -1 ; i = st[i].next)
41         {
42             if(!flag[st[i].to] && sum[st[i].to] < sum[u] + st[i].val)
43             {
44                 sum[st[i].to]=sum[u]+st[i].val;
45                 flag[st[i].to]=1;
46                 if(ans < sum[st[i].to])
47                 {
48                     ans=sum[st[i].to];
49                     node=st[i].to;            //记录以fir为起点的最长路的端点
50                 }
51                 que.push(st[i].to);
52             }
53         }
54
55     }
56 }
57 int main()
58 {
59     int k=0;
60     int t;
61     scanf("%d",&t);
62     while(t--)
63     {
64         num=0;
65         memset(head,-1,sizeof(head));
66         scanf("%d",&n);
67         for(i = 1 ; i < n ; i++)
68         {
69             scanf("%d %d %d",&a,&b,&c);
70             add_edge(a,b,c);
71             add_edge(b,a,c);
72         }
73         bfs(1);
74         bfs(node);
75         printf("Case %d: %d\n",++k,ans);
76     }
77 }
时间: 2024-08-26 13:39:47

Farthest Nodes in a Tree (求树的直径)的相关文章

lightoj-1094 Farthest Nodes in a Tree(求树的直径)

1094 - Farthest Nodes in a Tree PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBGiven a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirect

Lightoj 1094 - Farthest Nodes in a Tree 【树的直径 裸题】

1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undire

light oj 1094 Farthest Nodes in a Tree(树的直径模板)

1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undire

lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cstdlib> 5 #include <cstrin

lightoj1094 - Farthest Nodes in a Tree

1094 - Farthest Nodes in a Tree   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undi

Codeforces1294F. Three Paths on a Tree(两次BFS求树的直径)

题意: 给一棵树,找到三个顶点,使三个顶点两两之间路径的并集最大 思路: 必定会有一组最优解,使得 a,b是树直径上的端点. 证明: 假设某个答案取连接点x.x最远的树到达的点是s,根据树的直径算法,s是树的某个直径a的端点.假设x的最远和第二远的点组成的链是b,b就会和a有一段公共部分.我们取a和b相交部分距离s最远的那个点y.那么取这个链上点y的答案一定比x更优 用两次BFS可以求出直径的两个端点,在这个过程中还能顺便求出一个端点到树上每一点的距离.之后再用一次BFS求得另一个端点到树上每一

poj:1985:Cow Marathon(求树的直径)

Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5496   Accepted: 2685 Case Time Limit: 1000MS Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has com

ural 1145 Rope in the Labyrinth 图中 bfs求树的直径

1145. Rope in the Labyrinth Time limit: 0.5 second Memory limit: 64 MB A labyrinth with rectangular form and size m × n is divided into square cells with sides' length 1 by lines that are parallel with the labyrinth's sides. Each cell of the grid is

hdu4612 无向图中任意添加一条边后使桥的数量最少 / 无向图缩点+求树的直径

题意如上,含有重边(重边的话,俩个点就可以构成了边双连通). 先缩点成树,在求数的直径,最远的连起来,剩下边(桥)的自然最少.这里学习了树的直径求法:第一次选任意起点U,进行bfs,到达最远的一个点v(level最深)该点必然是树的直径的一个端点,,再从该点出发,bfs,到最深的一点,该点深度就是直径.(证明:先假设u,是直径上一点,S,T是直径的端点,设v!=t,则有(V,U)+(U,S)>(T,U)+(U,S),矛盾,故t=v:若u不是直径上一点,设u到直径上的一点为x,同理易证. 最后 缩