-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathBoj1167.cpp
More file actions
62 lines (51 loc) · 1.23 KB
/
Boj1167.cpp
File metadata and controls
62 lines (51 loc) · 1.23 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
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
vector<vector<pair<int, int>>> v;
vector<int> result;
vector<bool> visited;
int n;
int farthest_node = 0;
void dfs(int start) {
stack<int> stack;
stack.push(start);
visited[start] = true;
while(!stack.empty()) {
int cur = stack.top();
stack.pop();
for(int i = 0; i < v[cur].size(); i++) {
int next = v[cur][i].first;
int cost = v[cur][i].second;
if(visited[next]) continue;
result[next] = result[cur] + cost;
stack.push(next);
visited[next] = true;
if(result[farthest_node] < result[next]) {
farthest_node = next;
}
}
}
}
int main() {
cin>>n;
v.resize(n+1);
result.assign(n+1, 0);
visited.assign(n+1, false);
for(int i = 0; i < n; i++) {
int a, b, c;
cin>>a;
while(true) {
cin>>b;
if(b == -1) break;
cin>>c;
v[a].push_back({b, c});
}
}
dfs(1);
result.assign(n+1, 0);
visited.assign(n+1, false);
dfs(farthest_node);
cout<<*max_element(result.begin(), result.end());
}