题目大意:中文
代码:
/*
* Problem ID : HDU 1285 确定比赛名次
* Author : Lirx.t.Una
* Language : C++
* Run Time : 15 ms
* Run Memory : 500 KB
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define MAXN 500
using namespace std;
bool g[MAXN + 1][MAXN + 1];
short deg[MAXN + 1];
priority_queue<short, vector<short>, greater<short> > heap;
queue<short> ans;
void
topsort(int n) {
int i;
int u, v;
for ( i = 1; i <= n; i++ )
if ( !deg[i] ) {
deg[i]--;
heap.push(i);
}
while ( !heap.empty() ) {
deg[ u = heap.top() ]--;
ans.push(u);
heap.pop();
for ( v = 1; v <= n; v++ )
if ( g[u][v] && !( --deg[v] ) )
heap.push(v);
}
printf("%d", ans.front());
ans.pop();
while ( !ans.empty() ) {
printf(" %d", ans.front());
ans.pop();
}
putchar('\n');
}
int
main() {
int n, m;
int u, v;
while ( ~scanf("%d%d", &n, &m) ) {
memset(g, false, sizeof(g));
memset(deg, 0, sizeof(deg));
while ( m-- ) {
scanf("%d%d", &u, &v);
if ( !g[u][v] ) {
g[u][v] = true;
deg[v]++;
}
}
topsort(n);
}
return 0;
}