1 /**************************************************************
2 Problem: 2815
3 User: Doggu
4 Language: C++
5 Result: Accepted
6 Time:216 ms
7 Memory:17152 kb
8 ****************************************************************/
9
10 #include <cstdio>
11 #include <algorithm>
12 template<class T>inline void readin(T &res) {
13 static char ch;T flag=1;
14 while((ch=getchar())<‘0‘||ch>‘9‘)if(ch==‘-‘)flag=-1;
15 res=ch-48;while((ch=getchar())>=‘0‘&&ch<=‘9‘)res=(res<<1)+(res<<3)+ch-48;res*=flag;
16 }
17
18 const int N = 100000;
19 const int M = 100000;
20 const int S = 20;
21 struct Edge {int v, upre;};
22 struct CON {
23 Edge e[M];int head[N], ne;
24 void adde(int u,int v) {e[++ne]=(Edge){v,head[u]};head[u]=ne;}
25 }g, vg, gg;
26
27 int n, in[N], p[N], topo[N], stack[N], top, anc[N][S+1], dep[N], siz[N];
28 void topo_sort() {
29 int t=0;for( int i = 1; i <= n; i++ ) if(!in[i]) stack[++top]=i;
30 while(top!=0) {
31 int u=stack[top--];topo[++t]=u;
32 for( int i=g.head[u]; i; i=g.e[i].upre ) {
33 int v=g.e[i].v;
34 in[v]--;if(!in[v]) stack[++top]=v;
35 }
36 }
37 }
38 int LCA(int u,int v) {
39 if(dep[u]<dep[v]) std::swap(u,v);
40 for( int t=dep[u]-dep[v], p=0; t; t>>=1, p++ ) if(t&1) u=anc[u][p];
41 if(u==v) return u;
42 for( int p=S; p>=0&&anc[u][0]!=anc[v][0]; p-- ) if(anc[u][p]!=anc[v][p]) u=anc[u][p], v=anc[v][p];
43 return anc[u][0];
44 }
45 void DFS(int u,int fa) {
46 siz[u]=1;
47 for( int i=gg.head[u]; i; i=gg.e[i].upre ) {
48 int v=gg.e[i].v;
49 if(v==fa) continue;
50 DFS(v,u);
51 siz[u]+=siz[v];
52 }
53 }
54 int main() {
55 readin(n);
56 for( int v = 1,u; v <= n; v++ ) while(1) {
57 readin(u);if(u==0) break;
58 g.adde(u,v);
59 vg.adde(v,u);
60 in[v]++;p[v]++;
61 }
62 for( int v = 1; v <= n; v++ ) if(!in[v]) g.adde(n+1,v), vg.adde(v,n+1), in[v]++, p[v]++;
63 n++;topo_sort();
64 for( int i = 1; i <= n; i++ ) {
65 anc[i][0]=i;
66 dep[i]=1;
67 }
68 for( int i = 1; i <= n; i++ ) {
69 int u=topo[i];
70 if(!p[u]) continue;bool first=1;int ca;
71 for( int i=vg.head[u]; i; i=vg.e[i].upre ) {
72 int v=vg.e[i].v;
73 if(first) ca=v, first=0;
74 else ca=LCA(ca,v);
75 }
76 gg.adde(ca,u);
77 anc[u][0]=ca;
78 for( int i = 1; i <= S; i++ ) anc[u][i]=anc[anc[u][i-1]][i-1];
79 dep[u]=dep[ca]+1;
80 }
81 DFS(n,n);
82 for( int i = 1; i < n; i++ ) printf("%d\n",siz[i]-1);
83 return 0;
84 }
85