zoukankan      html  css  js  c++  java
  • hdu1285 确定比赛名次(拓扑排序)

    确定比赛名次

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 51   Accepted Submission(s) : 32

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

    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<cstdio>
    #include<cstring>
    #include<vector>
    #include<queue>
    using namespace std;
    int i,n,m;
    int a[505],cnt[505];
    struct cmp
    {
        bool operator()(int a,int b)
        {
            return a>b;
        }
    };
    vector<int> s[505];
    void toposort()
    {
        priority_queue<int,vector<int>,cmp> Q;
        int l=0;
        for(int i=1;i<=n;i++)
            if (cnt[i]==0) Q.push(i);
        while(!Q.empty())
        {
            int u=Q.top();
            Q.pop();
            a[++l]=u;
            for(int i=0;i<s[u].size();i++)
            {
                cnt[s[u][i]]--;
                if (cnt[s[u][i]]==0) Q.push(s[u][i]);
            }
        }
        return;
    }
    int main()
    {
        memset(cnt,0,sizeof(cnt));
        while(~scanf("%d%d",&n,&m))
        {
            for(i=1;i<=n;i++) s[i].clear();
            for(i=1;i<=m;i++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                s[x].push_back(y);
                cnt[y]++;
            }
            toposort();
            for(i=1;i<n;i++)printf("%d ",a[i]);
            printf("%d\n",a[n]);
        }
        return 0;
    }
  • 相关阅读:
    larave5.6 引入自定义函数库时,报错不能重复定义
    laravel获取当前认证用户登录
    淘宝免费ip地址查询导致服务堵死的坑
    this关键字
    Jsp Spring Security 权限管理系统
    spring secrity
    spring bean何时实例化
    Spring Security3 页面 权限标签
    Spring常用注解,自动扫描装配Bean
    java继承时,实例化子类,是否会默认调用父类构造方法
  • 原文地址:https://www.cnblogs.com/stepping/p/5700367.html
Copyright © 2011-2022 走看看