-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj_1167.cpp
More file actions
63 lines (57 loc) · 1.13 KB
/
boj_1167.cpp
File metadata and controls
63 lines (57 loc) · 1.13 KB
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
//<1167>번 : <트리의 지름>
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> adj[100001];
int p[100001] = {};
int d[100001] = {};
int n;
int dfs(int a) {
int max1 = 0, max2 = 0;
for(pair<int, int> e : adj[a]) {
int b = e.first;
int w = e.second;
if(p[a] == b) continue;
p[b] = a;
int k = dfs(b) + w;
if(k > max1) {
max2 = max1;
max1 = k;
}
else if(k > max2) {
max2 = k;
}
}
if(max1 == 0) {
d[a] = 0;
return 0;
}
else if(max2 == 0) {
d[a] = max1;
return max1;
}
d[a] = max1 + max2;
return max1;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 0; i < n; i++) {
int a, b = 0, w = 0;
cin >> a;
while(w != -1) {
cin >> b;
if(b == -1) break;
cin >> w;
adj[a].push_back({b, w});
}
}
dfs(1);
int m = 0;
for(int i = 1; i <= n; i++) {
m = max(d[i], m);
}
cout << m << '\n';
return 0;
}