当前位置: 代码迷 >> 综合 >> HDU - 1285 -确定比赛名次【拓扑排序】
  详细解决方案

HDU - 1285 -确定比赛名次【拓扑排序】

热度:67   发布时间:2023-11-20 05:56:41.0

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285

#include<bits/stdc++.h>
using namespace std;
const int maxn = 515;
int mp[maxn][maxn];
int _in[maxn];
int ans[maxn];
int N, M;
void init()
{memset(_in, 0, sizeof(_in));memset(ans, 0, sizeof(ans));memset(mp, 0, sizeof(mp));
}
void toposort()
{for (int i = 1; i <= N; i++){for (int j = 1; j <= N; j++){if(mp[i][j]) _in[j]++;}}for (int i = 1; i <= N; i++){int k = 1;while(_in[k] != 0) k++;ans[i] = k;_in[k] = -1;for (int j = 1; j <= N; j++){if(mp[k][j]) _in[j]--;}}
}
int main()
{while(~scanf("%d%d", &N, &M)){init();for (int i = 0; i < M; i++){int a, b;scanf("%d%d", &a, &b);mp[a][b] = 1;}toposort();for (int i = 1; i < N; i++){printf("%d ", ans[i]);}printf("%d\n", ans[N]);}return 0;
}