Guest User

a.cpp

a guest
Mar 12th, 2026
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f first
  3. #define s second
  4. #define ll long long
  5. #define pb push_back
  6. #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  7.  
  8. using namespace std;
  9. const int MAXN = 1e5 + 10;
  10. const int MOD = 1e9 + 7;
  11. vector <int> g[MAXN];
  12. char ans[MAXN];
  13. int cnt[MAXN];
  14. bool removed[MAXN];
  15. int get_cnt(int x, int pr = -1){
  16.     cnt[x] = 1;
  17.     for (int to : g[x]){
  18.         if (!removed[to] && to != pr){
  19.             cnt[x] += get_cnt(to, x);
  20.         }
  21.     }
  22.     return cnt[x];
  23. }
  24. int get_centriod(int x, int cnt_v, int pr = -1){
  25.  
  26.     for (int to : g[x]){
  27.         if (!removed[to] && to != pr){
  28.             if (2 * cnt[to] > cnt_v){
  29.                 return get_centriod(to, cnt_v, x);
  30.             }
  31.         }
  32.     }
  33.     return x;
  34. }
  35.  
  36. void run(int x, int y){
  37.     int cnt_v = get_cnt(x, -1);
  38.     int centroid = get_centriod(x, cnt_v, -1);
  39.  
  40.     ans[centroid] = y + 'A' - 1;
  41.     removed[centroid] = 1;
  42.     for (int to : g[centroid]){
  43.         if (!removed[to]){
  44.             run(to, y + 1);
  45.         }
  46.     }
  47. }
  48.  
  49. int main(){
  50.     IOS;
  51.     int n;
  52.     cin >> n;
  53.  
  54.     for (int i = 1; i < n; i++){
  55.         int x, y;
  56.         cin >> x >> y;
  57.         g[x].pb(y);
  58.         g[y].pb(x);
  59.     }
  60.  
  61.     run(1, 1);
  62.  
  63.     for (int i = 1; i <= n; i++){
  64.         cout << ans[i] << ' ';
  65.     }
  66.  
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment