-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboj1967.cpp
48 lines (42 loc) · 989 Bytes
/
boj1967.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 <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using p = pair<int, int>;
const int MAX = 10e+4;
vector<p> adj[MAX+1];
int dist[MAX+1];
bool visited[MAX+1];
inline void Quick_IO(){
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
void dfs(int prev, int curr, int v){
visited[curr] = true;
for(auto x:adj[curr]){
if(!visited[x.first]){
visited[x.first] = false;
dfs(curr, x.first, v+x.second);
visited[x.first] = true;
}
}
dist[curr] = v;
}
int main(){
Quick_IO();
int n;
int a, b, c;
cin>>n;
for (int i = 0; i <n-1; ++i) {
cin>>a>>b>>c;
adj[a].emplace_back(b, c);
adj[b].emplace_back(a, c);
}
dfs(0, 1, 0);
int maxIndex = max_element(dist+1, dist+n+1) - dist;
fill(visited+1, visited+n+1, false);
dfs(0, maxIndex, 0);
cout<<*max_element(dist+1, dist+n+1)<<endl;
return 0;
}