1022. Genealogical Tree

Background

The system of Martians’ blood relations is confusing enough. Actually, Martians bud when they want and where they want. They gather together in different groups, so that a Martian can have one parent as well as ten. Nobody will be surprised by a hundred of children. Martians have got used to this and their style of life seems to them natural.
And in the Planetary Council the confusing genealogical system leads to some embarrassment. There meet the worthiest of Martians, and therefore in order to offend nobody in all of the discussions it is used first to give the floor to the old Martians, than to the younger ones and only than to the most young childless assessors. However, the maintenance of this order really is not a trivial task. Not always Martian knows all of his parents (and there’s nothing to tell about his grandparents!). But if by a mistake first speak a grandson and only than his young appearing great-grandfather, this is a real scandal.

Problem

Your task is to write a program, which would define once and for all, an order that would guarantee that every member of the Council takes the floor earlier than each of his descendants.

Input

The first line of the standard input contains an only number N, 1 ≤ N ≤ 100 — a number of members of the Martian Planetary Council. According to the centuries-old tradition members of the Council are enumerated with integers from 1 up to N. Further, there are exactly N lines, moreover, the i-th line contains a list of i-th member’s children. The list of children is a sequence of serial numbers of children in a arbitrary order separated by spaces. The list of children may be empty. The list (even if it is empty) ends with 0.

Output

The standard output should contain in its only line a sequence of speakers’ numbers, separated by spaces. If several sequences satisfy the conditions of the problem, you are to write to the standard output any of them. At least one such sequence always exists.

Sample

inputoutput
5
0
4 5 1 0
1 0
5 3 0
3 0
2 4 5 3 1
**************************************************************************************
拓扑排序
**************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cctype>
 6 #include<cstdio>
 7 #include<map>
 8 #include<stack>
 9 #include<queue>
10 #include<vector>
11 using namespace std;
12 int ind[1000],L[1000];//ind数组为第i点的入度,L记录对应顺序的点
13 vector<int>g[105];//记录以i为前驱的子节点
14 int n,d,i,j,k,tot;
15 void toposort()
16 {
17     queue<int>Q;//LIFO符合本题的要求
18     for(i=1;i<=n;i++)
19      if(ind[i]==0)
20        Q.push(i);//刚开始无前驱的地位最高
21     tot=0;
22     while(!Q.empty())
23       {
24           int h=Q.front();//每次拿出一个无前驱的点,去掉其与孩子节点的边
25           Q.pop();
26           L[tot++]=h;
27           for(int j=0;j<g[h].size();j++)
28            {
29                ind[g[h][j]]--;//减孩子节点的入度
30                if(ind[g[h][j]]==0)//如果该节点此时不再有前驱,则放入队中
31                 {
32                     Q.push(g[h][j]);
33                 }
34            }
35       }
36 }
37 int main()
38 {
39     cin>>n;
40     memset(ind,0,sizeof(ind));
41     for(i=1;i<=n;i++)
42      {
43          while(cin>>d&&d)
44           {
45               g[i].push_back(d);//记录放入第i节点的孩子
46               ind[d]++;//记录相应节点的入度数量
47           }
48      }
49      toposort();
50      for(i=0;i<n;i++)
51       cout<<L[i]<<' ';//输出结果
52     cout<<endl;
53     return 0;
54 }
View Code
原文地址:https://www.cnblogs.com/sdau--codeants/p/3238086.html