#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(B[l] == T[l]) continue;
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;
if(ss) break;
if(emp.size()) {
for(int l = 1; l <= N; l++) {
if(tp[l].size()) {
res.push_back({tp[l].back(), emp.back()});
swap(B[emp.back()], T[tp[l].back()]);
ss = true; break;
}
}
}
op |= ss; break;
}
if(!op) {
cout << -1; return 0;
}
} while(true);
cout << res.size() << "\n";
for(auto u : res)
cout << ++u.first << " " << ++u.second << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2067 ms |
8856 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
412 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
36 ms |
396 KB |
Output is correct |
4 |
Correct |
17 ms |
392 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
41 ms |
408 KB |
Output is correct |
7 |
Correct |
9 ms |
396 KB |
Output is correct |
8 |
Incorrect |
6 ms |
340 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Execution timed out |
2067 ms |
8856 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |