179102025-09-22 18:18:42algoproTestnevelés óracpp17Accepted 50/50126ms13352 KiB
// UUID: ebfa39d1-1e41-4cf7-889d-90d09e84cd69
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    cin >> N >> K;

    vector<vector<int>> g(N + 1);
    vector<int> indeg(N + 1, 0);

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

    // topologikus rendezés (Kahn algoritmus)
    queue<int> q;
    for (int i = 1; i <= N; i++) {
        if (indeg[i] == 0) q.push(i);
    }

    vector<int> order;
    bool multiple = false;
    int multi_where = -1;

    while (!q.empty()) {
        if (q.size() > 1) {
            multiple = true;
            multi_where = order.size();
        }
        int u = q.front();
        q.pop();
        order.push_back(u);
        for (int v : g[u]) {
            indeg[v]--;
            if (indeg[v] == 0) q.push(v);
        }
    }

    if ((int)order.size() != N) {
        cout << 0 << "\n";
        return 0;
    }

    if (!multiple) {
        cout << 1 << "\n";
        for (int i = 0; i < N; i++) {
            cout << order[i] << (i + 1 == N ? '\n' : ' ');
        }
    } else {
        cout << 2 << "\n";
       
        for (int i = 0; i < N; i++) {
            cout << order[i] << (i + 1 == N ? '\n' : ' ');
        }
        swap(order[multi_where], order[multi_where + 1]);
        for (int i = 0; i < N; i++) {
            cout << order[i] << (i + 1 == N ? '\n' : ' ');
        }


        //         vector<int> indeg2(N + 1, 0);
        // for (int i = 1; i <= N; i++)
        //     for (int v : g[i]) indeg2[v]++;
        // priority_queue<int, vector<int>, greater<int>> pq; 
        // for (int i = 1; i <= N; i++)
        //     if (indeg2[i] == 0) pq.push(i);

        // vector<int> order2;
        // bool skipped = false;
        // while (!pq.empty()) {
        //     if (pq.size() > 1 && !skipped) {
                
        //         int first = pq.top(); pq.pop();
        //         int second = pq.top(); pq.pop();
        //         order2.push_back(second);
        //         for (int v : g[second]) {
        //             indeg2[v]--;
        //             if (indeg2[v] == 0) pq.push(v);
        //         }
        //         pq.push(first);
        //         skipped = true;
        //     } else {
        //         int u = pq.top(); pq.pop();
        //         order2.push_back(u);
        //         for (int v : g[u]) {
        //             indeg2[v]--;
        //             if (indeg2[v] == 0) pq.push(v);
        //         }
        //     }
        // }
        // for (int i = 0; i < N; i++) {
        //     cout << order2[i] << (i + 1 == N ? '\n' : ' ');
        // }
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/01ms316 KiB
3Accepted0/092ms7340 KiB
4Accepted2/21ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms316 KiB
7Accepted3/31ms508 KiB
8Accepted1/11ms316 KiB
9Accepted3/31ms316 KiB
10Accepted3/32ms316 KiB
11Accepted3/32ms316 KiB
12Accepted1/12ms316 KiB
13Accepted2/22ms480 KiB
14Accepted3/31ms316 KiB
15Accepted1/171ms4276 KiB
16Accepted3/382ms9900 KiB
17Accepted5/539ms9996 KiB
18Accepted1/1126ms13352 KiB
19Accepted2/270ms4528 KiB
20Accepted3/3101ms11688 KiB
21Accepted4/4122ms11688 KiB
22Accepted4/497ms11684 KiB