#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 1e6 + 10;
int n, m, A[MAXN][2];
vector<pll> ans;
inline int sz(int i) {
return int(A[i][0] > 0) + int(A[i][1] > 0);
}
inline int emp_col() {
for (int i = 1; i <= m; i++)
if (!sz(i))
return i;
return 0;
}
inline void move(int i, int j) {
A[j][sz(j)] = A[i][sz(i) - 1];
A[i][sz(i) - 1] = 0;
ans.push_back({i, j});
}
inline bool try_match() {
for (int i = 1; i <= m; i++) {
if (sz(i) != 1) continue;
int x = A[i][0];
for (int j = 1; j <= m; j++) {
if (j == i || sz(j) == 0 || A[j][sz(j) - 1] != x) continue;
move(j, i);
return true;
}
}
return false;
}
inline bool is_ok() {
for (int i = 1; i <= m; i++)
if (A[i][0] != A[i][1])
return false;
return true;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) cin >> A[i][0] >> A[i][1];
while (!is_ok()) {
while (try_match()) {}
int x = emp_col();
if (!x) return cout << -1 << endl, 0;
for (int i = 1; i <= n; i++) {
if (sz(i) == 2 && A[i][0] != A[i][1]) {
move(i, x);
break;
}
}
}
if (!is_ok()) return cout << -1 << endl, 0;
cout << ans.size() << endl;
for (auto [i, j] : ans)
cout << i << sep << j << endl;
return 0;
}
# |
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 |
0 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 |
0 ms |
256 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 |
Execution timed out |
2077 ms |
1844 KB |
Time limit exceeded |
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 |
141 ms |
452 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 |
0 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 |
0 ms |
256 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 |
Execution timed out |
2077 ms |
1844 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |