确定比赛名次
有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。
其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。
4 3 1 2 2 3 4 3
1 2 4 3
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int n,m;
bool vis[520][520];
int in[520]; //入度
int sum[520];
int ans;
void topo()
{
ans = 1 ;
int cout;
while(1)
{
bool flag = false;
for(int i = 1 ; i <= n ; i++)
{
if(in[i] == 0)
{
cout = i;
sum [ans++] = i;
flag = true;
in[i] = -1;
break;
}
}
if(!flag)
break;
for( int i = 1 ; i <= n ;i++)
{
if(vis[cout][i])
{
in[i] -- ;
vis[cout][i] = false;
}
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
memset(in, 0 , sizeof(in));
memset(vis , false ,sizeof(vis));
for(int i = 1; i <= m ; i++)
{
int x,y;
scanf("%d %d",&x,&y);
if(!vis[x][y])
{
in[y]++;
vis[x][y] = true;
}
}
topo();
for(int i = 1 ; i < n ; i++)
{
printf("%d ",sum[i]);
}
printf("%d\n",sum[n]);
}
return 0;
}