Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define f first
- #define s second
- #define ll long long
- #define pb push_back
- #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
- using namespace std;
- const int MAXN = 1e5 + 10;
- const int MOD = 1e9 + 7;
- vector <int> g[MAXN];
- char ans[MAXN];
- int cnt[MAXN];
- bool removed[MAXN];
- int get_cnt(int x, int pr = -1){
- cnt[x] = 1;
- for (int to : g[x]){
- if (!removed[to] && to != pr){
- cnt[x] += get_cnt(to, x);
- }
- }
- return cnt[x];
- }
- int get_centriod(int x, int cnt_v, int pr = -1){
- for (int to : g[x]){
- if (!removed[to] && to != pr){
- if (2 * cnt[to] > cnt_v){
- return get_centriod(to, cnt_v, x);
- }
- }
- }
- return x;
- }
- void run(int x, int y){
- int cnt_v = get_cnt(x, -1);
- int centroid = get_centriod(x, cnt_v, -1);
- ans[centroid] = y + 'A' - 1;
- removed[centroid] = 1;
- for (int to : g[centroid]){
- if (!removed[to]){
- run(to, y + 1);
- }
- }
- }
- int main(){
- IOS;
- int n;
- cin >> n;
- for (int i = 1; i < n; i++){
- int x, y;
- cin >> x >> y;
- g[x].pb(y);
- g[y].pb(x);
- }
- run(1, 1);
- for (int i = 1; i <= n; i++){
- cout << ans[i] << ' ';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment