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 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

Output for 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


Case 1: 100

Case 2: 80

Notes

Dataset is huge, use faster i/o methods.



PROBLEM SETTER: JANE ALAM JAN

题意:给定若干两点间的距离,求两点的距离的最大距离。即?树的直径,据说找到离任意一点最远的点index,然后找离index最远的点就是最大距离?

假定0为根节点找离0最远的点就是,最深的点index,然后找离index最远的点就是树上最远的距离

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4
 5 using namespace std;
 6
 7 #define N 30008
 8
 9 struct node
10 {
11     int v, w, next;
12 }e[N*4];
13
14 int n, cnt, maxx;
15 int Index;   // 写成index过不了lightoj=·=||
16 int head[N], dist[N];
17
18 void addedge(int u, int v, int w)
19 {
20     e[cnt].v = v;
21     e[cnt].w = w;
22     e[cnt].next = head[u];
23     head[u] = cnt++;
24 }
25
26 void dfs(int u, int w)
27 {
28     dist[u] = w;
29     if(w > maxx)
30     {
31         maxx = dist[u];
32         Index = u;
33     }
34     for(int i = head[u]; i != -1; i = e[i].next)
35     {
36         if(dist[e[i].v] == -1)
37         {
38             dfs(e[i].v, dist[u]+e[i].w);
39         }
40     }
41 }
42 int main()
43 {
44     int t, u, v, w, k = 1;
45
46     scanf("%d", &t);
47
48     while(t--)
49     {
50         cnt = maxx = 0;
51         memset(head, -1, sizeof(head));
52
53         scanf("%d", &n);
54         n--;
55         while(n--)
56         {
57             scanf("%d%d%d", &u, &v, &w);
58             addedge(u, v, w);
59             addedge(v, u, w);
60         }
61         memset(dist, -1, sizeof(dist));
62         dfs(0, 0);
63         memset(dist, -1, sizeof(dist));
64         dfs(Index, 0);
65         printf("Case %d: %d\n", k++, maxx);
66     }
67     return 0;
68 }

bfs

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5
 6 using namespace std;
 7
 8 #define N 30008
 9
10 struct node
11 {
12     int v, w, next;
13 } e[N*2];
14
15 int n, cnt, maxx;
16 int Index;
17 int head[N], dist[N], vis[N];
18
19 void addedge(int u, int v, int w)
20 {
21     e[cnt].v = v;
22     e[cnt].w = w;
23     e[cnt].next = head[u];
24     head[u] = cnt++;
25 }
26
27 void bfs(int u)
28 {
29     memset(vis, 0, sizeof(vis));
30     queue<int> Q;
31     Q.push(u);
32     vis[u] = 1;
33     dist[u] = 0;
34
35     while(Q.size())
36     {
37         u = Q.front();
38         Q.pop();
39
40         for(int i = head[u]; i != -1; i = e[i].next)
41         {
42             int v = e[i].v;
43             if(!vis[v])
44             {
45                 vis[v] = 1;
46                 dist[v] = dist[u]+e[i].w;
47                 if(dist[v] > maxx)
48                 {
49                     maxx = dist[v];
50                     Index = v;
51
52                 }
53                 Q.push(v);
54             }
55         }
56     }
57 }
58
59 int main()
60 {
61     int t, u, v, w, k = 1;
62     scanf("%d", &t);
63
64     while(t--)
65     {
66         maxx = cnt = 0;
67         memset(head, -1,sizeof(head));
68
69         scanf("%d", &n);
70         n--;
71
72         while(n--)
73         {
74             scanf("%d%d%d", &u, &v, &w);
75             addedge(u, v, w);
76             addedge(v, u, w);
77         }
78         memset(dist, 0, sizeof(dist));
79         bfs(0);
80         bfs(Index);
81         printf("Case %d: %d\n", k++, maxx);
82     }
83     return 0;
84 }
时间: 2024-10-29 23:24:39

lightoj1094 - 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

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

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 a

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

LightOJ1257 Farthest Nodes in a Tree (II)(树的点分治)

题目给一棵树,边带有权值,求每一点到其他点路径上的最大权和. 树上任意两点的路径都可以看成是经过某棵子树根的路径,于是果断树分治. 对于每次分治的子树,计算其所有结点到根的距离:对于每个结点,找到另一个离根最远的且与该结点路径过根的结点,二者的距离和就是这个点在过这棵子树的根能到的最远距离. 现在问题就是怎么比较快地找到这另一个最远距离的点..两点路径过根,说明两点间不存在一点是另一点的祖先..我一开始还想用DFS序+线段树来着..想了想,想出了线性的算法: 记录每个结点属于根的哪个儿子,把当前

Farthest Nodes in a Tree (II) LightOJ - 1257

#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> #include<vector> #define MAX_N 300010 using namespace std; vector<pair<int,int> >G[MAX_N]; int dis1[MAX_N],dis2[MAX_N],vis[MAX_N]; int N; vo

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

Farthest Nodes in a Tree LightOJ - 1094

树上最远点对(树的直径) 树形dp,类似要求出离任意一个点最远的点的方法. 最长路一定是经过树上的某一个节点的.ans[i]表示i点往下走的最长路.每个节点处都统计一下向下的最长和次长链,将两条拼接起来去更新答案. 1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 typedef long long L

Minimum no. of iterations to pass information to all nodes in the tree

Given a very large n-ary tree. Where the root node has some information which it wants to pass to all of its children down to the leaves with the constraint that it can only pass the information to one of its children at a time (take it as one iterat