178972025-09-22 17:58:08algoproTestnevelés óracpp17Runtime error 24/5065ms64000 KiB
// UUID: e01bb8f3-ce7d-4592-bdf6-abd00feea34a
#include <bits/stdc++.h>
using namespace std;

int n, k;
vector<int> topology;
vector<bool> vis;   
vector<vector<int>> adj;
vector<vector<bool>> is_edge;
vector<int> in_nodes;
vector<bool> active;

void dfs(int v){
    vis[v] = true;
    //cout << "belép: " << v << '\n';
    for(auto x : adj[v]){
        if(!vis[x]){
            dfs(x);
        }else if(active[x]){
            cout << 0;
            exit(0);
        }else{
        }
    }
    //cout << "elhagy: " << v << '\n';
    active[v] = false;
    topology.push_back(v);
}

int main() {
	cin >> n >> k;
    vis.resize(n+1, false);
    active.resize(n+1, true);
    in_nodes.resize(n+1, 0);
    adj.resize(n+1, vector<int>());
    is_edge.resize(n+1, vector<bool>(n+1, false));
    for(int i=1;i<=k;i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        is_edge[a][b] = true;
        is_edge[b][a] = true;
        in_nodes[b]++;
    }
    for(int i=1;i<=n;i++){
        if(!in_nodes[i]){
            dfs(i);
        }
    }
    reverse(topology.begin(), topology.end());

    int ans = 1;
    for(int i=0;i<topology.size()-1;i++){
        if(!is_edge[topology[i+1]][topology[i]]) ans = 2;
    }
    cout << ans << '\n';
    for(auto x : topology) cout << x << ' ';
    if(ans == 2){
        cout << '\n';
        for(int i=0;i<topology.size()-1;i++){
            if(!is_edge[topology[i+1]][topology[i]]){
                cout << topology[i+1] << ' ' << topology[i] << ' ';
                i++;
            }else{
                cout << topology[i] << ' ';
            }
        }
        cout << topology[topology.size()-1];
    }

}
SubtaskSumTestVerdictTimeMemory
base24/50
1Accepted0/01ms316 KiB
2Accepted0/01ms316 KiB
3Runtime error0/052ms64000 KiB
4Accepted2/21ms316 KiB
5Accepted3/31ms404 KiB
6Accepted3/31ms316 KiB
7Accepted3/31ms316 KiB
8Accepted1/11ms356 KiB
9Accepted3/31ms508 KiB
10Accepted3/33ms568 KiB
11Wrong answer0/33ms556 KiB
12Accepted1/13ms564 KiB
13Accepted2/23ms608 KiB
14Accepted3/32ms564 KiB
15Runtime error0/161ms64000 KiB
16Runtime error0/364ms64000 KiB
17Runtime error0/564ms64000 KiB
18Runtime error0/152ms64000 KiB
19Runtime error0/261ms64000 KiB
20Runtime error0/354ms64000 KiB
21Runtime error0/454ms64000 KiB
22Runtime error0/465ms64000 KiB