题意:拓扑排序,不知到为什么用小白上可以判断回路的写会出错
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 600;
int G[MAXN][MAXN],in[MAXN],ans[MAXN];
int n,m;
void topsort(){
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (G[i][j])
in[j]++;
for (int i = 1; i <= n; i++){
int k = 1;
while (in[k] != 0)
k++;
ans[i] = k;
in[k]--;
for (int j = 1; j <= n; j++)
if (G[k][j])
in[j]--;
}
}
int main(){
while (scanf("%d%d",&n,&m) != EOF){
memset(in,0,sizeof(in));
memset(ans,0,sizeof(ans));
memset(G,0,sizeof(G));
for (int i = 0; i < m; i++){
int a,b;
scanf("%d%d",&a,&b);
G[a][b] = 1;
}
topsort();
for (int i = 1; i < n; i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n]);
}
return 0;
}