HDU——1285 肯定比賽名次

肯定比賽名次

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


php

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
 

 

Author
SmallBeer(CML)
 

 

Source
 

 

Recommend
lcy
 
原汁原味的拓撲、、、、
鬼畜的輸出、、、、
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N  10000
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
int n,m,x,y,s,tot,in[N],head[N],ans[N];
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
struct Edge
{
    int from,to,next;
}edge[N];
int add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        s=0,tot=0;
        memset(in,0,sizeof(in));
        memset(ans,0,sizeof(ans));
        memset(head,0,sizeof(head));
        memset(edge,0,sizeof(edge));
        for(int i=1;i<=m;i++)
        {
            x=read(),y=read();
            in[y]++;add(x,y);
        }
        for(int i=1;i<=n;i++)
         if(in[i]==0) q.push(i);
        while(!q.empty())
        {
            x=q.top(),q.pop();
            ans[++s]=x;
            for(int i=head[x];i;i=edge[i].next)
            {
                int t=edge[i].to;
                in[t]--;
                if(in[t]==0) q.push(t);
            }
        }
        for(int i=1;i<s;i++)
         printf("%d ",ans[i]);
        printf("%d\n",ans[s]);
    }
    return 0;
}
相關文章
相關標籤/搜索