Description
In an ecosystem, the maintenance of the food chain is very important. The disruption of the food chain often causes a chain reaction that leads to an ecosystem collapsing like a domino.
Now consider a simplified model. In an ecosystem, there are $n$ species, which are divided into two categories: producers and consumers. Producers survive through energy outside the system, the most common of which is plant photosynthesis. And consumers need to "consume", that is, other organisms as food to survive. In order to simplify the problem, we assume that all consumers can be layered, the upper tier of consumers may be the source of food from its strict lower level. The producer can be regarded as the lowest member. When a species is extinct, consumers who depend on it become extinct because all food disappears. The extinction of these consumers could further lead to the extinction of its upper members. We define the indicator $c (x) $, which is used to denote the number of organisms that are extinct with it when x disappears from the ecosystem.
Given a food chain model, for all mobs, calculate its extinction indicator $c (x) $.
Input
The first line is an integer $n that represents the number of mobs. Mobs begin numbering from $1$.
Next $n$ line, line $i$ represents the food list of the $i$ species. The number of the two foods is separated by a space, and the input $0$ indicates the end of the bank. If there is only one $0$ in this line, it is a producer who cannot be thought of as having no food and is naturally extinct.
Output
A total of $n$, line $i$ is the extinction indicator of the $i$ creature $c (i) $.
Sample Input
501 01 02 3 02 0
Sample Output
4 1 0 0 0
HINT
$N \;\leq\;65534$, the input is guaranteed to be legal (i.e. it can be layered). The input file size does not exceed $1mb$.
Solution
Obviously, the whole food net is $dag$.
The graph is layered using the $topo$ sequence.
A species will perish when and only if the public ancestors of all its food perish.
Achievements, the common ancestor of all food of each species, to each species of one side, to seek the size of the sub-tree.
#include <cmath>#include<ctime>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#defineK 17#defineN 65535#defineM 300000using namespacestd;structgraph{intNxt,to;} E[n<<1],e1[m],e2[m];structlives{intN,DEP;} A[n];intF[n][k],t[n],g[n],g1[n],g2[n],dep[n],siz[n],n,cnt,tot;queue<int>Q;inlinevoidAdde1 (intXinty) {e1[++cnt].nxt=g1[x];g1[x]=cnt;e1[cnt].to=y;} InlinevoidAdde2 (intXinty) {e2[++tot].nxt=g2[x];g2[x]=tot;e2[tot].to=y;} InlinevoidAddedge (intXinty) {e[++cnt].nxt=g[x];g[x]=cnt;e[cnt].to=y;} InlineBOOLcmp (lives x,lives y) {returnx.dep<Y.DEP;} InlinevoidToposort () {intu; for(intI=1; i<=n;++i)if(!T[i]) Q.push (i); while(!Q.empty ()) {u=q.front (); Q.pop (); a[u].dep=++tot; for(intI=g1[u];i;i=e1[i].nxt)if(! (--t[e1[i].to])) Q.push (e1[i].to); }}inlinevoidDfsintu) {Siz[u]=1; for(intI=g[u];i;i=e[i].nxt) {DFS (e[i].to); Siz[u]+=Siz[e[i].to]; }}inlineintSwimintXinth) { for(intI=0; h;++i,h>>=1) if(h&1) x=F[x][i]; returnx;} InlineintLcaintXinty) { inti,t; if(dep[x]<Dep[y]) {T=x;x=y;y=T; } x=swim (x,dep[x]-Dep[y]); if(x==y)returnx; while(true){ for(i=0; f[x][i]!=f[y][i];++i); if(!i)returnf[x][0]; X=f[x][i-1];y=f[y][i-1]; }}inlinevoidAireen () {scanf ("%d",&N); for(intI=1, k;i<=n;++i) { while(true) {scanf ("%d",&k); if(!k) Break; Adde1 (k,i);++T[i];adde2 (i,k); }} tot=cnt=0; Toposort (); for(intI=1; i<=n;++i) A[I].N=i; Sort (a+1, A +1+n,cmp); for(intq=1, i,j,l;q<=n;++q) {i=a[q].n;j=G2[i]; if(j) {L=e2[j].to; for(j=e2[j].nxt;j;j=e2[j].nxt) L=LCA (E2[J].TO,L); Addedge (L,i); f[i][0]=l; for(intk=1; k<k;++k) F[i][k]=f[f[i][k-1]][k-1]; Dep[i]=dep[l]+1; } Else{Addedge (0, i); for(intk=0; k<k;++k) F[i][k]=0; Dep[i]=1; }} DFS (0); for(intI=1; i<=n;++i) printf ("%d\n", siz[i]-1);}intMain () {Freopen ("catas.in","R", stdin); Freopen ("Catas.out","W", stdout); Aireen (); Fclose (stdin); Fclose (stdout); return 0;}
[Daily training] Catastrophe