#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 2e5+10;
int n, m, b[maxn], t[maxn], in[maxn], done[maxn], out[maxn];
vector<int> pos[maxn], ot[3], g[maxn];
void solve() {
cin >> n >> m;
vector<int> ept;
for(int i = 1; i <= m; i++) {
cin >> b[i] >> t[i];
if(b[i] == 0) {
ept.pb(i);
continue;
}
if(b[i] != 0) pos[b[i]].pb(i);
if(t[i] != 0) pos[t[i]].pb(i+m);
if(b[i] == t[i]) {
done[b[i]] = 1;
}
else if(b[i] != 0 && t[i] != 0) {
g[t[i]].pb(b[i]);
out[t[i]]++;
in[b[i]]++;
}
}
for(int i = 1; i <= n; i++) {
if(done[i] == 0 && in[i] == 0) {
ot[out[i]].pb(i);
}
}
vector<pair<int,int>> ans;
while(true) {
if(ot[0].size()) {
int i = ot[0].back(); ot[0].pop_back(); done[i] = 1;
sort(all(pos[i]));
int p1 = pos[i][0];
int p2 = pos[i][1];
ans.pb(mp(p1,p2));
b[p1] = 0;
t[p2] = i;
ept.pb(p1);
pos[i][0] = p2+m;
}
else if(ot[1].size()) {
int i = ot[1].back(); ot[1].pop_back(); done[i] = 1;
sort(all(pos[i]));
int p1 = pos[i][0];
int p2 = pos[i][1];
ans.pb(mp(p2,p1));
t[p2-m] = 0;
for(auto x : g[i]) {
if(--in[x] == 0) {
ot[out[x]].pb(x);
}
}
t[p1] = i;
pos[i][1] = p1+m;
}
else if(ot[2].size() && ept.size()) {
int i = ot[2].back(); ot[2].pop_back(); done[i] = 1;
int e = ept.back(); ept.pop_back();
sort(all(pos[i]));
int p1 = pos[i][0];
int p2 = pos[i][1];
ans.pb(mp(p1,e));
ans.pb(mp(p2,e));
t[p1-m] = 0;
t[p2-m] = 0;
for(auto x : g[i]) {
if(--in[x] == 0) {
ot[out[x]].pb(x);
}
}
b[e] = i;
t[e] = i;
pos[i][0] = e;
pos[i][1] = e+m;
}
else {
break;
}
}
for(int i = 1; i <= n; i++) {
if(done[i] == 0) {
cout << -1 << endl;
return;
}
}
cout << ans.size() << endl;
for(auto x : ans) {
if(x.fr > m) x.fr-= m;
if(x.sc > m) x.sc-= m;
cout << x.fr << " " << x.sc << endl;
}
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9728 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Incorrect |
5 ms |
9724 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
24004 KB |
Output is correct |
2 |
Correct |
104 ms |
25656 KB |
Output is correct |
3 |
Correct |
67 ms |
20956 KB |
Output is correct |
4 |
Correct |
56 ms |
21320 KB |
Output is correct |
5 |
Correct |
99 ms |
25212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
9812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
9812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9860 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Incorrect |
5 ms |
9732 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9728 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Incorrect |
5 ms |
9724 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |