#include <bits/stdc++.h>
using namespace std;
void process(vector<array<int, 2>> &a, int from, int to) {
int k = a[from][0] == -1;
int t = a[to][1] == -1;
assert(t == 1 || a[from][k] == a[to][t + 1]);
a[to][t] = a[from][k];
a[from][k] = -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<array<int, 2>> a(m), inv(n, {-1, -1}), ans;
auto drivable = [&](int i, int p) {
assert(i < n && p < 2);
int pos = inv[i][p];
if (pos == -1) {
while (true) {
cout << "axaxa" << endl;
}
}
return a[pos][0] == i || a[pos][0] == -1;
};
for (int i = 0; i < m; ++i) {
cin >> a[i][1] >> a[i][0];
--a[i][1], --a[i][0];
for (int k : a[i]) {
if (k != -1) {
if (inv[k][0] == -1) {
inv[k][0] = i;
} else {
inv[k][1] = i;
}
}
}
}
auto b = a;
vector<bool> done(n);
vector<int> free;
for (int i = 0; i < m; ++i) {
if (a[i][0] == -1 && a[i][1] == -1) {
free.push_back(i);
}
}
for (int i = 0; i < n; ++i) {
if (inv[i][0] == inv[i][1]) {
done[i] = true;
}
}
while (find(done.begin(), done.end(), false) != done.end()) {
bool any = false;
vector<bool> kek(m);
for (int x : free) {
assert(a[x][0] == a[x][1] && a[x][0] == -1);
kek[x] = true;
}
for (int i = 0; i < m; ++i) {
if (a[i][0] == a[i][1] && a[i][0] == -1) {
while (!kek[i]) {
cout << "keks" << endl;
}
}
}
for (int i = 0; i < n; ++i) {
if (!done[i]) {
int from = inv[i][0], to = inv[i][1];
if (drivable(i, 0) && drivable(i, 1) && (a[from][0] == -1 || a[to][0] == -1)) {
if (a[to][0] != -1) {
swap(from, to);
}
done[i] = true;
any = true;
ans.push_back({from, to});
a[from][a[from][1] == i] = -1;
a[to][0] = i;
inv[i][0] = inv[i][1] = to;
if (a[from][1] == -1) {
free.push_back(from);
}
}
}
}
if (!any) {
for (int i = 0; i < n; ++i) {
if (!done[i]) {
int x = inv[i][0], y = inv[i][1];
if (drivable(i, 0) && drivable(i, 1) && !free.empty()) {
ans.push_back({x, free.back()});
ans.push_back({y, free.back()});
a[free.back()][1] = a[free.back()][0] = i;
inv[i][0] = inv[i][1] = free.back();
free.pop_back();
a[x][0] = -1, a[y][0] = -1;
assert(a[x][1] != -1 && a[y][1] != -1);
done[i] = true;
any = true;
}
}
}
if (!any) {
break;
}
}
}
if (find(done.begin(), done.end(), false) != done.end()) {
// if (m > 4) {
// assert(false);
// }
if (free.empty()) {
cout << "-1\n";
return 0;
}
for (int i = 0; i < n; ++i) {
if (!done[i]) {
int &x = inv[i][0], &y = inv[i][1];
if (a[x][0] != i) {
swap(x, y);
}
assert(a[inv[i][0]][0] == i && a[inv[i][1]][1] == i);
}
}
for (int i = 0; i < n; ++i) {
if (!done[i]) {
int x = inv[i][0];
int y = inv[i][0];
vector<int> cycle;
do {
cycle.push_back(y);
y = inv[a[y][1]][0];
} while (x != y);
ans.push_back({cycle[0], free.back()});
for (int j = 1; j < size(cycle); ++j) {
ans.push_back({cycle[j], cycle[j - 1]});
}
ans.push_back({free.back(), cycle.back()});
for (int j : cycle) {
done[a[j][0]] = done[a[j][1]] = true;
}
}
}
}
for (auto [x, y] : ans) {
process(b, x, y);
}
for (int i = 0; i < m; ++i) {
assert(b[i][0] == b[i][1]);
}
cout << ans.size() << '\n';
for (auto [x, y] : ans) {
cout << x + 1 << " " << y + 1 << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:166:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
166 | for (int j = 1; j < size(cycle); ++j) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
71 ms |
6340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
11 |
Incorrect |
71 ms |
6340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |