48392023-03-31 17:01:10UnluckYTestnevelés óracpp11Wrong answer 40/50287ms38664 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]){
        dfs3(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++){
        dfs1(i);
    }

    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
base40/50
1Accepted0/03ms1812 KiB
2Accepted0/03ms2056 KiB
3Accepted0/0194ms14736 KiB
4Accepted2/23ms2208 KiB
5Accepted3/33ms2340 KiB
6Wrong answer0/33ms2580 KiB
7Accepted3/33ms2796 KiB
8Accepted1/12ms2804 KiB
9Accepted3/33ms2728 KiB
10Accepted3/34ms2900 KiB
11Accepted3/34ms3164 KiB
12Accepted1/14ms3368 KiB
13Accepted2/24ms3484 KiB
14Accepted3/33ms3576 KiB
15Accepted1/1167ms11448 KiB
16Accepted3/3160ms20868 KiB
17Accepted5/561ms18836 KiB
18Accepted1/1240ms26464 KiB
19Accepted2/2165ms12356 KiB
20Wrong answer0/3195ms32588 KiB
21Wrong answer0/4219ms38664 KiB
22Accepted4/4287ms35552 KiB