이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 = 1e2 + 10; // change this shit TODO
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |