179212025-09-23 16:08:10zhuyiTestnevelés óracpp17Elfogadva 50/50128ms15044 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k; cin >> n >> k;
    vector <vector <int>> g(n+1);
    vector <int> be(n+1), kezd, folyt1, folyt2, vis(n+1);
    for(int i = 0; i < k; i++){
        int a, m; cin >> a >> m;
        g[a].push_back(m);
        be[m]++;
    }
    queue <int> q;
    for(int i = 1; i <= n; i++){
        if(be[i] == 0) q.push(i);
    }
    int s1 = -1, s2 = -1;
    while(!q.empty()){
        if(q.size() > 1){
            s1 = q.front();
            q.pop();
            s2 = q.front();
            break;
        }
        int u = q.front(); q.pop();
        vis[u] = true;
        for(int v : g[u]){
            be[v]--;
            if(be[v] == 0){
                q.push(v);
            }
        }
        kezd.push_back(u);
    }
    while(!q.empty()) q.pop();

    if(s1 != -1){
        for(int i = 1; i <= n; i++){
            if(be[i] == 0 && !vis[i]) q.push(i);
        }
        while(!q.empty()){
            int u = q.front(); q.pop();
            for(int v : g[u]){
                be[v]--;
                if(be[v] == 0) q.push(v);
            }
            folyt1.push_back(u);
            folyt2.push_back(u);
        }
        swap(folyt1[0], folyt1[1]);
    }

    if(kezd.size() + folyt1.size() < n){
        cout << 0;
        return 0;
    }
    else if(kezd.size() == n){
        cout << 1 << "\n";
        for(int it : kezd) cout << it << " ";
    }
    else{
        cout << 2 << "\n";
        for(int it : kezd) cout << it << " ";
        for(int it : folyt1) cout << it << " ";
        cout << "\n";
        for(int it : kezd) cout << it << " ";
        for(int it : folyt2) cout << it << " ";
    }
    return 0;
}









RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms316 KiB
2Elfogadva0/01ms316 KiB
3Elfogadva0/094ms8096 KiB
4Elfogadva2/21ms316 KiB
5Elfogadva3/31ms316 KiB
6Elfogadva3/31ms316 KiB
7Elfogadva3/31ms316 KiB
8Elfogadva1/11ms316 KiB
9Elfogadva3/31ms348 KiB
10Elfogadva3/32ms316 KiB
11Elfogadva3/32ms464 KiB
12Elfogadva1/12ms388 KiB
13Elfogadva2/22ms316 KiB
14Elfogadva3/31ms316 KiB
15Elfogadva1/168ms4536 KiB
16Elfogadva3/383ms10732 KiB
17Elfogadva5/546ms11620 KiB
18Elfogadva1/1128ms15044 KiB
19Elfogadva2/271ms4784 KiB
20Elfogadva3/3107ms12332 KiB
21Elfogadva4/4112ms12972 KiB
22Elfogadva4/4118ms12972 KiB