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
| #include<bits/stdc++.h> using namespace std; #ifdef LOCAL #define debug(...) fprintf(stdout, ##__VA_ARGS__) #else #define debug(...) void(0) #endif #define int long long inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'|| ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;} #define Z(x) (x)*(x) #define pb push_back #define fi first #define se second
#define N 400010
int n, m, i, j, k, T; int ans, f[N], g[N], h[N]; int zu[N], F[N], firstF[N]; int u, v, x; vector<int>G[N];
void cun(int x, int y) { G[x].pb(y); G[y].pb(x); }
int calc(int x) { if(!x) return 0; int ans=0; if(x>n) { ans+=f[x]*(f[x]-1)*(f[x]+1); ans-=f[x]*f[x]; ans+=2*f[x]*h[x]; } else { ans=g[x]*g[x]; } return ans; }
int fa(int x) { if(zu[x]==x) return x; return zu[x]=fa(zu[x]); }
void dfs(int x, int fa) { zu[x]=x; F[x]=firstF[x]=fa; for(int y : G[x]) if(y!=fa) { debug("%lld -> %lld\n", x, y); dfs(y, x); f[x]++; g[x]+=f[y]; h[x]+=g[y]; } debug("%lld : %lld %lld %lld\n", x, f[x], g[x], h[x]); ans+=calc(x); }
signed main() { freopen("last.in", "r", stdin); freopen("last.out", "w", stdout); #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif
n=read(); for(i=1; i<n; ++i) { u=read(); v=read(); cun(u, i+n); cun(v, i+n); } dfs(n, 0); for(x=1; x<n; ++x) { printf("%lld\n", ans); i=fa(firstF[x]); j=fa(F[i]); k=fa(F[j]); debug("del %lld : %lld %lld %lld\n", x, i, j, k) ; debug("# %lld : %lld %lld %lld\n", i, f[i], g[i], h[i]); debug("# %lld : %lld %lld %lld\n", j, f[j], g[j], h[j]); debug("# %lld : %lld %lld %lld\n", k, f[k], g[k], h[k]); ans-=calc(x); ans-=calc(i); ans-=calc(j); ans-=calc(k); f[k]--; g[k]-=f[j]; h[k]-=g[j]; f[j]--; g[j]-=f[i]; h[j]-=h[i]; f[i]--; g[i]-=f[x]; h[i]-=g[x]; for(int y : G[x]) if(y!=firstF[x]) { debug("In !\n"); ans-=calc(y); zu[y]=i; f[i]+=f[y]; g[i]+=g[y]; h[i]+=h[y]; } ans+=calc(i); f[j]++; g[j]+=f[i]; h[j]+=h[i]; ans+=calc(j); f[k]++; g[k]+=f[j]; h[k]+=g[j]; ans+=calc(k); debug("# %lld : %lld %lld %lld\n", i, f[i], g[i], h[i]); debug("# %lld : %lld %lld %lld\n", j, f[j], g[j], h[j]); debug("# %lld : %lld %lld %lld\n", k, f[k], g[k], h[k]); } printf("0\n"); return 0; }
|