-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1967.cpp
More file actions
72 lines (45 loc) · 934 Bytes
/
1967.cpp
File metadata and controls
72 lines (45 loc) · 934 Bytes
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
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<int, int>P;
vector<P> adj[10001];
int maxLeng = 0;
int dfs(int root) {
//터미널 노드일 경우
if (adj[root].empty()) {
return 0;
}
int max = 0, max2 = 0;
for (int i = 0; i < adj[root].size(); i++) {
int temp = dfs(adj[root][i].first) + adj[root][i].second;
if (max < temp) {
if (max2 < max) {
max2 = max;
}
max = temp;
}
else if (max2 < temp) {
max2 = temp;
}
}
if (maxLeng < max + max2) {
maxLeng = max + max2;
}
return max;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n-1; i++) {
int pa, ch, val;
cin >> pa >> ch >> val;
//adj[부모] = P(자식,가중치)
adj[pa].push_back(P(ch, val));
}
int max = dfs(1);
cout << maxLeng;
}