Genealogy Tree,
[Description]
There is a huge family of people, and the relationship between generations is very chaotic. Please help us sort out this relationship.
Provide information about each person's children.
Input a sequence so that each person's generation is listed after that person.
[Input]
The first line is an integer (1 <= N <= 100), indicating the number of people in the family.
The next N rows indicate the son of the I-th person.
0 indicates that the description is complete.
[Output]
Output a sequence so that each person's generation is listed after that person.
If multiple solutions exist, output any solution.
[Input example]
5
0
4 5 1 0
1 0
5 3
0
3 0
[Output example]
2 4 5 3 1
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int n; 7 int main(){ 8 ios::sync_with_stdio(false); 9 cin>>n;10 int r[101]={0},c[101]={0},a[101][101]={0};11 int tot=0,ans[101]={0},m=0,t;12 int i,j;13 for(i=1;i<=n;i++){14 cin>>j;15 while(j!=0){16 c[i]++;17 a[i][c[i]]=j;18 r[j]++;19 cin>>j;20 }21 }22 for(i=1;i<=n;i++) if(r[i]==0)ans[++tot]=i;23 while(m!=n){24 t=ans[tot];25 cout<<t<<' ';26 tot--;27 m++;28 for(i=1;i<=c[t];i++){29 r[a[t][i]]--;30 if(r[a[t][i]]==0)31 ans[++tot]=a[t][i];32 }33 }34 return 0;35 }