Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 815 | Accepted: 578 | Special Judge |
Description
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.
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
Output
Sample Input
5 0 4 5 1 0 1 0 5 3 0 3 0
Sample Output
2 4 5 3 1
Source
#include<stdio.h>
#include<string.h>
int a[101][101],b[101],c[101],f[101];
int n,x;
void dfs()
{
int num=1,k;
while(1)
{
for(int i=1;i<=n;i++)
if(b[i]==0&&!f[i])
{
c[num++]=i;
k=i;
f[i]=true;
break;
}
for(int i=1;i<=a[k][0];i++)
b[a[k][i]]--;
if(num>n) break;
}
for(int i=1;i<num-1;i++)
printf("%d ",c[i]);
printf("%d/n",c[num-1]);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(f,false,sizeof(f));
memset(b,0,sizeof(b));
for(int i=1;i<=n;i++)
{
int num=1;
while((scanf("%d",&x),x)!=0)
{
a[i][num++]=x;
b[x]++;
}
a[i][0]=num-1;
}
dfs();
}
return 0;
}