-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1167.cpp
More file actions
73 lines (48 loc) · 988 Bytes
/
1167.cpp
File metadata and controls
73 lines (48 loc) · 988 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
73
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef pair<int, int>P;
vector<P> adj[100001];
int dist[100001];
int maxLeng = 0;
int maxIdx;
void dfs(int root) {
for (P next : adj[root]) {
int node = next.first;
int val = next.second;
//¹æ¹®ÇÑÀûÀÖÀ¸¸é ½ºÅµ
if (dist[node] != -1)continue;
dist[node] = dist[root] + val;
if (dist[node] > maxLeng) {
maxLeng = dist[node];
maxIdx = node;
}
dfs(node);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int V;
cin >> V;
for (int i = 0; i < V; i++) {
int pa, ch = 0, val;
cin >> pa;
while (true) {
cin >> ch;
if (ch == -1)break;
cin >> val;
adj[pa].push_back(P(ch, val));
}
}
fill(&dist[0], &dist[100001], -1);
dist[1] = 0;
dfs(1);
fill(&dist[0], &dist[100001], -1);
dist[maxIdx] = 0;
dfs(maxIdx);
cout << maxLeng;
}