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 int long long
# define fir first
# define sec second
# define pb push_back
const int cnst = 2e5+5;
bool mutipletestcase = 0;
//bool debug = false;
int x[cnst], y[cnst];
int dis[cnst];
vector<int> vec[cnst], vec2[cnst];
// void dfs(int x) {
// for(auto v: vec[x]) {
// if(dis[v] != 0) continue;
// dis[v] = dis[x]+1;
// dfs(v);
// }
// }
void solve() {
int n, m; cin >> n >> m;
for(int i = 1; i<=m; i++) {
int a, b; cin >> a >> b;
vec[a].pb(b);
vec2[b].pb(a);
x[a]++; y[b]++;
}
// bool yes = 0;
// for(int i = 1; i<=n; i++) if(!y[i]) yes = 1;
// if(!yes) {cout << "-1" << endl; return;}
queue<int> q;
int done = 0;
for(int i = 1; i<=n; i++) {
if(!x[i]) q.push(i), done++;
}
vector<pair<int, int>> ans;
while(!q.empty()) {
int a = q.front(); q.pop();
for(auto v: vec2[a]) {
x[v]--;
if(!x[v]) {
q.push(v);
ans.pb({v, 1});
done++;
}
}
// cerr << a << " " << done << endl;
if(done != n) ans.pb({a, 0});
else break;
}
if(done == n) {
cout << ans.size() << endl;
for(auto v: ans) cout << v.fir << " " << v.sec << endl;
}
else cout << -1 << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
int t = 1;
if(mutipletestcase) cin >> t;
while(t--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |