This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |