-
Notifications
You must be signed in to change notification settings - Fork 0
/
1967.cpp
54 lines (49 loc) · 1.21 KB
/
1967.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
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n, answer = 0, tmpNode = 0;
bool visited[10001] = {0, };
vector<pair<int, int>>node[10001];
void input();
void dfs(int currentNode, int currentLength);
int main (){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
input();
visited[1] = true;
dfs(1, 0);
for(int i = 0 ; i < 10001; i++){
visited[i] = false;
}
visited[tmpNode] = true;
dfs(tmpNode, 0);
cout << answer;
}
void input(){
cin >> n;
for(int i = 1; i < n; i++){
int tmp1, tmp2, tmp3;
cin >> tmp1 >> tmp2 >> tmp3;
node[tmp1].push_back({tmp2, tmp3});
node[tmp2].push_back({tmp1, tmp3});
}
}
void dfs(int currentNode, int currentLength){
int cnt = 0;
for(int i = 0; i < node[currentNode].size(); i++){
if(!visited[node[currentNode][i].first]){
cnt++;
visited[node[currentNode][i].first] = true;
dfs(node[currentNode][i].first, currentLength + node[currentNode][i].second);
}
}
if(cnt == 0){
if(answer < currentLength){
answer = currentLength;
tmpNode = currentNode;
return;
}
}
}