48462023-03-31 22:43:56UnluckYTestnevelés óracpp11Accepted 50/50246ms38876 KiB
#include <bits/stdc++.h>

using namespace std;


vector<vector<int>> v;
vector<int> top, top2, color;
bool circle = false;
vector<bool> seen;


void dfs1(int x){
    if (color[x] == 2){circle = true; return;}
    if (color[x] == 3) return;
    color[x] = 2;

    for (int i : v[x]){
        dfs1(i);
    }

    color[x] = 3;
}

void dfs2(int x){
    if (seen[x]) return;
    seen[x] = true;

    for (int i : v[x]){
        dfs2(i);
    }

    top.push_back(x);

}


void dfs3(int x){
    if (seen[x]) return;
    seen[x] = true;

    for (int i = v[x].size()-1; i >= 0; i--){
        dfs3(v[x][i]);
    }

    top2.push_back(x);

}


int main(){


    int n, k; cin >> n >> k;
    v.assign(n+1, {});
    seen.assign(n+1, false);
    color.assign(n+1, 1);


    for (int i = 0; i < k; i++){

        int a, b; cin >> a >> b;
        v[a].push_back(b);

    }


    for (int i = 1; i <= n; i++){
        if (color[i] == 1) dfs1(i);
        if (circle) break;
    }

    if (circle){
        cout << 0;
        return 0;
    }


    for (int i = 1; i <= n; i++){
        dfs2(i);
    }

    reverse(top.begin(), top.end());
    seen.assign(n+1, false);


    for (int i = n; i >= 1; i--){
        dfs3(i);
    }


    reverse(top2.begin(), top2.end());

    bool one = true;

    for (int i = 0; i < n; i++){
        if (top[i] != top2[i]) one = false;
    }

    if (one){
        cout << 1 << endl;
        for (int i : top){
            cout << i << " ";
        }
        return 0;
    }

    cout << 2 << endl;

    for (int i : top) cout << i << " ";

    cout << endl;

    for (int i : top2) cout << i << " ";


    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1964 KiB
2Accepted0/03ms2100 KiB
3Accepted0/0193ms14812 KiB
4Accepted2/23ms2636 KiB
5Accepted3/33ms2812 KiB
6Accepted3/33ms2976 KiB
7Accepted3/32ms2916 KiB
8Accepted1/13ms2948 KiB
9Accepted3/33ms3052 KiB
10Accepted3/34ms3308 KiB
11Accepted3/34ms3532 KiB
12Accepted1/14ms3728 KiB
13Accepted2/24ms3708 KiB
14Accepted3/33ms3700 KiB
15Accepted1/1167ms11776 KiB
16Accepted3/3158ms21168 KiB
17Accepted5/561ms19332 KiB
18Accepted1/1231ms26532 KiB
19Accepted2/2165ms12548 KiB
20Accepted3/3216ms33384 KiB
21Accepted4/4238ms38876 KiB
22Accepted4/4246ms38860 KiB