43742023-03-27 11:07:011478Testnevelés óracpp17Accepted 50/50225ms24992 KiB
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ///koszi peti1234
    //ifstream cin("in.txt");
    //ofstream cout("out.txt");
    int n, k;
    cin >> n >> k;
    vector<vector<int>> sz(n + 1);
    vector<int> be(n + 1);
    for(int i = 1; i <= k; i++){
        int a, b;
        cin >> a >> b;
        sz[a].push_back(b);
        be[b]++;
    }

    queue<int> s;
    for(int i = 1; i <= n; i++){
        if(be[i] == 0){
            s.push(i);
        }
    }

    vector<int> ans;
    while(!s.empty()){
        int f = s.front();
        //cout<<f<<" ";
        ans.push_back(f);
        s.pop();
        for(int x:sz[f]){
            be[x]--;
            if(!be[x]){
                s.push(x);
            }
        }
    }

   /* for(int i = 0; i < n; i++){
            cout << ans[i] << " ";
    }*/


    if(ans.size() != n){
        cout << 0;
        return 0;
    }

    int hol = 0;
    for(int i = 0; i < n - 1; i++){
        int a = ans[i];
        int b = ans[i + 1];
        bool csere = 1;
        for(int x : sz[a]){
            if(b == x){
                csere = 0;
            }
        }
        if(csere){
            hol = i;
        }
    }

    if(hol == 0){
        cout << 1 << '\n';
        for(int i = 0; i < n; i++){
            cout << ans[i] << " ";
        }
    }
    else{
        cout << 2 << '\n';
        for(int i = 0; i < n; i++){
            cout << ans[i] << " ";
        }
        cout << '\n';
        swap(ans[hol], ans[hol + 1]);
        for(int i = 0; i < n; i++){
            cout << ans[i] << " ";
        }
    }



    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1840 KiB
2Accepted0/02ms1984 KiB
3Accepted0/0181ms13980 KiB
4Accepted2/23ms2524 KiB
5Accepted3/33ms2768 KiB
6Accepted3/33ms2980 KiB
7Accepted3/33ms3044 KiB
8Accepted1/12ms3156 KiB
9Accepted3/33ms3364 KiB
10Accepted3/34ms3388 KiB
11Accepted3/34ms3656 KiB
12Accepted1/14ms3756 KiB
13Accepted2/24ms3864 KiB
14Accepted3/33ms3952 KiB
15Accepted1/1155ms11416 KiB
16Accepted3/3138ms19932 KiB
17Accepted5/559ms18464 KiB
18Accepted1/1225ms24992 KiB
19Accepted2/2153ms11940 KiB
20Accepted3/3185ms23612 KiB
21Accepted4/4175ms23616 KiB
22Accepted4/4177ms23616 KiB