拓扑排序,居然要考虑重边,使用STL实现。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define MAXNUM 505 char map[MAXNUM][MAXNUM];
int node[MAXNUM];
priority_queue<int, vector<int>, greater<int> > teams; int main() {
int n, m, a, b;
int i, j; while (scanf("%d%d", &n, &m) != EOF) {
memset(node, , sizeof(node));
memset(map, , sizeof(map)); for (i=; i<m; ++i) {
scanf("%d %d", &a, &b);
if (map[a][b] == )
node[b]++;
map[a][b] = ;
}
for (i=; i<=n; ++i)
if (node[i] == )
teams.push(i);
j = ;
while ( !teams.empty() ) {
a = teams.top();
teams.pop();
if (j == ) {
printf("%d", a);
j = ;
} else
printf(" %d", a);
for (i=; i<=n; ++i)
if (map[a][i]) {
node[i]--;
if (node[i] == )
teams.push(i);
}
}
printf("\n");
} return ;
}