Submission #781666

#TimeUsernameProblemLanguageResultExecution timeMemory
781666kebineNaboj (COCI22_naboj)C++17
110 / 110
155 ms13924 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second

queue<int> q;
vector<int> child[200005], ans;
int par[200005];

int main() { 
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    /*
    1 2
    2 3
    3 4
    urutan penarikan = 4 -> 3 -> 2 -> 1
    urutan pengeboman = 1 -> 2 -> 3 -> 4
    3 3
    1 2
    2 3
    1 3
    3 -> 2 -> 1
    3 -> 1
    */
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        child[u].push_back(v);
        par[v]++;
    }
    for (int i = 1; i <= n; i++) if (par[i] == 0) q.push(i);
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        ans.push_back(x);
        for (auto u : child[x]) {
            par[u]--;
            if (par[u] == 0) q.push(u);
        }
    }
    for (int i = 1; i <= n; i++) {
        if (par[i] != 0) {
            cout << -1 << "\n";
            return 0;
        }
    }
    cout << (int)ans.size() << "\n";
    for (auto x : ans) cout << x << " 0" << "\n";
} 
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...