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 lim = 200005;
int degree[lim], n, m, a, b, totdeg;
vector<int> adjl[lim];
vector<int> seq;
bool vis[lim];
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a >> b;
adjl[b].push_back(a);
degree[a]++;
totdeg++;
}
queue<int> q;
for (int i = 1; i <= n; i++) {
// cout << i << " " << degree[i] << endl;
if (degree[i] == 0) {
q.push(i);
seq.push_back(i);
}
}
// cout << totdeg << endl;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (auto i: adjl[cur]) {
degree[i]--;
totdeg--;
if (!vis[i]) {
if (degree[i] == 0) {
q.push(i);
seq.push_back(i);
}
}
}
}
// cout << totdeg << endl;
if (totdeg > 0) cout << -1 << endl;
else {
cout << seq.size() << endl;
for (auto i: seq) {
cout << i << " " << 1 << endl;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |