2117 problem B determine the place of competition

Keywords: iOS

Question B: determine the place of competition
Time limit: 1 Sec memory limit: 32 MB
Flower offering: 56 solution: 38
[flower offering] [wreath] [TK question bank]
Title Description
There are N teams (1 < = N < = 500) numbered 1, 2, 3,.... , N for the competition, after the competition, the referee committee will rank all the participating teams in turn from the front to the back, but now the referee committee can not directly obtain the results of each team's competition, only know the results of each game, that is, P1 wins P2, expressed by P1, P2, when ranking P1 is before P2. Now please program your ranking.
input
There are several groups of input, the first row in each group is two numbers N (1 < = N < = 500), m; where N is the number of teams, and M is the input data followed by M rows. In the next row M data, there are also two integers P1 in each row. P2 means that team P1 wins team P2.
output

Give a satisfactory ranking. When outputting, there is a space between the team numbers, and there is no space after the last one.
Other notes: the qualified ranking may not be unique. At this time, the team with small number is in the front when output is required; the input data is guaranteed to be correct, i.e. the input data ensures that there must be a qualified ranking.

sample input
3 2
3 1
3 2
17 16
16 1
13 2
7 3
12 4
12 5
17 6
10 7
11 8
11 9
16 10
13 11
15 12
15 13
17 14
17 15
17 16
0 0
sample output
3 1 2
17 6 14 15 12 4 5 13 2 11 8 9 16 1 10 7 3

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cfloat>
#include <iomanip>
#include <map>
#include <functional>

#define INF INT32_MAX
using namespace std;


const int MaxN = 510;
int G[MaxN][MaxN];
int InDegree[MaxN] = { 0 };
queue<int>res;
int N;

bool TopSort()
{
    for (int i = 1; i <= N; ++i)
    {
        for (int k = 1; k <= N; ++k)
        {
            if (G[i][k])
                ++InDegree[k];
        }
    }

    priority_queue<int,vector<int>,greater<int>> que;
    for (int i = 1; i <= N; ++i)
    {
        if (!InDegree[i])
            que.push(i);
    }

    int count = 0;

    while (que.size())
    {
        while (que.size())
        {
            int u = que.top(); que.pop();
            res.push(u); ++count;
            for (int k = 0; k < N; ++k)
            {
                if (G[u][k])
                {
                    --InDegree[k];
                    if (!InDegree[k])
                        que.push(k);
                }
            }
        }
    }

    if (count < N)return false;
    return true;
}


void QueClear(queue<int> que)
{
    while (que.size())que.pop();
}


int main()
{
#ifdef _DEBUG
    freopen("data.txt", "r+", stdin);
#endif // _DEBUG

    std::ios::sync_with_stdio(false);

    int M;
    while (cin >> N >> M)
    {
        QueClear(res);
        fill(G[0], G[0] + (N + 2) * MaxN, 0);
        for (int i = 0; i < M; ++i)
        {
            int u, v;
            cin >> u >> v;
            G[u][v] = 1;
        }

        TopSort();
        while (res.size())
        {
            cout << res.front() << " "; res.pop();
        }
        cout << endl;
    }


    return 0;
}
/**************************************************************
    Problem: 2117
    User: Sharwen
    Language: C++
    Result: Immortals
    Time:51 ms
    Memory:2736 kb
****************************************************************/

Posted by volka on Tue, 31 Mar 2020 18:43:34 -0700