Time limit: 1000 ms memory limit: 65536 k
Submit the total: 9926, 6219: accept special judge
Description
Martian kinship system confused, in fact, the martians bud in anytime and anywhere, they gathered in different groups, so that a Martian can have one parent and ten parents, no one would surprise for one hundred children, the martians have become accustomed to this, their way of life seems to be very natural for them,
In planetary council, confusing tree system caused some embarrassment, met the most valuable Mars people there, therefore, in order to not offend anyone in all discussions, first is to let the older martians, young people and the most young childless assessors speech, however, to maintain this command is not an easy thing, Mars was not always recognize all of his parents (don't have anything to say about his grandparents!) , but if say first due to wrong a grandson, and only appear younger than his great-grandfather, that's a scandal,
Your task is to write a program, the program would be defined once and for all a command, to ensure that the security council each member before their offspring,
Input item
The standard input of the first line contains only Numbers N, 1 & lt;=N & lt;=100 - Mars, the planet of the members of the council, according to the tradition with a history of hundreds of years, the council members of natural Numbers from 1 to N, and N lines exactly, in addition, the first line contains a list of members of the ith a child, child list is child serial number sequence, separated by Spaces of any order, item list may be empty, the list (even empty) and end with 0,
Output
The standard output should be in one line only contains the speaker serial number sequence, and space separated, if several sequences satisfy the conditions of the problem, will any one write to standard output, always has at least one such sequence,
Sample input
5
0
4 5 1 0
1 0
5 3 0
3 0
Sample output
2, 4, 5 3 1
#include
#include
# define N 100
Int arc [N] [N].//store relationship
Int S [N].//stack
Int indegree [N].//into the degree of
Int T [N].
//topological sort
Void TopoSort (int n)//n number indicates that the element
{
int i, j;
Int m;//accept stack element
Int top=1;//stack location
Int t=0;
//a into the degree of zero
for(i=1; i<=n; I++)
{
If (indegree [I]==0)
S [+ + top]=I;
}
//not empty stack
While (top!=1)
{
M=S [top --];
T [t++]=m;
for(i=1; i<=n; I++)
{
//into the degrees - 1
If (arc [m] [I]==1)
{
Indegree [I] -;
If (indegree [I]==0)
S [+ + top]=I;//into the stack
}
}
}
}
Int main ()
{
int n;//accept vertex number
Int m;//receive members
int i, j;
The scanf (" % d ", & amp; N);
Initialized//
for(i=1; i<=n; I++)
{
Indegree [I]=0;
for(j=1; J<=n; J++)
The arc [I] [j]=0;
}
for(i=1; i<=n; I++)
{
While (1)
{
The scanf (" % d ", & amp; M);
If (m==0)
break;
The arc [I] [m]=1;
Indegree [m] + +;
}
}
TopoSort (n);
for(i=0; i<=n - 2; I++)
Printf (" % d ", [I] T);
Printf (" % d ", [I] T);
}
CodePudding user response:
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.htmlI hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html