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>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define eb emplace_back
#define int LL
using namespace std;
using LL = long long;
using pii = pair<int, int>;
const int MAXN = 200020;
vector<int> adj[MAXN];
int deg[MAXN];
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
// nya ><
int n, m; cin >> n >> m;
For(i, 1, m) {
int a, b; cin >> a >> b;
adj[a].eb(b);
deg[b]++;
}
queue<int> que;
vector<int> ans;
For(i, 1, n) if(deg[i] == 0) {
que.emplace(i);
}
while(!que.empty()) {
int now = que.front(); que.pop();
ans.eb(now);
for(auto &i:adj[now]) {
deg[i]--;
if(!deg[i]) que.emplace(i);
}
}
if(sz(ans) != n) {
cout << "-1\n";
} else {
cout << n << "\n";
reverse(all(ans));
for(auto &i:ans) cout << i << " 1\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |