150812025-02-12 13:05:26csdavidTestnevelés óracpp17Accepted 50/50237ms16252 KiB
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct gyerek{
    int nagyobb=0;
    vector<int> kisebb;
};

gyerek a[200000];
int n, x, y, k, s[200000];
bool csere=0;
pair<int, int> cs;
queue<int> q;
int main()
{
    cin >> n >> k;
    for(int i=0; i<k; i++){
        cin >> x >> y;
        x--;
        y--;
        a[y].kisebb.push_back(x);
        a[x].nagyobb++;
    }
    for(int i=0; i<n; i++){
        if(a[i].nagyobb==0){
            q.push(i);
        }
    }
    for(int i=0; i<n; i++){
        if(q.empty()){
            cout << 0;
            return 0;
        }
        s[i]=q.front();
        q.pop();
        if(!q.empty()){
            csere=1;
            cs.first=i;
            cs.second=i+1;
        }
        for(auto& it:a[s[i]].kisebb){
            a[it].nagyobb--;
            if(a[it].nagyobb==0){
                q.push(it);
            }
        }
    }
    if(!csere){
        cout << 1 << '\n';
        for(int i=n-1; i>=0; i--){
            cout << s[i]+1 << ' ';
        }
    }
    else{
        cout<< 2 << '\n';
        for(int i=n-1; i>=0; i--){
            cout << s[i]+1 << ' ';
        }
        swap(s[cs.first], s[cs.second]);
        cout << '\n';
        for(int i=n-1; i>=0; i--){
            cout << s[i]+1 << ' ';
        }
    }


    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/06ms6452 KiB
2Accepted0/07ms6452 KiB
3Accepted0/0196ms13112 KiB
4Accepted2/26ms6452 KiB
5Accepted3/37ms6452 KiB
6Accepted3/36ms6460 KiB
7Accepted3/37ms6680 KiB
8Accepted1/17ms6452 KiB
9Accepted3/36ms6452 KiB
10Accepted3/38ms6708 KiB
11Accepted3/38ms6708 KiB
12Accepted1/18ms6524 KiB
13Accepted2/27ms6708 KiB
14Accepted3/38ms6692 KiB
15Accepted1/1164ms11436 KiB
16Accepted3/3151ms12532 KiB
17Accepted5/546ms10840 KiB
18Accepted1/1237ms16252 KiB
19Accepted2/2162ms11576 KiB
20Accepted3/3181ms15672 KiB
21Accepted4/4204ms15668 KiB
22Accepted4/4178ms15544 KiB