-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1167.cpp
48 lines (43 loc) · 961 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
#include <bits/stdc++.h>
using namespace std;
int v, maxDist, maxNode, a, b, c;
vector<pair<int,int>> vec[100010];
bool visited[100010];
void dfs(int k, int dist){
visited[k] = true;
if(maxDist < dist){
maxDist = dist;
maxNode = k;
}
for(int i = 0; i < vec[k].size(); i++){
int nex = vec[k][i].first;
int nexv = vec[k][i].second;
if(!visited[nex]) dfs(nex, nexv+dist);
}
}
int main (){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> v;
for(int i = 0; i < v; i++){
cin >> a;
while(1){
cin >> b;
if(b == -1){
break;
}
cin >> c;
vec[a].push_back({b,c});
vec[b].push_back({a,c});
}
}
dfs(1,0);
for(int i = 0; i <= v; i++){
visited[i] = 0;
}
maxDist = 0;
dfs(maxNode, 0);
cout << maxDist << '\n';
return 0;
}