-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiameter_dfs.cpp
More file actions
74 lines (73 loc) · 1.28 KB
/
diameter_dfs.cpp
File metadata and controls
74 lines (73 loc) · 1.28 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
#include "bits/stdc++.h"
using namespace std;
const int N = 2e5 + 10;
vector<int> t[N];
int in[N], out[N], n;
// finds the farthest node from u
void dfs1(int u = 1, int par = 0)
{
in[u] = 0;
for (auto v : t[u])
{
if (v == par)
{
continue;
}
dfs1(v, u);
in[u] = max(in[u], in[v] + 1);
}
}
void dfs2(int u = 1, int par = -1)
{
int mx1 = -1, mx2 = -1;
for (auto v : t[u])
{
if (v == par)
continue;
if (in[v] >= mx1)
{
mx2 = mx1;
mx1 = in[v];
}
else if (in[v] > mx2)
{
mx2 = in[v];
}
}
for (auto v : t[u])
{
if (v == par)
continue;
int longs = mx1;
if (mx1 == in[v])
{
longs = mx2;
}
out[v] = 1 + max(out[u], 1 + longs);
dfs2(v, u);
}
}
void diameter()
{
dfs1();
dfs2();
int ans = 0;
for (int i = 1; i <= n; ++i)
{
ans = max({ans, in[i], out[i]});
}
cout << "The diameter is " << ans << "\n";
}
int main()
{
cin >> n;
for (int i = 0; i < n - 1; ++i)
{
int x, y;
cin >> x >> y;
t[x].push_back(y);
t[y].push_back(x);
}
diameter();
return 0;
}