Genealogy Tree,

Source: Internet
Author: User

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 }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.