Akatsuki13

Centroid Decomp Zu vai

Oct 28th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define PB push_back
  3. #define MP make_pair
  4. #define F first
  5. #define S second
  6. #define FRI freopen("in.txt","r",stdin)
  7. #define FRO freopen("out.txt","w",stdout)
  8. #define RAD(x) ((x*PI)/180)
  9. #define NEX(x) ((x)==n-1?0:(x)+1)
  10. #define PRE(x) ((x)==0?n-1:(x)-1)
  11. #define DEG(x) ((x*180)/PI)
  12. #define EPS 1e-12
  13. #define INF 1e18
  14. #define MOD 1000000007
  15. #define MAXN 100005
  16. using namespace std;
  17.  
  18. typedef long long LL;
  19. typedef unsigned long long ULL;
  20. typedef long double LD;
  21.  
  22. vector<int>adj[MAXN];
  23. bool vis[MAXN],alive[MAXN];
  24. int sub[MAXN],bestChild[MAXN],rnk[MAXN];
  25.  
  26. void DFS(int s) {
  27.     int i,c,maxi=0;
  28.     vis[s]=true;
  29.     sub[s]=1;
  30.     bestChild[s]=-1;
  31.     for(i=0;i<adj[s].size();i++) {
  32.         c=adj[s][i];
  33.         if(vis[c]||!alive[c]) continue;
  34.         DFS(c);
  35.         if(maxi<sub[c]) {
  36.             maxi=sub[c];
  37.             bestChild[s]=c;
  38.         }
  39.         sub[s]+=sub[c];
  40.     }
  41. }
  42.  
  43. int main() {
  44.     int n,i,a,b,s,tS,cur;
  45.     bool more;
  46.     scanf("%d",&n);
  47.     for(i=0;i<n-1;i++) {
  48.         scanf("%d %d",&a,&b);
  49.         a--,b--;
  50.         adj[a].PB(b);
  51.         adj[b].PB(a);
  52.     }
  53.     for(i=0;i<n;i++) alive[i]=true;
  54.     cur=0;
  55.     more=true;
  56.     while(more) {
  57.         more=false;
  58.         for(i=0;i<n;i++) vis[i]=false;
  59.         for(i=0;i<n;i++)
  60.             if(alive[i]&&!vis[i]) {
  61.                 more=true;
  62.                 s=i;
  63.                 DFS(s);
  64.                 tS=sub[s];
  65.                 while(bestChild[s]!=-1&&sub[bestChild[s]]>=tS/2) s=bestChild[s];
  66.                 alive[s]=false;
  67.                 rnk[s]=cur;
  68.             }
  69.         cur++;
  70.     }
  71.     for(i=0;i<n;i++) printf("%c ",rnk[i]+'A');
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment