码迷,mamicode.com
首页 > 编程语言 > 详细

HDU 1285 确定比赛名次【字典序最小的拓扑排序 + 优先队列】

时间:2018-06-13 21:44:28      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:double   NPU   using   mis   比赛   tput   ++   AC   fir   

确定比赛名次
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33964 Accepted Submission(s): 13321

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
杭电ACM集训队训练赛(VII)

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e5 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int n,m,num,ok,u,v;
vector<int> G[maxn];
int ans[maxn];

char s[10];
int inDeg[maxn],x;
int a,c;
char b;
priority_queue<int,vector<int>,greater<int> > q;

void topSort()
{
    int num=0;
    while(!q.empty()) q.pop();
    for(int i=1;i<=n;i++) if(!inDeg[i]) q.push(i);
    while(!q.empty())
    {
        int now = q.top();
        q.pop();
        ans[++num]=now;
        for(int i=0; i<G[now].size(); i++)
        {
            int nxt = G[now][i];
            if(--inDeg[nxt] == 0) q.push(nxt);
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        ms(inDeg,0);
        ms(ans,0);
        for(int i=1;i<=n;i++) G[i].clear();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            inDeg[v]++;
        }
        topSort();
        for(int i=1;i<n;i++)
            printf("%d ",ans[i]);
        printf("%d\n",ans[n]);
    }
    return 0;
}

HDU 1285 确定比赛名次【字典序最小的拓扑排序 + 优先队列】

标签:double   NPU   using   mis   比赛   tput   ++   AC   fir   

原文地址:https://www.cnblogs.com/Roni-i/p/9180180.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!