#include<iostream>
#include<queue>
using namespace std;
int a[105][105],n;
int stage[105];
void tupu()
{
queue<int>q;
int i,j,sign=0;
for(i=1; i<=n; i++)
if(stage[i]==0)
q.push(i);
while(q.empty()==false)
{
int t=q.front();
q.pop();
if(sign==1)
cout<<" ";
sign=1;
cout<<t;
j=1;
while(a[t][j]!=0)
{
stage[a[t][j]]--;
if(stage[a[t][j]]==0)
q.push(a[t][j]);
j++;
}
}
cout<<endl;
}
int main()
{
int i,j;
cin>>n;
for(i=1; i<=n; i++)
stage[i]=0;
for(i=1; i<=n; i++)
{
int m;
j=1;//为了一致,都从1开始的
while(1)
{
cin>>m;
a[i][j++]=m;
if(m==0)
break;
stage[m]++;
}
}
tupu();
return 0;
}
/
用二重循环的
#include<iostream>
using namespace std;
int main()
{
int N,i,j,a[101][101],m,pre[101];
cin>>N;
for(i=1; i<=N; i++)
pre[i]=0;
for(i=1; i<=N; i++)
{
j=0;
while(1)
{
cin>>m;
a[i][j++]=m;
if(m==0)//子女的个数是以0表示结尾的
break;
pre[m]++;//前驱加一的
}
}
int sum=0;
while(1)
{
for(i=1; i<=N; i++)
{
if(pre[i]==0)
{
if(sum!=0)
cout<<" ";
cout<<i;
sum++;
pre[i]=-1;
j=0;
while(a[i][j]!=0)
{
pre[a[i][j++]]--;
}
}
if(sum==N)
break;
}
if(sum==N)
break;
}
cout<<endl;
return 0;
}