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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#include<bits/stdc++.h> using namespace std; #define _ =read() #define int long long inline int read(){ int r = 0 , w = 1; char ch = getchar(); while(ch > '9' || ch < '0') {if(ch == '-') w = -1; ch = getchar();} while(ch >='0' && ch <='9') {r = r * 10 + ch -'0'; ch = getchar();} return r * w; } namespace FileIO{ string InFile; string OutFile; void File(string s){ InFile = s + ".in"; OutFile = s + ".out"; freopen(InFile.data() , "r" , stdin); freopen(OutFile.data() , "w" , stdout); } } const int Maxn = 201010; int n; struct Vollerei{ int to; int nxt; }; Vollerei *ed[Maxn << 1]; int ecnt; int head[Maxn]; void add(int x , int y){ ed[++ecnt] = new Vollerei; ed[ecnt]->to = y; ed[ecnt]->nxt = head[x]; head[x] = ecnt; } namespace Trie{ struct node{ int siz; int val; node *l; node *r; }; node tr[2000010]; node *cnt = tr; node *rt[Maxn]; node* Newnode(int x , int dep = 17){ node *cur = cnt++; cur->siz = 1; if(dep == 0) return cur; int num = (x >> dep - 1 & 1); if(num == 0) cur->l = Newnode(x , dep - 1); else cur->r = Newnode(x , dep - 1); return cur; } inline int find(node *x , node *y , int dep){ int s = 0; for( ; dep ; dep--){ int num = !(x->l); if(num == 0) x = x->l; else x = x->r; if(num == 0){ if(y->l) y = y->l; else s |= 1 << dep - 1 , y = y->r; } else{ if(y->r) y = y->r; else s |= 1 << dep - 1 , y = y->l; } } return s; } node* merge(node *x , node *y , int dep = 17){ if(x == nullptr) return y; if(y == nullptr) return x; x->siz += y->siz; if(dep == 0) return x; x->l = merge(x->l , y->l , dep - 1); x->r = merge(x->r , y->r , dep - 1); if(x->l == nullptr) x->val = x->r->val; else if(x->r == nullptr) x->val = x->l->val; else if(x->l->siz == 1) x->val = 1 << dep - 1 | find(x->l , x->r , dep - 1); else if(x->r->siz == 1) x->val = 1 << dep - 1 | find(x->r , x->l , dep - 1); else x->val = max(x->l->val , x->r->val); return x; } }; using namespace Trie; int ans[Maxn]; void dfs(int x , int fa){ for(int i = head[x] ; i ; i = ed[i]->nxt){ int to = ed[i]->to; if(to == fa) continue; dfs(to , x); rt[x] = merge(rt[x] , rt[to]); } if(rt[x]->siz < 2) ans[x] = -1; else ans[x] = rt[x]->val; } signed main(){ n _; for(int i = 1 ; i <= n ; i++){ int x _; rt[i] = Newnode(x); } for(int i = 1 ; i < n ; i++){ int x _ , y _; add(x , y) , add(y , x); } dfs(1 , 0); for(int i = 1 ; i <= n ; i++) cout << ans[i] << "\n"; getchar(); return 0; }
|