-
Notifications
You must be signed in to change notification settings - Fork 34
/
1094-Farthest-Nodes-In-Tree.cpp
83 lines (61 loc) · 1.1 KB
/
1094-Farthest-Nodes-In-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
76
77
78
79
80
81
82
#include <iostream>
#include <stdio.h>
#include <vector>
#include <utility>
#include <limits.h>
#include <string.h>
using namespace std;
vector < vector <pair <int, int> > > graph;
bool vis[30005];
long long maxi;
int start;
int end;
long long explore(int j, long long dis)
{
int x;
int w;
if(dis > maxi) {
maxi = dis;
start = j;
}
for (int i = 0; i < graph[j].size(); i++) {
x = graph[j][i].first;
w = graph[j][i].second;
if(vis[x] == 0) {
vis[x] = 1;
explore(x, dis+w);
}
}
}
int main()
{
int x;
int y;
int w;
int t;
int temps;
int n;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
scanf("%d", &n);
vector < vector <pair <int, int> > > temp(n+2);
swap(temp, graph);
for (int i = 0; i < n - 1; i++) {
cin >> x;
cin >> y;
cin >> w;
graph[x].push_back(make_pair(y, w));
graph[y].push_back(make_pair(x, w));
}
maxi = INT_MIN;
memset(vis, 0, sizeof(vis));
vis[0] = 1;
explore(0, 0);
temps = start;
maxi = INT_MIN;
memset(vis, 0, sizeof(vis));
vis[start] = 1;
explore(start, 0);
printf("Case %d: %lld\n", cs, maxi);
}
}