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;
const int MAXN = 2e5 + 25;
vector <int> adj[MAXN];
int deg[MAXN], n, m;
int main () {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
deg[b]++;
}
queue <int> cur;
for (int i = 1; i <= n; i++) {
if (deg[i] == 0) {
cur.push(i);
}
}
int cnt = 0;
vector <int> topo;
while (!cur.empty()) {
int k = cur.front();
cur.pop();
cnt++; topo.push_back(k);
for (auto j : adj[k]) {
deg[j]--;
if (deg[j] == 0) {
cur.push(j);
}
}
}
reverse(topo.begin(), topo.end());
if (cnt != n) {
cout << "-1\n";
} else {
cout << n << '\n';
for (auto i : topo) cout << i << " 1\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... |