-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1131.cpp
More file actions
65 lines (59 loc) · 1.18 KB
/
cses_1131.cpp
File metadata and controls
65 lines (59 loc) · 1.18 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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 200087;
int dis[MAXN];
bool visited[MAXN];
vector<int> tree[MAXN];
queue<int> q;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, a, b, farest, ans;
cin >> n;
if (n == 1)
{
cout << "0\n";
return 0;
}
for (int i = 1; i < n; i++)
{
cin >> a >> b;
tree[a].emplace_back(b);
tree[b].emplace_back(a);
}
q.emplace(n / 2 + 1);
while (!q.empty())
{
visited[q.front()] = true;
for (int x : tree[q.front()])
{
if (visited[x])
continue;
q.emplace(x);
visited[q.front()] = true;
farest = x;
}
q.pop();
}
q.emplace(farest);
for (int i = 0; i < MAXN; i++)
dis[i] = -1;
dis[farest] = 0;
while (!q.empty())
{
for (int x : tree[q.front()])
{
if (dis[x] != -1)
continue;
q.emplace(x);
dis[x] = dis[q.front()] + 1;
ans = dis[x];
}
q.pop();
}
cout << ans << "\n";
return 0;
}