-
Notifications
You must be signed in to change notification settings - Fork 2
/
1967_josueyeon.cpp
64 lines (49 loc) · 1.12 KB
/
1967_josueyeon.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
57
58
59
60
61
62
// DFS
// 1167 참조하기
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 1967 트리의 지름
int result;
int pos;
void DFS(vector<pair<int, int>> graph[], bool visited[], int node, int weight)
{
visited[node] = true;
for(int i = 0;i < graph[node].size();i++)
{
int next = graph[node][i].first;
int w = graph[node][i].second;
if (visited[next] == false)
DFS(graph, visited, next, weight + w);
}
if (result < weight)
{
result = weight;
pos = node;
}
}
int main()
{
// n(node)
int n;
cin>>n;
vector<pair<int, int>> graph[10001];
bool visited[10001];
for(int i = 0;i < n - 1;i++)
{
int a, b, c;
cin>>a>>b>>c;
// a-b를 잇는 가중치 c
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
result = 0;
fill_n(visited, n + 1, false);
DFS(graph, visited, 1, 0);
result = 0;
fill_n(visited, n + 1, false);
DFS(graph, visited, pos, 0);
cout<<result<<"\n";
return 0;
}