B - 确定比赛名次

时间:2023-03-09 15:39:11
B - 确定比赛名次
B - 确定比赛名次

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

Description

有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。 

Input

输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;其中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1,P2表示即P1队赢了P2队。 

Output

给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。 



其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。 

Sample Input

4 3
1 2
2 3
4 3

Sample Output

1 2 4 3
放法一:
我用邻接矩阵写的,看起来有点土,不过最高兴的是我终于把自己的想法实现了,写这一题要注意有重边的情
况,要不然会RW。
劣码如下:
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
typedef struct dot{
int num;
int du;
bool falg;
}team;
#define MAX 550
int a[MAX][MAX] = {0} ;
int main()
{
int n,m;
while(cin>>n>>m)
{
int x,y;
team k[MAX]={0};
memset(a,0,sizeof(a));
memset(k,0,sizeof(k));
for(int i = 0; i < m;i++){
cin>>x>>y;
if(a[x][y] != 1)
k[y].du ++ ;
a[x][y] = 1; //cout<<y<<" "<<k[y].du <<endl;
k[y].falg = 0;
}
queue<int> q;
while(!q.empty())q.pop();
for(int i = 1; i <= n;i ++){
if(k[i].du == 0){
k[i].falg = 1;
q.push(i);
break;
}
}
/* for(int i = 1;i <= n;i ++)
cout<<k[i].du<<" ";
cout<<endl; cout<<q.size()<<endl;
*/
int f = 0,goal[600];
while(!q.empty() ){
int temp;
temp = q.front() ;
goal[f++]=temp;
// cout<<temp <<endl;
q.pop();
for(int i = 1; i <= n; i ++){
if(a[temp ][i] == 1)
k[i].du --;
}
for(int i = 1; i<= n; i ++){
if(k[i].du == 0&&k[i].falg == 0){
q.push(i);
k[i].falg = 1;
break;
}
}
}
for(int i = 0;i < f-1;i ++)
cout<<goal[i]<<" ";
cout<<goal[f-1]<<endl;
}
return 0;
}

AC好开心!B - 确定比赛名次