#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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9720 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9720 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9720 KB |
Output is correct |
7 |
Correct |
5 ms |
9736 KB |
Output is correct |
8 |
Incorrect |
5 ms |
9648 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
480 ms |
31764 KB |
Integer parameter [name=k] equals to 333174, violates the range [-1, 200000] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9720 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9720 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9720 KB |
Output is correct |
7 |
Correct |
5 ms |
9736 KB |
Output is correct |
8 |
Incorrect |
5 ms |
9648 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |