-
Notifications
You must be signed in to change notification settings - Fork 0
/
1094 - Farthest Nodes in a Tree.cpp
75 lines (73 loc) · 1.55 KB
/
1094 - Farthest Nodes in a Tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<stdio.h>
#include<iostream>
#include<queue>
#define inf 1000000000
using namespace std;
vector<int>graph[30000];
int n,visited[30000];
int dis[30000];
vector<int>cost[30000];
void bfs(int s)
{
for(int i=0; i<n; i++)
{
visited[i]=0;
dis[i]=inf;
}
queue<int>q;
q.push(s);
visited[s]=1;
dis[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=0; i<graph[u].size(); i++)
{
int v=graph[u][i];
if(visited[v]==0)
{
visited[v]=1;
dis[v]=dis[u]+cost[u][i];
q.push(v);
}
}
}
}
int main()
{
int t,test=1,i,u,v,w;
cin>>t;
while(t--)
{
scanf("%d",&n);
for(i=0; i<n-1; i++)
{
scanf("%d%d%d",&u,&v,&w);
graph[u].push_back(v);
graph[v].push_back(u);
cost[u].push_back(w);
cost[v].push_back(w);
}
bfs(0);
int maxdis=0,start;
for(i=0; i<n; i++)
if(dis[i]>maxdis)
start=i,maxdis=dis[i];
bfs(start);
maxdis=0;
for(i=0; i<n; i++)
if(dis[i]>maxdis)
{
maxdis=dis[i];
maxdis=dis[i];
}
printf("Case %d: %d\n",test,maxdis);
for(int i=0;i<n;i++)
{
graph[i].clear(); cost[i].clear();
}
test++;
}
return 0;
}