-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1967.cpp
More file actions
39 lines (32 loc) · 755 Bytes
/
1967.cpp
File metadata and controls
39 lines (32 loc) · 755 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#define MAXN 10050
int n,max=-1,far_node,max_weight=0;
vector<pair<int,int>> adj[MAXN];
void DFS(int p, int u, int cur_weight){
if(cur_weight > max_weight) {
max_weight = cur_weight;
far_node = u;
}
for(auto v: adj[u]){
if(v.first == p) continue;
DFS(u,v.first,cur_weight+v.second);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n;
for(int i=0 ; i<n-1 ; i++){
int p,c,w; cin >> p >> c >> w;
adj[p].push_back({c,w});
adj[c].push_back({p,w});
}
DFS(0,1,0);
max_weight = 0;
DFS(0,far_node,0);
cout << max_weight;
}