1、纯拓扑排序。。。在想这么水的题怎么会WA,后来才发现不应写whlie(scanf("%d%d",&v,&e)==2&&v&&e),而应是while后加上if(!v&&!e) break,因为e可以为0!这时也要输出东西来,因为e为0代表对顺序没有要求,不是输不出排列!
2、以后一律采用第二种方案读入!不过交了后却发现还是WA,仔细查后发现忘记using namespace std了,加上后果断AC~囧。。。
#include<cstdio>
#include<cstring>
using namespace std;
int v,e,G[110][110],x,y;
int c[110],topo[110],t;
bool dfs(int u){
c[u]=-1;
for(int i=1;i<=v;i++)
if(G[u][i]){
if(c[i]<0) return false;
else if(!c[i]&&!dfs(i)) return false;
}
c[u]=1;topo[t--]=u;
return true;
}
bool toposort(){
t=v;
memset(c,0,sizeof(c));
for(int u=1;u<=v;u++)
if(!c[u])
if(!dfs(u)) return false;
return true;
}
int main(){
while(scanf("%d%d",&v,&e)==2){
if(!v&&!e) break;
memset(G,0,sizeof(G));
memset(topo,0,sizeof(topo));
for(int i=0;i<e;i++){
scanf("%d%d",&x,&y);
G[x][y]=1;
}
if(toposort()){
printf("%d",topo[1]);
for(int i=2;i<=v;i++)
printf(" %d",topo[i]);
printf("\n");
}
}
return 0;
}