201832026-01-03 22:09:27szabelrTestnevelés óracpp17Elfogadva 50/50172ms15544 KiB
// Testnevelés óra.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, k;
    cin >> n >> k;
    vector<vector<int>> adj(n + 1);
    vector<int> indegree_1(n + 1,0);
    vector<int> indegree_2(n + 1,0);
    for (int i = 1; i <= k; i++)
    {
        int x, y;
        cin >> x >> y;
        adj[x].push_back(y);
        indegree_1[y]++;
        indegree_2[y]++;
    }
    priority_queue<int> pq_max;
    priority_queue<int,vector<int>, greater<int> > pq_min;
    vector<int> list1;
    vector<int> list2;
    for (int i=1; i<=n; i++)
    {
        if (indegree_1[i] == 0)
        {
            pq_max.push(i);
            pq_min.push(i);
        }
    }
    while (!pq_max.empty())
    {
        int v = pq_max.top();
        pq_max.pop();
        list1.push_back(v);
        for (int i = 0; i < adj[v].size(); i++)
        {
            int next = adj[v][i];
            indegree_1[next]--;
            if (indegree_1[next] == 0)
                pq_max.push(next);
        }
    }
    while (!pq_min.empty())
    {
        int v = pq_min.top();
        pq_min.pop();
        list2.push_back(v);
        for (int i = 0; i < adj[v].size(); i++)
        {
            int next = adj[v][i];
            indegree_2[next]--;
            if (indegree_2[next] == 0)
                pq_min.push(next);
        }
    }
    if (list1.size() != n)
    {
        cout << 0;
    }
    else if (list1 == list2)
    {
        cout << 1 << endl;
        for (auto x : list1)
            cout << x << " ";
    }
    else
    {
        cout << 2 << endl;
        for (auto x : list1)
            cout << x << " ";
        cout << endl;
        for (auto x : list2)
            cout << x << " ";
    }
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms316 KiB
2Elfogadva0/01ms316 KiB
3Elfogadva0/0138ms8240 KiB
4Elfogadva2/21ms316 KiB
5Elfogadva3/31ms316 KiB
6Elfogadva3/31ms316 KiB
7Elfogadva3/31ms352 KiB
8Elfogadva1/11ms316 KiB
9Elfogadva3/31ms508 KiB
10Elfogadva3/32ms492 KiB
11Elfogadva3/32ms316 KiB
12Elfogadva1/12ms316 KiB
13Elfogadva2/22ms316 KiB
14Elfogadva3/32ms316 KiB
15Elfogadva1/174ms4852 KiB
16Elfogadva3/3112ms11068 KiB
17Elfogadva5/590ms12456 KiB
18Elfogadva1/1172ms15544 KiB
19Elfogadva2/276ms5072 KiB
20Elfogadva3/3150ms12912 KiB
21Elfogadva4/4130ms12840 KiB
22Elfogadva4/4136ms12844 KiB