士兵站队问题
#include<bits/stdc++.h>
using namespace std;
int n,x,y,cnt;
int du[1005],Head[1005],nxt[2010],to[2010];
int vis[1005],use[1005];
queue<int> q,Q;
void add(int u,int v){
++cnt;
nxt[cnt]=Head[u];
to[cnt]=v;
Head[u]=cnt;
}
int main(){
cin>>n;
while(scanf("%d%d",&x,&y)!=EOF){
add(x,y);
vis[x]=vis[y]=1;
du[y]++;
}
for(int i=1;i<=n;++i)
if(!vis[i]){
puts("No answer");
return 0;
}
for(int i=1;i<=n;++i)
if(du[i]==0&&vis[i]){
Q.push(i),q.push(i);
break;
}
while(!Q.empty()){
int u=Q.front();
use[u]=1,Q.pop();
for(int i=Head[u];i;i=nxt[i]){
du[to[i]]--;
if(du[to[i]]==0)
q.push(to[i]);
}
for(int i=1;i<=n;++i)
if(du[i]==0&&vis[i]&&!use[i]){
Q.push(i);
break;
}
}
int len=q.size();
if(len!=n){
puts("No answer");
return 0;
}
while(!q.empty()){
cout<<q.front()<<' ';
q.pop();
}
}