20452022-12-15 21:28:43TomaSajtTestnevelés óracpp17Wrong answer 7/50108ms25248 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) q.push(i);
    }
    int a = -1, b = -1;
    vector<int> ord;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        ord.push_back(u);
        a = -1;
        for (auto v : g[u]) {
            deps[v]--;
            if (deps[v] != 0 || b != -1) continue;
            if (a == -1) a = v;
            else b = v;
        }
        for (auto v : g[u]) {
            if (deps[v] == 0) 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) << ' ';
    }
}
SubtaskSumTestVerdictTimeMemory
base7/50
1Accepted0/03ms2100 KiB
2Wrong answer0/02ms2032 KiB
3Wrong answer0/085ms14040 KiB
4Wrong answer0/22ms2424 KiB
5Wrong answer0/32ms2652 KiB
6Wrong answer0/32ms2984 KiB
7Wrong answer0/32ms2844 KiB
8Accepted1/12ms2920 KiB
9Wrong answer0/32ms3044 KiB
10Wrong answer0/33ms3188 KiB
11Wrong answer0/33ms3348 KiB
12Accepted1/13ms3388 KiB
13Accepted2/23ms3568 KiB
14Wrong answer0/32ms3752 KiB
15Accepted1/168ms11456 KiB
16Wrong answer0/363ms19720 KiB
17Wrong answer0/526ms18352 KiB
18Wrong answer0/1108ms25248 KiB
19Accepted2/267ms12136 KiB
20Wrong answer0/393ms23936 KiB
21Wrong answer0/493ms23944 KiB
22Wrong answer0/497ms23944 KiB