180442025-09-25 17:59:30algoproTestnevelés óracpp17Wrong answer 19/5063ms64000 KiB
// UUID: 18add162-4547-49c3-8d23-affd170e6e5c
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;


int main() {

	int n, m; cin >> n >> m;
    vector<vector<int>> g(n+1, vector<int>(n+1, 0));
    vector<int> fokszam(n+1, 0);
    vector<int> ans;
    vector<bool> vis(n+1, 0);
    for (int i=0;i<m;i++){
        int a, b; cin >> a >> b;
        g[a].push_back(b);
        fokszam[b]++;
    }
    pair<int, int> felcserel = {0, 0};
    queue<int> q;
    for (int i=0;i<n;i++) {
        if (!fokszam[i]) {
            q.push(i);
            if (felcserel.first) {
                if (!felcserel.second) felcserel.second = i;
            } else felcserel.first = i;
        }
        } 
        // cout << fokszam[i];
    if (!felcserel.second) felcserel = {0, 0};
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        ans.push_back(u);
        vis[u] = 1;
        for (int i: g[u]) {
            fokszam[i]--;
            if (!fokszam[i]){
                q.push(i);
                if (felcserel.first) {
                    if (!felcserel.second) felcserel.second = i;
                } else felcserel.first = i;
            } 
        }
        if (!felcserel.second) felcserel = {0, 0};
        // cout << u << '\n';
    }
    // for(int i: fokszam) {
    //     cout << i << '\n';
    // }
    for (int i: vis) if (!i) {
            cout << 0;
            return 0;
        }
    
    cout << (felcserel.second ? 2: 1) << endl;
    for(int i: ans) {
        if (i) cout << i <<' ';
    }
    cout <<'\n';
    // cout << felcserel.first << ' ' << felcserel.second;

    // cout <<'\n';

    for(int i: ans) {
        if (i == felcserel.first) cout << felcserel.second << ' ';
        else if (i == felcserel.second) cout << felcserel.first << ' ';
        else if(i) cout << i <<' ';
    }
}
SubtaskSumTestVerdictTimeMemory
base19/50
1Wrong answer0/01ms316 KiB
2Accepted0/01ms316 KiB
3Runtime error0/052ms64000 KiB
4Accepted2/21ms316 KiB
5Wrong answer0/31ms316 KiB
6Accepted3/31ms316 KiB
7Wrong answer0/31ms316 KiB
8Accepted1/11ms316 KiB
9Accepted3/31ms316 KiB
10Accepted3/38ms4660 KiB
11Accepted3/313ms8156 KiB
12Accepted1/114ms8680 KiB
13Wrong answer0/212ms8668 KiB
14Accepted3/313ms8756 KiB
15Runtime error0/163ms64000 KiB
16Runtime error0/363ms64000 KiB
17Runtime error0/550ms64000 KiB
18Runtime error0/161ms64000 KiB
19Runtime error0/263ms64000 KiB
20Runtime error0/350ms64000 KiB
21Runtime error0/461ms64000 KiB
22Runtime error0/459ms64000 KiB