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
| #include <bits/stdc++.h> using namespace std; #define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) #define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++) #define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--) #define DEBUG(x) cout<<#x<<"="<<x<<endl; #define all(x) (x).begin(),(x).end() #define cle(x) memset(x,0,sizeof(x)) #define ll long long #define ull unsigned ll #define db double #define lb long db #define pb push_back #define mp make_pair #define fi first #define se second inline int read() { int x=0; char ch=getchar(); bool f=0; for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1; for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48); return f?-x:x; } const int N=70005; const int inf=1e9; int n; int ver[N<<1],ne[N<<1],head[N],tot=1; int d[N],ans[N],f[N]; int siz[N],mx[N],dep[N],rt; bool vis[N]; inline void add(int x,int y) { ver[++tot]=y; ne[tot]=head[x]; head[x]=tot; ver[++tot]=x; ne[tot]=head[y]; head[y]=tot; d[x]++; d[y]++; } void getroot(int u,int pre,int s) { siz[u]=1; mx[u]=0; for(int i=head[u],v;i;i=ne[i]) if((v=ver[i])!=pre&&!vis[ver[i]]) { getroot(v,u,s); siz[u]+=siz[v]; mx[u]=max(mx[u],siz[v]); } mx[u]=max(mx[u],s-siz[u]); if(mx[rt]>mx[u]) rt=u; } int sum[N],st[N],top; void dfs(int u,int pre) { st[++top]=u; for(int i=head[u],v;i;i=ne[i]) if((v=ver[i])!=pre&&!vis[ver[i]]) { dep[v]=dep[u]+1; dfs(v,u); } } inline void calc(int s,int opt) { fo(i,1,top) sum[min(max(0,f[st[i]]-dep[st[i]]),s+1)]+=d[st[i]]; fo(i,1,s) sum[i]+=sum[i-1]; fo(i,1,top) ans[st[i]]+=opt*sum[dep[st[i]]]; fo(i,0,s+1) sum[i]=0; } void divide(int u,int s) { top=0; dep[u]=0; dfs(u,0); calc(s,1); vis[u]=1; for(int i=head[u],v,Siz;i;i=ne[i]) if(!vis[v=ver[i]]) { Siz=siz[v]>siz[u]?s-siz[u]:siz[v]; top=0; dep[v]=1; dfs(v,u); calc(Siz,-1); } for(int i=head[u],v,Siz;i;i=ne[i]) if(!vis[v=ver[i]]) { Siz=siz[v]>siz[u]?s-siz[u]:siz[v]; rt=0; getroot(v,0,Siz); divide(rt,Siz); } } queue<int> q; inline void bfs() { fo(i,1,n) if(d[i]==1) q.push(i),f[i]=0,vis[i]=1; for(int u;!q.empty();) { u=q.front(); q.pop(); for(int i=head[u],v;i;i=ne[i]) if(!vis[v=ver[i]]) { f[v]=f[u]+1; vis[v]=1; q.push(v); } } fo(i,1,n) vis[i]=0; } int main() { n=read(); mx[0]=inf; fo(i,2,n) add(read(),read()); bfs(); fo(i,1,n) d[i]=2-d[i]; rt=0; getroot(1,0,n); divide(rt,n); fo(i,1,n) if(d[i]==1) ans[i]=1; fo(i,1,n) printf("%d\n",ans[i]); return 0; }
|