|
| 1 | +#include <bits/stdc++.h> |
| 2 | + |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +#define N 100100 |
| 6 | + |
| 7 | +int n, x, y; |
| 8 | +vector < int > graph[N]; |
| 9 | + |
| 10 | +struct CentroidDec { |
| 11 | + int root, visited[N], siz[N], layer[N], parent[N]; |
| 12 | + vector < int > centroidTree[N]; |
| 13 | + |
| 14 | + void init() { |
| 15 | + for (int i = 0; i < N; i++) { |
| 16 | + visited[i] = siz[i] = layer[i] = parent[i] = 0; |
| 17 | + centroidTree[i].clear(); |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + int getCentroid(int u, int p = 0) { |
| 22 | + siz[u] = 1; |
| 23 | + for (int v : graph[u]) |
| 24 | + if (v != p && !visited[v]) { |
| 25 | + getCentroid(v, u); |
| 26 | + siz[u] += siz[v]; |
| 27 | + } |
| 28 | + if (p) return 0; |
| 29 | + |
| 30 | + int par = 0, aux = u, nxt = 0; |
| 31 | + while (1) { |
| 32 | + for (int v : graph[aux]) |
| 33 | + if (!visited[v] && v != par && siz[v] > siz[u] / 2) |
| 34 | + nxt = v; |
| 35 | + |
| 36 | + if (!nxt) return aux; |
| 37 | + else { par = aux; aux = nxt; nxt = 0; } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + void buildTree(int u = 0) { |
| 42 | + if (u == 0) { |
| 43 | + u = root = getCentroid(1); |
| 44 | + visited[u] = 1; layer[u] = 1; |
| 45 | + } |
| 46 | + |
| 47 | + for (int v : graph[u]) |
| 48 | + if (!visited[v]) { |
| 49 | + int x = getCentroid(v); |
| 50 | + visited[x] = 1; layer[x] = layer[u] + 1; parent[x] = u; |
| 51 | + centroidTree[u].push_back(x); |
| 52 | + centroidTree[x].push_back(u); |
| 53 | + buildTree(x); |
| 54 | + } |
| 55 | + } |
| 56 | +} centroid; |
| 57 | + |
| 58 | +int main() { |
| 59 | + scanf("%d", &n); |
| 60 | + for (int i = 0; i < n-1; i++) { |
| 61 | + scanf("%d %d", &x, &y); |
| 62 | + graph[x].push_back(y); |
| 63 | + graph[y].push_back(x); |
| 64 | + } |
| 65 | + |
| 66 | + centroid.init(); |
| 67 | + centroid.buildTree(); |
| 68 | + |
| 69 | + for (int i = 1; i <= n; i++) { |
| 70 | + if (i > 1) printf(" "); |
| 71 | + printf("%c", centroid.layer[i] - 1 + 'A'); |
| 72 | + } |
| 73 | + printf("\n"); |
| 74 | + |
| 75 | + return 0; |
| 76 | +} |
0 commit comments