20462022-12-15 21:36:36TomaSajtTestnevelés óracpp17Accepted 50/50116ms27856 KiB
#include <bits/stdc++.h>
#define speed ios::sync_with_stdio(0);cin.tie(0)
using namespace std;

int main() {
    speed;
    int n, k;
    cin >> n >> k;
    vector<vector<int>> g(n + 1);
    vector<int> deps(n + 1);
    while (k--) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        deps[v]++;
    }
    queue<int> q;
    for (int i = 1; i <= n; i++) {
        if (deps[i] == 0) g[0].push_back(i), deps[i]++;
    }
    q.push(0);
    int a = -1, b = -1;
    vector<int> ord;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        if (u != 0) ord.push_back(u);
        vector<int> nexts;
        for (auto v : g[u]) {
            if (--deps[v] == 0) nexts.push_back(v);
        }
        if (a == -1 && nexts.size() > 1) a = nexts[0], b = nexts[1];
        for (auto v : nexts) q.push(v);

    }
    if (count(deps.begin() + 1, deps.end(), 0) != n) {
        cout << '0';
        return 0;
    }
    cout << (b == -1 ? 1 : 2) << '\n';
    for (auto e : ord) cout << e << ' ';
    cout << '\n';
    if (b != -1) {
        for (auto e : ord) cout << (e == a ? b : e == b ? a : e) << ' ';
        cout << '\n';
    }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1828 KiB
2Accepted0/02ms2056 KiB
3Accepted0/086ms14192 KiB
4Accepted2/22ms2324 KiB
5Accepted3/32ms2552 KiB
6Accepted3/32ms2632 KiB
7Accepted3/32ms2904 KiB
8Accepted1/12ms3072 KiB
9Accepted3/32ms3276 KiB
10Accepted3/33ms3584 KiB
11Accepted3/33ms3800 KiB
12Accepted1/13ms3976 KiB
13Accepted2/23ms3928 KiB
14Accepted3/33ms4200 KiB
15Accepted1/168ms11884 KiB
16Accepted3/381ms20732 KiB
17Accepted5/548ms22000 KiB
18Accepted1/1116ms27856 KiB
19Accepted2/268ms12316 KiB
20Accepted3/3108ms24032 KiB
21Accepted4/4107ms24036 KiB
22Accepted4/4107ms24100 KiB