134062025-01-07 20:40:46BucsMateTestnevelés óracpp17Elfogadva 50/50289ms25716 KiB
#include <iostream>
#include <vector>
#include <stack>
#include <queue>

using namespace std;

int bejovo[200001] = {};
vector<int> megoldas1, megoldas2;
vector<vector<int>> adj;
int visited[200001] = {};
int returned[200001] = {};
bool valid = true;

void DFS_validity(int node)
{
    if(!valid){
        return;
    }
    if(visited[node] && !returned[node]){
        valid = false;
        return;
    }
    if(returned[node]){
        return;
    }
    visited[node] = true;
    for(int i = 0; i < adj[node].size(); i++){
        DFS_validity(adj[node][i]);
    }

    returned[node] = true;
    return;
}

void DFS1(int node)
{
    if(returned[node]){
        return;
    }
    for(int i = 0; i < adj[node].size(); i++){
        DFS1(adj[node][i]);
    }

    returned[node] = true;
    megoldas1.push_back(node);
}

void DFS2(int node)
{
    if(returned[node]){
        return;
    }
    for(int i = adj[node].size()-1; i >= 0; i--){
        DFS2(adj[node][i]);
    }

    returned[node] = true;
    megoldas2.push_back(node);
}

int main()
{
    int N, K;
    cin >> N >> K;
    adj.resize(N+1);


    for(int i = 0; i < K; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
    }

    for(int i = 1; i <= N; i++){
        if(!returned[i]){
            DFS_validity(i);
        }
    }

    if(!valid){
        cout << 0 << endl;
        return 0;
    }

    for(int i = 1; i <= N; i++){
        returned[i] = false;
    }
    for(int i = 1; i <= N; i++){
        if(!returned[i]){
            DFS1(i);
        }
    }
    for(int i = 1; i <= N; i++){
        returned[i] = false;
    }
    for(int i = N; i >= 1; i--){
        if(!returned[i]){
            DFS2(i);
        }
    }

    bool van_tobb_megoldas = false;
    for(int i = 0; i < N; i++){
        if(megoldas1[i] != megoldas2[i]){
            van_tobb_megoldas = true;
        }
    }

    if(van_tobb_megoldas){
        cout << 2 << endl;
        for(int i = N-1; i >= 0; i--){
            cout << megoldas1[i] << " ";
        }
        cout << endl;
        for(int i = N-1; i >= 0; i--){
            cout << megoldas2[i] << " ";
        }
    }
    else{
        cout << 1 << endl;
        for(int i = N-1; i >= 0; i--){
            cout << megoldas1[i] << " ";
        }
    }
    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms328 KiB
2Elfogadva0/01ms316 KiB
3Elfogadva0/0226ms10284 KiB
4Elfogadva2/21ms316 KiB
5Elfogadva3/31ms316 KiB
6Elfogadva3/31ms500 KiB
7Elfogadva3/31ms316 KiB
8Elfogadva1/11ms316 KiB
9Elfogadva3/31ms316 KiB
10Elfogadva3/33ms316 KiB
11Elfogadva3/33ms316 KiB
12Elfogadva1/13ms316 KiB
13Elfogadva2/23ms316 KiB
14Elfogadva3/32ms316 KiB
15Elfogadva1/1182ms7224 KiB
16Elfogadva3/3167ms12548 KiB
17Elfogadva5/564ms10760 KiB
18Elfogadva1/1240ms17192 KiB
19Elfogadva2/2180ms7224 KiB
20Elfogadva3/3289ms20520 KiB
21Elfogadva4/4252ms25716 KiB
22Elfogadva4/4246ms22824 KiB