#include <bits/stdc++.h>
using namespace std;
int N, M;
int B[200005], T[200005];
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> N >> M;
for(int l = 0; l < M; l++)
cin >> B[l] >> T[l];
vector<pair<int, int>> res;
int calc = 0;
do {
bool done = true;
calc++;
vector<int> emp;
vector<vector<int>> tp(N + 1), bt(N + 1);
vector<array<int, 2>> c;
for(int l = 0; l < M; l++) {
done &= ((B[l] == 0) || ((T[l] > 0) && (B[l] == T[l])));
if(B[l] == 0) emp.push_back(l);
if(T[l] > 0) tp[T[l]].push_back(l);
if(T[l] == 0 && B[l] > 0)
bt[B[l]].push_back(l);
if(tp[T[l]].size() == 2)
c.push_back({tp[T[l]][0], tp[T[l]][1]});
}
if(done) break;
bool op = false;
while(true) {
bool ff = false, ss = false;
for(int l = 1; l <= N; l++) {
if(bt[l].size() < 2) continue;
res.push_back({bt[l][0], bt[l][1]});
swap(B[bt[l][0]], T[bt[l][1]]);
ff = true;
break;
}
op |= ff;
if(ff) break;
for(int l = 1; l <= N; l++) {
if(bt[l].size() != 1 || tp[l].size() != 1)
continue;
if(bt[l] == tp[l]) continue;
res.push_back({tp[l][0], bt[l][0]});
swap(T[tp[l][0]], T[bt[l][0]]);
ff = true; break;
}
op |= ff;
if(ff) break;
if(emp.size() && c.size()) {
res.push_back({c.back()[0], emp.back()});
res.push_back({c.back()[1], emp.back()});
swap(B[emp.back()], T[c.back()[0]]);
swap(T[emp.back()], T[c.back()[1]]);
ss = true;
}
op |= ss; break;
}
if(!op) {
cout << -1; return 0;
}
} while(calc < 10);
cout << res.size() << "\n";
for(auto u : res)
cout << ++u.first << " " << ++u.second << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
168 ms |
10216 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |