forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
centroid_decomposition.cpp
76 lines (62 loc) · 1.75 KB
/
centroid_decomposition.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bits/stdc++.h>
using namespace std;
#define N 100100
int n, x, y;
vector < int > graph[N];
struct CentroidDec {
int root, visited[N], siz[N], layer[N], parent[N];
vector < int > centroidTree[N];
void init() {
for (int i = 0; i < N; i++) {
visited[i] = siz[i] = layer[i] = parent[i] = 0;
centroidTree[i].clear();
}
}
int getCentroid(int u, int p = 0) {
siz[u] = 1;
for (int v : graph[u])
if (v != p && !visited[v]) {
getCentroid(v, u);
siz[u] += siz[v];
}
if (p) return 0;
int par = 0, aux = u, nxt = 0;
while (1) {
for (int v : graph[aux])
if (!visited[v] && v != par && siz[v] > siz[u] / 2)
nxt = v;
if (!nxt) return aux;
else { par = aux; aux = nxt; nxt = 0; }
}
}
void buildTree(int u = 0) {
if (u == 0) {
u = root = getCentroid(1);
visited[u] = 1; layer[u] = 1;
}
for (int v : graph[u])
if (!visited[v]) {
int x = getCentroid(v);
visited[x] = 1; layer[x] = layer[u] + 1; parent[x] = u;
centroidTree[u].push_back(x);
centroidTree[x].push_back(u);
buildTree(x);
}
}
} centroid;
int main() {
scanf("%d", &n);
for (int i = 0; i < n-1; i++) {
scanf("%d %d", &x, &y);
graph[x].push_back(y);
graph[y].push_back(x);
}
centroid.init();
centroid.buildTree();
for (int i = 1; i <= n; i++) {
if (i > 1) printf(" ");
printf("%c", centroid.layer[i] - 1 + 'A');
}
printf("\n");
return 0;
}