#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=201000; int n,go[N],f[N],alive[N],alive2[N],sz[N]; VI e[N],newv[N],rev[N],son[N]; map<int,int> outcomp[N]; VI got[N]; int find(int x) { return f[x]==x?x:f[x]=find(f[x]); } bool canadd(int u) { return alive2[u]&&SZ(outcomp[u])==1; } struct node { node *s[2],*f,*minv; int sz,d; bool isr() { return !f||(f->s[0]!=this && f->s[1]!=this);} bool dir() { return f->s[1]==this;} void setc(node *c,int d) { s[d]=c;if (c) c->f=this;} void upd() { sz=d; rep(i,0,2) if (s[i]) sz+=s[i]->sz; } }pool[N],*cur; void rot(node *x) { node *p=x->f;bool d=x->dir(); if (!p->isr()) p->f->setc(x,p->dir()); else x->f=p->f; p->setc(x->s[!d],d);x->setc(p,!d); p->upd(); } void splay(node *x) { while (!x->isr()) { if (x->f->isr()) rot(x); else if (x->dir()==x->f->dir()) rot(x->f),rot(x); else rot(x),rot(x); } x->upd(); } node *expose(node *x) { node *q=NULL; for (;x;x=x->f) splay(x),x->s[1]=q,(q=x)->upd(); return q; } ll ans=0; int findlca(int u) { node *p=pool+e[u][0]; expose(p); for (auto v:e[u]) p=expose(pool+v),expose(p); return p-pool; } int finddep(int u) { node *p=pool+u; expose(p); splay(p); return p->d+(p->s[0]?p->s[0]->sz:0); } ll solve(int k) { function<void(int)> add=[&](int u) { if (go[u]!=-1) return; int lca=findlca(u); go[u]=lca; if (find(u)!=find(lca)) { son[lca].pb(u); node *p=pool+u; expose(p); splay(p); assert(p->s[0]==NULL); p->f=pool+lca; ans+=(ll)sz[find(u)]*finddep(lca); int pu=find(u),pv=find(lca); if (SZ(got[pu])>SZ(got[pv])) { swap(pu,pv); } f[pu]=pv; sz[pv]+=sz[pu]; VI nc; for (auto x:got[pu]) { assert(outcomp[x][pu]>0); outcomp[x][pu]--; if (outcomp[x][pu]==0) outcomp[x].erase(pu); outcomp[x][pv]++; got[pv].pb(x); } for (auto x:got[pu]) if (canadd(x)) nc.pb(x); got[pu].clear(); for (auto x:nc) add(x); } else { VI cyc,vec; int w=lca; function<void(int)> dfs=[&](int x) { vec.pb(x); for (auto y:son[x]) dfs(y); }; dfs(u); for (auto x:vec) ans-=finddep(x); while (1) { cyc.pb(w); w=go[w]; if (w==lca) break; } for (auto p:cyc) { node *q=pool+p; expose(q); splay(q); if (p==u) q->d=SZ(cyc); else q->d=0; q->upd(); } for (auto x:vec) ans+=finddep(x); } }; alive[k]=1; for (auto u:rev[k]) { outcomp[u][find(k)]++,got[find(k)].pb(u); } for (auto u:newv[k]) { alive2[u]=1; if (canadd(u)) add(u); } return ans; } int main() { scanf("%d",&n); rep(i,1,n+1) { int x; scanf("%d",&x); rep(j,0,x) { int u; scanf("%d",&u); e[i].pb(u); rev[u].pb(i); } newv[max(i,*max_element(all(e[i])))].pb(i); f[i]=i; go[i]=-1; sz[i]=1; pool[i].d=pool[i].sz=1; } rep(k,1,n+1) { printf("%lld%c",solve(k)," \n"[k==n]); } }
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=201000; int n,go[N],f[N],alive[N],alive2[N],sz[N]; VI e[N],newv[N],rev[N],son[N]; map<int,int> outcomp[N]; VI got[N]; int find(int x) { return f[x]==x?x:f[x]=find(f[x]); } bool canadd(int u) { return alive2[u]&&SZ(outcomp[u])==1; } struct node { node *s[2],*f,*minv; int sz,d; bool isr() { return !f||(f->s[0]!=this && f->s[1]!=this);} bool dir() { return f->s[1]==this;} void setc(node *c,int d) { s[d]=c;if (c) c->f=this;} void upd() { sz=d; rep(i,0,2) if (s[i]) sz+=s[i]->sz; } }pool[N],*cur; void rot(node *x) { node *p=x->f;bool d=x->dir(); if (!p->isr()) p->f->setc(x,p->dir()); else x->f=p->f; p->setc(x->s[!d],d);x->setc(p,!d); p->upd(); } void splay(node *x) { while (!x->isr()) { if (x->f->isr()) rot(x); else if (x->dir()==x->f->dir()) rot(x->f),rot(x); else rot(x),rot(x); } x->upd(); } node *expose(node *x) { node *q=NULL; for (;x;x=x->f) splay(x),x->s[1]=q,(q=x)->upd(); return q; } ll ans=0; int findlca(int u) { node *p=pool+e[u][0]; expose(p); for (auto v:e[u]) p=expose(pool+v),expose(p); return p-pool; } int finddep(int u) { node *p=pool+u; expose(p); splay(p); return p->d+(p->s[0]?p->s[0]->sz:0); } ll solve(int k) { function<void(int)> add=[&](int u) { if (go[u]!=-1) return; int lca=findlca(u); go[u]=lca; if (find(u)!=find(lca)) { son[lca].pb(u); node *p=pool+u; expose(p); splay(p); assert(p->s[0]==NULL); p->f=pool+lca; ans+=(ll)sz[find(u)]*finddep(lca); int pu=find(u),pv=find(lca); if (SZ(got[pu])>SZ(got[pv])) { swap(pu,pv); } f[pu]=pv; sz[pv]+=sz[pu]; VI nc; for (auto x:got[pu]) { assert(outcomp[x][pu]>0); outcomp[x][pu]--; if (outcomp[x][pu]==0) outcomp[x].erase(pu); outcomp[x][pv]++; got[pv].pb(x); } for (auto x:got[pu]) if (canadd(x)) nc.pb(x); got[pu].clear(); for (auto x:nc) add(x); } else { VI cyc,vec; int w=lca; function<void(int)> dfs=[&](int x) { vec.pb(x); for (auto y:son[x]) dfs(y); }; dfs(u); for (auto x:vec) ans-=finddep(x); while (1) { cyc.pb(w); w=go[w]; if (w==lca) break; } for (auto p:cyc) { node *q=pool+p; expose(q); splay(q); if (p==u) q->d=SZ(cyc); else q->d=0; q->upd(); } for (auto x:vec) ans+=finddep(x); } }; alive[k]=1; for (auto u:rev[k]) { outcomp[u][find(k)]++,got[find(k)].pb(u); } for (auto u:newv[k]) { alive2[u]=1; if (canadd(u)) add(u); } return ans; } int main() { scanf("%d",&n); rep(i,1,n+1) { int x; scanf("%d",&x); rep(j,0,x) { int u; scanf("%d",&u); e[i].pb(u); rev[u].pb(i); } newv[max(i,*max_element(all(e[i])))].pb(i); f[i]=i; go[i]=-1; sz[i]=1; pool[i].d=pool[i].sz=1; } rep(k,1,n+1) { printf("%lld%c",solve(k)," \n"[k==n]); } } |