48392023-03-31 17:01:10UnluckYTestnevelés óracpp11Hibás válasz 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base40/50
1Elfogadva0/03ms1812 KiB
2Elfogadva0/03ms2056 KiB
3Elfogadva0/0194ms14736 KiB
4Elfogadva2/23ms2208 KiB
5Elfogadva3/33ms2340 KiB
6Hibás válasz0/33ms2580 KiB
7Elfogadva3/33ms2796 KiB
8Elfogadva1/12ms2804 KiB
9Elfogadva3/33ms2728 KiB
10Elfogadva3/34ms2900 KiB
11Elfogadva3/34ms3164 KiB
12Elfogadva1/14ms3368 KiB
13Elfogadva2/24ms3484 KiB
14Elfogadva3/33ms3576 KiB
15Elfogadva1/1167ms11448 KiB
16Elfogadva3/3160ms20868 KiB
17Elfogadva5/561ms18836 KiB
18Elfogadva1/1240ms26464 KiB
19Elfogadva2/2165ms12356 KiB
20Hibás válasz0/3195ms32588 KiB
21Hibás válasz0/4219ms38664 KiB
22Elfogadva4/4287ms35552 KiB