180272025-09-25 16:43:01algoproTestnevelés óracpp17Accepted 50/50144ms15052 KiB
// UUID: b0dab646-1a68-4bf5-8cb3-5cbb7f15b564
#include <bits/stdc++.h>
using namespace std;
const int modulo = 1000000007;
using ll = long long;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k; cin >> n >> k;
    vector <int> be(n+1), vis(n+1), kezd, folyt1, folyt2;
    vector <vector <int>> g(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;
    while(!q.empty()){
        if(q.size() > 1){
            s1 = q.front();
            break;
        }
        int u = q.front();
        q.pop();
        vis[u] = 1;
        for(int v : g[u]){
            be[v]--;
            if(be[v] == 0) q.push(v);
        }
        kezd.push_back(u);
    }

    if(s1 != -1){
        while(!q.empty()){
            int u = q.front();
            q.pop();
            for(int v : g[u]){
                if(!vis[v]){
                    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;
    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;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/01ms508 KiB
3Accepted0/0101ms8180 KiB
4Accepted2/21ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms316 KiB
7Accepted3/31ms316 KiB
8Accepted1/11ms556 KiB
9Accepted3/31ms508 KiB
10Accepted3/32ms316 KiB
11Accepted3/32ms316 KiB
12Accepted1/12ms508 KiB
13Accepted2/22ms316 KiB
14Accepted3/31ms352 KiB
15Accepted1/175ms4492 KiB
16Accepted3/385ms10904 KiB
17Accepted5/545ms11680 KiB
18Accepted1/1136ms15052 KiB
19Accepted2/275ms4784 KiB
20Accepted3/3144ms12428 KiB
21Accepted4/4122ms13108 KiB
22Accepted4/4123ms12972 KiB