48462023-03-31 22:43:56UnluckYTestnevelés óracpp11Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1964 KiB
2Elfogadva0/03ms2100 KiB
3Elfogadva0/0193ms14812 KiB
4Elfogadva2/23ms2636 KiB
5Elfogadva3/33ms2812 KiB
6Elfogadva3/33ms2976 KiB
7Elfogadva3/32ms2916 KiB
8Elfogadva1/13ms2948 KiB
9Elfogadva3/33ms3052 KiB
10Elfogadva3/34ms3308 KiB
11Elfogadva3/34ms3532 KiB
12Elfogadva1/14ms3728 KiB
13Elfogadva2/24ms3708 KiB
14Elfogadva3/33ms3700 KiB
15Elfogadva1/1167ms11776 KiB
16Elfogadva3/3158ms21168 KiB
17Elfogadva5/561ms19332 KiB
18Elfogadva1/1231ms26532 KiB
19Elfogadva2/2165ms12548 KiB
20Elfogadva3/3216ms33384 KiB
21Elfogadva4/4238ms38876 KiB
22Elfogadva4/4246ms38860 KiB