您的位置:首页 > 其它

【HDOJ】1285 确定比赛名次

2014-04-13 17:38 288 查看
拓扑排序,居然要考虑重边,使用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, 0, sizeof(node));
memset(map, 0, sizeof(map));

for (i=0; i<m; ++i) {
scanf("%d %d", &a, &b);
if (map[a][b] == 0)
node[b]++;
map[a][b] = 1;
}
for (i=1; i<=n; ++i)
if (node[i] == 0)
teams.push(i);
j = 0;
while ( !teams.empty() ) {
a = teams.top();
teams.pop();
if (j == 0) {
printf("%d", a);
j = 1;
} else
printf(" %d", a);
for (i=1; i<=n; ++i)
if (map[a][i]) {
node[i]--;
if (node[i] == 0)
teams.push(i);
}
}
printf("\n");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: