#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <string> #include <bitset> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> #include <iomanip> #include <assert.h> using namespace std; #define pb push_back #define mp make_pair typedef pair<int,int> pii; typedef long long ll; typedef double ld; typedef vector<int> vi; #define fi first #define se second #define fe first #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);} #define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);} #define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);} #define es(x,e) (int e=fst[x];e;e=nxt[e]) #define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e]) #define SZ 666666 int n,eg[SZ]; set<int> so[SZ]; set<int> ds[SZ]; bool good[SZ]; int ff[SZ],sz[SZ]; vector<int> dp[SZ]; int ss[SZ],rs[SZ],cs[SZ],rdep[SZ]; int sn; int gf(int x) { // return ff[x]?ff[x]=gf(ff[x]):x; if(!ff[x]) return x; int u=gf(ff[x]); if(ff[x]!=u) cs[x]+=cs[ff[x]]+rs[ff[x]], rdep[x]+=rdep[ff[x]]+1; ff[x]=u; return u; } int gd(int x) { int g=gf(x); if(x==g) return rs[x]; return cs[x]+rs[x]+rs[g]; } #define U 20 int fa[SZ],up[200055][U]; int grdep(int x) { // int w=-1; // for(int f=x;f;f=fa[f]) ++w; // return w; int g=gf(x); if(x==g) return 0; return rdep[x]+1; } int gup(int x,int d) { if((1<<d)>grdep(x)) return 0; if(up[x][d]) return up[x][d]; if(d==0) { assert(fa[x]); return up[x][d]=fa[x]; } return up[x][d]=gup(gup(x,d-1),d-1); } int lca(int x,int y) { int dx=grdep(x),dy=grdep(y); if(dx>dy) swap(x,y),swap(dx,dy); dy-=dx; for(int i=0;i<U;++i) if(dy&(1<<i)) y=gup(y,i); if(x==y) return x; for(int i=U-1;i>=0;--i) if(gup(x,i)!=gup(y,i)) x=up[x][i],y=up[y][i]; return gup(x,0); // set<int> f; // for(int u=x;u;u=fa[u]) f.insert(u); // for(int u=y;;u=fa[u]) { // if(f.count(u)) return u; // } // return -1; //unreachable } void cons(int x) { // cerr<<" cons "<<x<<"\n"; if(eg[x]||!good[x]) return; set<int> pf; bool g=1; for(auto w:so[x]) { g&=good[w],pf.insert(gf(w)); if(!g||pf.size()>1) break; } if(g&&pf.size()==1) { int t=0; for(auto w:so[x]) { if(!t) t=w; else t=lca(t,w); } // cerr<<" gj "<<x<<"->"<<t<<"\n"; eg[x]=t; ss[++sn]=x; } } ll ans=0; vector<int> son[SZ]; ll ts[SZ]; vector<int> td; void dfs(int x) { ts[x]=0; td.pb(x); for(auto s:son[x]) { dfs(s); ts[x]+=ts[s]; } ans-=ts[x]++; } int ST=0,mk[SZ],bl[SZ]; void deal_queue() { while(sn) { int x=ss[sn--],f=eg[x]; if(bl[f]) f=bl[f]; assert(!bl[x]); // cerr<<x<<"->"<<f<<"?\n"; // for(auto D:dp[x]) cout<<D<<",";cout<<"|"; // for(auto D:dp[f]) cout<<D<<",";cout<<" \n"; int g=gf(f); if(gf(x)==gf(f)) { assert(g==x); td.clear(); dfs(x); // cerr<<"a cycle appeared. RIP removed0="<<ans<<"\n"; ++ST; int cl=0; for(int u=f;u;u=fa[u]) ++cl,mk[u]=ST,bl[u]=x,rs[u]=0; // cerr<<"cycle with len "<<cl<<" removed="<<ans<<"\n"; rs[x]=cl; for(auto w:td) { cs[w]=rdep[w]=0; if(w==x); else { ff[w]=fa[w]; if(mk[w]!=ST) rs[w]=1; } } // for(auto w:td) { // cerr<<w<<"haha "<<gd(w)<<"\n"; // } ans+=cl*(ll)(cl-1); for(auto w:td) { // cerr<<w<<":"<<rs[w]<<" "<<fa[w]<<" "<<ts[w]<<"\n"; ans+=rs[w]*(ll)(ts[w]-rs[w]); } } else { assert(gf(x)==x); fa[x]=f;son[f].pb(x); // cerr<<"depth="<<gd(f)<<"\n"; // cerr<<"depth2="<<gd(f)<<"\n"; ans+=sz[x]*(ll)gd(f); if(dp[x].size()>=dp[g].size()) swap(dp[x],dp[g]); for(auto u:dp[x]) dp[g].pb(u); ff[x]=f; sz[g]+=sz[x]; sz[x]=-1e9; // cerr<<"rst="<<gd(x)<<"\n"; for(auto u:dp[x]) cons(u); dp[x].clear(); // for(auto D:dp[g]) cout<<D<<",";cout<<"\b finale\n"; deal_queue(); } // cerr<<"ans\'="<<ans<<"\n"; } } void add(int x) { if(!so[x].size()) { cerr<<"warning: empty "<<x<<" edgelist\n"; } // cerr<<"add "<<x<<"\n"; good[x]=1; for(auto w:ds[x]) dp[x].pb(w); cons(x); for(auto u:dp[x]) cons(u); deal_queue(); // cerr<<"cur ans: "<<ans<<"\n"; } ll br() { ll x=0; for(int i=0;i<10;++i) x=x*(RAND_MAX+1)+rand(); if(x<0) x=-x;return x; } int main() { // n=2e5; scanf("%d",&n); for(int i=1;i<=n;++i) sz[i]=rs[i]=1; for(int i=1,l,x;i<=n;++i) { // l=2+rand()%2; scanf("%d",&l); while(l--) // x=br()%100+1, scanf("%d",&x), so[i].insert(x), ds[x].insert(i); } // cerr<<"EZ\n"; for(int i=1;i<=n;++i) { add(i); printf("%lld ",ans); } // cerr<<clock()<<"ms\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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | #include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <string> #include <bitset> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> #include <iomanip> #include <assert.h> using namespace std; #define pb push_back #define mp make_pair typedef pair<int,int> pii; typedef long long ll; typedef double ld; typedef vector<int> vi; #define fi first #define se second #define fe first #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);} #define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);} #define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);} #define es(x,e) (int e=fst[x];e;e=nxt[e]) #define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e]) #define SZ 666666 int n,eg[SZ]; set<int> so[SZ]; set<int> ds[SZ]; bool good[SZ]; int ff[SZ],sz[SZ]; vector<int> dp[SZ]; int ss[SZ],rs[SZ],cs[SZ],rdep[SZ]; int sn; int gf(int x) { // return ff[x]?ff[x]=gf(ff[x]):x; if(!ff[x]) return x; int u=gf(ff[x]); if(ff[x]!=u) cs[x]+=cs[ff[x]]+rs[ff[x]], rdep[x]+=rdep[ff[x]]+1; ff[x]=u; return u; } int gd(int x) { int g=gf(x); if(x==g) return rs[x]; return cs[x]+rs[x]+rs[g]; } #define U 20 int fa[SZ],up[200055][U]; int grdep(int x) { // int w=-1; // for(int f=x;f;f=fa[f]) ++w; // return w; int g=gf(x); if(x==g) return 0; return rdep[x]+1; } int gup(int x,int d) { if((1<<d)>grdep(x)) return 0; if(up[x][d]) return up[x][d]; if(d==0) { assert(fa[x]); return up[x][d]=fa[x]; } return up[x][d]=gup(gup(x,d-1),d-1); } int lca(int x,int y) { int dx=grdep(x),dy=grdep(y); if(dx>dy) swap(x,y),swap(dx,dy); dy-=dx; for(int i=0;i<U;++i) if(dy&(1<<i)) y=gup(y,i); if(x==y) return x; for(int i=U-1;i>=0;--i) if(gup(x,i)!=gup(y,i)) x=up[x][i],y=up[y][i]; return gup(x,0); // set<int> f; // for(int u=x;u;u=fa[u]) f.insert(u); // for(int u=y;;u=fa[u]) { // if(f.count(u)) return u; // } // return -1; //unreachable } void cons(int x) { // cerr<<" cons "<<x<<"\n"; if(eg[x]||!good[x]) return; set<int> pf; bool g=1; for(auto w:so[x]) { g&=good[w],pf.insert(gf(w)); if(!g||pf.size()>1) break; } if(g&&pf.size()==1) { int t=0; for(auto w:so[x]) { if(!t) t=w; else t=lca(t,w); } // cerr<<" gj "<<x<<"->"<<t<<"\n"; eg[x]=t; ss[++sn]=x; } } ll ans=0; vector<int> son[SZ]; ll ts[SZ]; vector<int> td; void dfs(int x) { ts[x]=0; td.pb(x); for(auto s:son[x]) { dfs(s); ts[x]+=ts[s]; } ans-=ts[x]++; } int ST=0,mk[SZ],bl[SZ]; void deal_queue() { while(sn) { int x=ss[sn--],f=eg[x]; if(bl[f]) f=bl[f]; assert(!bl[x]); // cerr<<x<<"->"<<f<<"?\n"; // for(auto D:dp[x]) cout<<D<<",";cout<<"|"; // for(auto D:dp[f]) cout<<D<<",";cout<<" \n"; int g=gf(f); if(gf(x)==gf(f)) { assert(g==x); td.clear(); dfs(x); // cerr<<"a cycle appeared. RIP removed0="<<ans<<"\n"; ++ST; int cl=0; for(int u=f;u;u=fa[u]) ++cl,mk[u]=ST,bl[u]=x,rs[u]=0; // cerr<<"cycle with len "<<cl<<" removed="<<ans<<"\n"; rs[x]=cl; for(auto w:td) { cs[w]=rdep[w]=0; if(w==x); else { ff[w]=fa[w]; if(mk[w]!=ST) rs[w]=1; } } // for(auto w:td) { // cerr<<w<<"haha "<<gd(w)<<"\n"; // } ans+=cl*(ll)(cl-1); for(auto w:td) { // cerr<<w<<":"<<rs[w]<<" "<<fa[w]<<" "<<ts[w]<<"\n"; ans+=rs[w]*(ll)(ts[w]-rs[w]); } } else { assert(gf(x)==x); fa[x]=f;son[f].pb(x); // cerr<<"depth="<<gd(f)<<"\n"; // cerr<<"depth2="<<gd(f)<<"\n"; ans+=sz[x]*(ll)gd(f); if(dp[x].size()>=dp[g].size()) swap(dp[x],dp[g]); for(auto u:dp[x]) dp[g].pb(u); ff[x]=f; sz[g]+=sz[x]; sz[x]=-1e9; // cerr<<"rst="<<gd(x)<<"\n"; for(auto u:dp[x]) cons(u); dp[x].clear(); // for(auto D:dp[g]) cout<<D<<",";cout<<"\b finale\n"; deal_queue(); } // cerr<<"ans\'="<<ans<<"\n"; } } void add(int x) { if(!so[x].size()) { cerr<<"warning: empty "<<x<<" edgelist\n"; } // cerr<<"add "<<x<<"\n"; good[x]=1; for(auto w:ds[x]) dp[x].pb(w); cons(x); for(auto u:dp[x]) cons(u); deal_queue(); // cerr<<"cur ans: "<<ans<<"\n"; } ll br() { ll x=0; for(int i=0;i<10;++i) x=x*(RAND_MAX+1)+rand(); if(x<0) x=-x;return x; } int main() { // n=2e5; scanf("%d",&n); for(int i=1;i<=n;++i) sz[i]=rs[i]=1; for(int i=1,l,x;i<=n;++i) { // l=2+rand()%2; scanf("%d",&l); while(l--) // x=br()%100+1, scanf("%d",&x), so[i].insert(x), ds[x].insert(i); } // cerr<<"EZ\n"; for(int i=1;i<=n;++i) { add(i); printf("%lld ",ans); } // cerr<<clock()<<"ms\n"; } |