标题党,这题是DAG特殊求法的支配树(
WA成30 pts了,只过了#2,#3,#5
求助
#include <cstdio>
#include <bitset>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define ri register int
#define ll long long
#define Tp template<class T>
#define g() getchar()
#define pc(x) putchar(x)
#define isd(x) (x>=48&&x<=57)
namespace SlowIO{
Tp inline void rd(T &x) {
x=0; char i=g(); bool f=1;
while(!isd(i)) f&=(i!='-'),i=g();
while(isd(i)) x=(x<<3)+(x<<1)+(i^48),i=g();
x*=((f<<1)-1);
}
const int OUT=1e6;
static char st[OUT]; int out;
Tp inline void op(T x){
out=0; x<0&&(x=-x,pc('-'));
if(!x){ pc(48); return; }
while(x) st[++out]=x%10+48,x/=10;
while(out) pc(st[out--]);
}
Tp inline void writeln(T x){ op(x);pc('\n'); }
Tp inline void writesp(T x){ op(x); pc(' '); }
Tp inline void write(T x,char c=0){ op(x); c&&pc(c); }
#define Pi pair<int,int>
#define Rp register Pi
#define fir(x) x.first
#define sec(x) x.second
inline void writep(Pi x,char c=0){ writesp(fir(x)),write(sec(x),c); }
}; using namespace SlowIO;
#define N 65536
struct Graph{
int head[N],cntr;
struct edge{
int to,nxt;
}e[400001];
inline void add(int u,int v){
e[++cntr]=(edge){v,head[u]};
head[u]=cntr;
}
}ori,tr;
#define gforeo(u) for(ri i=ori.head[u];i;i=ori.e[i].nxt)
#define gforet(u) for(ri i=tr.head[u];i;i=tr.e[i].nxt)
int n,S;
int lst[N];
int fa[N][22];
int indeg[N];
bitset<N> vis;
int q[N],l,r;
int siz[N],depth[N];
inline void dfs(int u){
siz[u]=1;
gforet(u){ int &v=tr.e[i].to;
if(v==lst[u]) continue;
dfs(v),siz[u]+=siz[v];
}
}
#define swp(a,b) (a^=b^=a^=b)
inline int LCA(int a,int b){
if(!a||!b) return a+b;
if(depth[a]<depth[b]) swp(a,b);
for(ri i=19;i>=0;--i)
if(depth[fa[a][i]]>=depth[b]) a=fa[a][i];
if(a==b) return a;
for(ri i=19;i>=0;--i)
if(fa[a][i]!=fa[b][i]) a=fa[a][i],b=fa[b][i];
return fa[a][0];
}
inline void topo(){
q[l=r=1]=S;
depth[S]=1;
while(l<=r){
int now=q[l++];
gforeo(now){ int &v=ori.e[i].to;
--indeg[v];
if(vis[v]){
lst[v]=now;
} else
lst[v]=LCA(lst[v],lst[now]);//,printf("now at %d and lst is %d\n",now,lst[now]);
if(!indeg[v]){
q[++r]=v;
tr.add(lst[v],v),tr.add(v,lst[v]);
fa[v][0]=lst[v];
depth[v]=depth[lst[v]]+1;
// printf("lst[%d] is %d\n",v,lst[v]);
// printf("depth[%d] is %d\n",v,depth[v]);
for(ri i=1;(1<<i)<=depth[v];++i)
fa[v][i]=fa[fa[v][i-1]][i-1];
}
}
}
}
int main()
{
rd(n); S=n+1;
for(ri i=1,a;i<=n;++i){
while(true){
rd(a);
if(!a) break;
ori.add(a,i);
++indeg[i];
}
if(!indeg[i])
ori.add(S,i),++indeg[i];
vis[i]=(indeg[i]==1);
} topo();
dfs(S);
for(ri i=1;i<=n;++i)
write(siz[i]-1,'\n');
return 0;
}