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;
typedef long long ll;
const int MX = 2e5 + 5;
int N, M;
int deg[MX];
vector<int> g[MX];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> N >> M;
for(int i = 0; i < M; i++) {
int u, v;
cin >> u >> v;
g[v].push_back(u);
deg[u]++;
}
queue<int> q;
vector<int> ord;
for(int i = 1; i <= N; i++) {
if(!deg[i]) {
q.push(i);
}
}
int cnt = 0;
while(!q.empty()) {
auto v = q.front(); q.pop();
cnt++;
for(auto u : g[v]) {
deg[u]--;
if(!deg[u]) {
q.push(u);
ord.push_back(u);
}
}
}
if(cnt < N) {
cout << -1 << '\n';
return 0;
}
cout << ord.size() << '\n';
for(auto x : ord) {
cout << x << " " << 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... |