-
Notifications
You must be signed in to change notification settings - Fork 0
/
1167.cpp
56 lines (51 loc) · 1.26 KB
/
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
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int maxNode;
int maxDist;
void dfs(vector<vector<pair<int,int>>> &v, vector<bool> &visited, int node, int dist){
visited[node] = true;
if(dist > maxDist){
maxNode = node;
maxDist = dist;
}
// cout << "Node: " << node << endl;
// cout << "dist: " << dist << endl;
for(auto there: v[node]){
if(visited[there.second]){
continue;
}
else{
dfs(v, visited, there.second, dist + there.first);
}
}
}
int main(void){
int n;
cin >> n;
vector<vector<pair<int, int>>> v(n+1);
int input_int1, input_int2;
for(int i=1; i<=n; i++){
int vertex;
cin >> vertex;
while(true){
cin >> input_int1;
if(input_int1 == -1){
break;
}
cin >> input_int2;
v[vertex].push_back(make_pair(input_int2, input_int1));//°Å¸®, Á¤Á¡À¸·Î ÀúÀå.
}
}
maxNode = -1;
maxDist = 0;
vector<bool> visited(n+1, false);
dfs(v, visited, 1, 0);
fill(visited.begin(), visited.end(), false);
maxDist = 0;
dfs(v, visited, maxNode, 0);
cout << maxDist;
return 0;
}