hdu 1285 確定比賽名次

確定比賽名次

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7059    Accepted Submission(s): 2674


Problem 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
 


注意重邊

代碼:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxx 505
//degree[]	每個結點的入度
//f[]	每個結點所在的層
int degree[maxx];
int f[maxx];
int top;
int map[maxx][maxx];
int n,m;
struct node
{
    int p;
    int top;
} po[maxx];
void Toplogical_sort()
{
    int i,j;
    bool p=true;
    top=0;
    int tmp;
    while(p)
    {
        p=false;
        top++;
        tmp=0;
        for(i=1; i<=n; i++)
            if(degree[i]==0)
            {
                p=true;
                f[i]=top;
                tmp=i;
                break;
            }
        for(j=1; j<=n; j++)
            if(map[tmp][j])
                degree[j]--;
        degree[tmp]=-1;
    }
    top--;
}
int cmp(struct node a,struct node b)
{
    return a.top<b.top;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(map,0,sizeof(map));
        memset(degree,0,sizeof(degree));
        memset(f,0,sizeof(f));
        int a,b;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&a,&b);
            if(!map[a][b])
            {
                map[a][b]=1;
                degree[b]++;
            }
        }
        Toplogical_sort();
        for(int i=0; i<n; i++)
        {
            po[i].p=i+1;
            po[i].top=f[i+1];
        }
        sort(po,po+n,cmp);
        for(int i=0; i<n-1; i++)
            printf("%d ",po[i].p);
        printf("%d\n",po[n-1].p);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章