-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1167.cpp
63 lines (54 loc) · 958 Bytes
/
1167.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
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int n;
long long m;
int root;
vector<pair<int, int>> v[100001];
bool b[100001];
long long dfs(int x) {
b[x] = true;
long long l = 0, r = 0, t;
long long p_m = 0;
for (auto it = v[x].begin(); it != v[x].end(); it++) {
if (b[(*it).first])
continue;
if (l == 0)
l = dfs((*it).first) + (*it).second;
else if (r == 0) {
r = dfs((*it).first) + (*it).second;
if (l > r)
swap(l, r);
}
else {
t = dfs((*it).first) + (*it).second;
if (t > r) {
l = r;
r = t;
}
else if (t > l)
l = t;
}
}
m = max(m, l + r);
return max(l, r);
}
int main() {
scanf("%d", &n);
for (int i = 0, x, y, z; i < n; i++) {
scanf("%d", &x);
while (1) {
scanf("%d", &y);
if (y == -1)
break;
scanf("%d", &z);
v[x].push_back({ y,z });
if (root == 0 || y == root)
root = x;
}
}
dfs(root);
printf("%lld", m);
return 0;
}