This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//記得跳題
//#pragma GCC optimize("O4,unroll_loops")
//#pragma GCC target("avx2")
#include<iostream>
#include<array>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<cstring>
#include<iomanip>
#include<bitset>
#include<tuple>
#include<random>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define pb push_back
#define INF (ll)(2e18)
#define F first
#define S second
#define endl "\n"
#define AC ios::sync_with_stdio(0);
using namespace std;
int n, m;
int in[200005];
vector<int> e[200005];
vector<pair<int, int> > ans;
signed main(){
AC;
cin >> n >> m;
FOR (i, 0, m) {
int a, b; cin >> a >> b;
in[b]++;
e[a].pb(b);
}
queue<int> Q;
FOR (i, 1, n+1) if (!in[i]) {
Q.push(i);
}
int cnt = 0;
while (Q.size()) {
int now = Q.front(); Q.pop();
ans.pb({now, 0});
cnt++;
for (auto i:e[now]) {
in[i]--;
if (!in[i]) {
Q.push(i);
}
}
}
if (cnt != n) {
cout << -1 << endl;
} else {
cout << ans.size() << endl;
for (auto i:ans) {
cout << i.F << " " << i.S << 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... |