-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01967_DiameterOfTree_DFS.cpp
More file actions
52 lines (45 loc) · 1016 Bytes
/
01967_DiameterOfTree_DFS.cpp
File metadata and controls
52 lines (45 loc) · 1016 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
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 100000;
int n, ans_node, ans_dist;
bool visited[MAX + 1];
vector<pair<int, int>> tree[MAX + 1];
void dfs(int cur, int dist)
{
visited[cur] = 1;
for (int i = 0; i < tree[cur].size(); i++)
{
int next = tree[cur][i].first;
int ndist = tree[cur][i].second;
if (!visited[next])
{
if (ans_dist < dist + ndist)
{
ans_dist = dist + ndist;
ans_node = next;
}
dfs(next, dist + ndist);
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
int temp = n - 1;
while (temp--)
{
int n1, n2, d;
cin >> n1 >> n2 >> d;
tree[n1].push_back(make_pair(n2, d));
tree[n2].push_back(make_pair(n1, d));
}
dfs(1, 0);
for (int i = 1; i <= n; i++) visited[i] = 0;
dfs(ans_node, 0);
cout << ans_dist;
return 0;
}