-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1132.cpp
More file actions
78 lines (70 loc) · 1.52 KB
/
cses_1132.cpp
File metadata and controls
78 lines (70 loc) · 1.52 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
75
76
77
78
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 200087;
int dis[MAXN][2];
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;
cin >> n;
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();
}
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < 2; j++)
dis[i][j] = -1;
q.emplace(farest);
dis[farest][0] = 0;
while (!q.empty())
{
for (int x : tree[q.front()])
{
if (dis[x][0] != -1)
continue;
q.emplace(x);
dis[x][0] = dis[q.front()][0] + 1;
farest = x;
}
q.pop();
}
q.emplace(farest);
dis[farest][1] = 0;
while (!q.empty())
{
for (int x : tree[q.front()])
{
if (dis[x][1] != -1)
continue;
q.emplace(x);
dis[x][1] = dis[q.front()][1] + 1;
}
q.pop();
}
for (int i = 1; i <= n; i++)
cout << max(dis[i][0], dis[i][1]) << " ";
cout << "\n";
return 0;
}