zoukankan      html  css  js  c++  java
  • HDU-1285-确定比赛名次

    链接:https://vjudge.net/problem/HDU-1285

    题意:

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

    思路:

    拓扑排序,group记录每个点赢的人,in数组记录每个点的入度。

    对每个入度为0的点添加到优先队列中,同时取出来的时候把这个点赢的点的入度都减1,题目数据保证正确性。

    代码:

    #include <iostream>
    #include <memory.h>
    #include <vector>
    #include <map>
    #include <algorithm>
    #include <cstdio>
    #include <math.h>
    #include <queue>
    
    using namespace std;
    
    typedef long long LL;
    
    const int MAXN = 500 + 10;
    
    vector<int> Group[MAXN];
    int in[MAXN];
    int n, m;
    
    vector<int> topo()
    {
        vector<int> res;
        priority_queue<int> que;
        for (int i = 1; i <= n;i++)
            if (in[i] == 0)
                que.push(-i);
    
        while (!que.empty())
        {
            int now = -que.top();
            que.pop();
            res.push_back(now);
            for (auto x : Group[now])
                if (--in[x] == 0)
                    que.push(-x);
        }
    
        return res;
    
    }
    
    int main()
    {
        int l, r;
        while (~scanf("%d%d", &n, &m))
        {
            for (int i = 1;i <= n;i++)
                Group[i].clear();
            memset(in, 0, sizeof(in));
    
            for (int i = 1; i <= m; i++)
            {
                scanf("%d%d", &l, &r);
                Group[l].push_back(r);
                in[r]++;
            }
    
            vector<int> res = topo();
            int cnt = 0;
            for (auto x : res)
            {
                if (cnt > 0)
                    cout << ' ' ;
                cout << x;
                cnt ++;
            }
            cout << endl;
        }
    
    
        return 0;
    }
    

      

     

  • 相关阅读:
    JAVA 线程安全与同步机制
    JAVA 多线程
    el-table 宽度自适应bug
    详解迭代器Iterator
    理解基本类型的溢出
    理解classpath
    I/O(一):基础知识
    C++: 智能指针
    C++: 值类别与移动语义基础
    CUDA 架构与编程概述
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10486596.html
Copyright © 2011-2022 走看看