-
Notifications
You must be signed in to change notification settings - Fork 0
/
1967.cpp
48 lines (40 loc) · 784 Bytes
/
1967.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
#include<cstdio>
#include<vector>
using namespace std;
vector<pair<int, int> > vp[10001];
int check[10001];
int node;
int length;
void Reset() {
for (int i = 0; i < 10001; i++) {
check[i] = 0;
}
}
void dfs(int init, int cost) {
check[init] = 1;
if (length < cost) {
length = cost;
node = init;
}
for (int i = 0; i < vp[init].size(); i++) {
if (!check[vp[init][i].first]) {
dfs(vp[init][i].first, cost + vp[init][i].second);
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int a, b, cost;
scanf("%d %d %d", &a, &b, &cost);
vp[a].push_back(make_pair(b, cost));
vp[b].push_back(make_pair(a, cost));
}
dfs(1, 0); // find node
length = 0;
Reset();
dfs(node, 0); // find diameter
printf("%d\n", length);
return 0;
}