#include<bits/stdc++.h>
using namespace std;
mt19937 rnd(51);
#define ll long long
#define pb push_back
#define ld long double
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> a(m, vector<int>(2));
for (int i = 0; i < m; i++) {
for (int j = 0; j < 2; j++) {
cin >> a[i][j];
}
}
int cnt = 0, cur = 0;
vector<deque<pair<int,int>>> pos(n + 1);
vector<vector<int>> val(m, vector<int>(2)), g(2 * n);
vector<int> num(2 * n), ord, index(2 * n);
vector<bool> is_top(2 * n), used(2 * n);
deque<int> cond, emp, non_emp;
for (int i = 1; i <= n; i++) {
cond.pb(i);
}
for (int i = 0; i < m; i++) {
emp.pb(i);
non_emp.pb(i);
}
for (int i = 0; i < m; i++) {
while (a[i].size() > 0 && a[i].back() == 0) {
a[i].pop_back();
}
}
for (int i = 0; i < m; i++) {
if (a[i].size() > 0) {
index[cur] = i;
num[cur] = a[i][0];
val[i][0] = cur++;
pos[a[i][0]].pb({i, 0});
}
if (a[i].size() > 1) {
index[cur] = i;
num[cur] = a[i][1];
is_top[cur] = 1;
val[i][1] = cur++;
pos[a[i][1]].pb({i, 1});
}
if (a[i].size() > 1) {
int x = val[i][0], y = val[i][1];
g[x].pb(y);
g[y].pb(x);
}
}
for (int i = 1; i <= n; i++) {
int x = val[pos[i][0].first][pos[i][0].second];
int y = val[pos[i][1].first][pos[i][1].second];
g[x].pb(y);
g[y].pb(x);
}
for (int j = 1; j <= 2; j++) {
for (int i = 0; i < 2 * n; i++) {
if (used[i]) continue;
if (g[i].size() <= j) {
deque<int> q{i};
used[i] = 1;
int cnt = 0;
set<int> st;
while (q.size() > 0) {
int v = q.back();
st.insert(index[v]);
q.pop_back();
cnt++;
for (auto to : g[v]) {
if (is_top[v] && is_top[to]) {
ord.pb(num[v]);
}
if (!used[to]) {
used[to] = 1;
q.pb(to);
}
}
}
}
}
}
reverse(ord.begin(), ord.end());
vector<pair<int,int>> ans;
auto upd = [&](int i, int j) -> void {
assert(i != j && a[i].size() > 0 && (a[j].size() == 0 || (a[j].size() == 1 && a[i].back() == a[j][0])));
for (auto to : a[i]) {
cond.pb(to);
}
for (auto to : a[j]) {
cond.pb(to);
}
emp.pb(i);
non_emp.pb(j);
int val = a[i].back();
if (pos[val][0].first == i) {
pos[val].pop_front();
} else {
pos[val].pop_back();
}
pos[val].pb({j, a[j].size()});
a[i].pop_back();
a[j].pb(val);
ans.pb({i, j});
};
while (1) {
bool op = 0;
while (cond.size() > 0) {
int type = cond.back();
cond.pop_back();
if (pos[type][0].first == pos[type][1].first) continue;
if (pos[type][0].second != 0) {
swap(pos[type][0], pos[type][1]);
}
int i = pos[type][0].first, j = pos[type][1].first;
if (pos[type][0].second == 0 && a[i].size() == 1 && a[j].size() == pos[type][1].second + 1) {
upd(j, i);
op = 1;
break;
}
}
if (op) continue;
while (emp.size() > 0) {
int i = emp.front();
if (a[i].size() == 0) {
break;
}
emp.pop_front();
}
if (ord.size() > 0) {
if (emp.size() == 0) {
cout << -1 << endl;
return 0;
}
int type = ord.back();
ord.pop_back();
if (pos[type][0].first == pos[type][1].first) continue;
upd(pos[type][1].first, emp[0]);
upd(pos[type][0].first, emp[0]);
continue;
}
while (non_emp.size() > 0) {
int i = non_emp.front();
if (a[i].size() == 2 && (a[i][0] != a[i][1])) {
break;
}
non_emp.pop_front();
}
if (non_emp.size() == 0) {
break;
}
if (emp.size() == 0) {
cout << -1 << endl;
return 0;
}
upd(non_emp[0], emp[0]);
}
cout << ans.size() << endl;
for (auto to : ans) {
cout << to.first + 1 << ' ' << to.second + 1 << endl;
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:72:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
72 | if (g[i].size() <= j) {
| ~~~~~~~~~~~~^~~~
Main.cpp:128:77: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
128 | if (pos[type][0].second == 0 && a[i].size() == 1 && a[j].size() == pos[type][1].second + 1) {
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:25:9: warning: unused variable 'cnt' [-Wunused-variable]
25 | int cnt = 0, cur = 0;
| ^~~
# |
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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
Correct |
295 ms |
87920 KB |
Output is correct |
2 |
Correct |
363 ms |
103552 KB |
Output is correct |
3 |
Correct |
208 ms |
70980 KB |
Output is correct |
4 |
Correct |
201 ms |
66752 KB |
Output is correct |
5 |
Correct |
354 ms |
102488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
3 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
4 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
3 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
3 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
4 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
3 ms |
1236 KB |
Output is correct |
10 |
Correct |
829 ms |
187048 KB |
Output is correct |
11 |
Correct |
327 ms |
179384 KB |
Output is correct |
12 |
Correct |
448 ms |
179360 KB |
Output is correct |
13 |
Correct |
781 ms |
184476 KB |
Output is correct |
14 |
Correct |
454 ms |
184904 KB |
Output is correct |
15 |
Correct |
434 ms |
177976 KB |
Output is correct |
16 |
Correct |
848 ms |
189304 KB |
Output is correct |
17 |
Correct |
426 ms |
176360 KB |
Output is correct |
18 |
Correct |
814 ms |
188364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
2 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
3 ms |
1108 KB |
Output is correct |
9 |
Correct |
3 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
3 ms |
1364 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
3 ms |
1236 KB |
Output is correct |
17 |
Correct |
1 ms |
1108 KB |
Output is correct |
18 |
Correct |
4 ms |
1236 KB |
Output is correct |
19 |
Correct |
3 ms |
1184 KB |
Output is correct |
20 |
Correct |
3 ms |
1236 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
3 ms |
1108 KB |
Output is correct |
23 |
Correct |
1 ms |
1108 KB |
Output is correct |
24 |
Correct |
3 ms |
1236 KB |
Output is correct |
# |
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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
Correct |
295 ms |
87920 KB |
Output is correct |
12 |
Correct |
363 ms |
103552 KB |
Output is correct |
13 |
Correct |
208 ms |
70980 KB |
Output is correct |
14 |
Correct |
201 ms |
66752 KB |
Output is correct |
15 |
Correct |
354 ms |
102488 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
3 ms |
1236 KB |
Output is correct |
20 |
Correct |
1 ms |
1236 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
4 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1108 KB |
Output is correct |
24 |
Correct |
3 ms |
1236 KB |
Output is correct |
25 |
Correct |
829 ms |
187048 KB |
Output is correct |
26 |
Correct |
327 ms |
179384 KB |
Output is correct |
27 |
Correct |
448 ms |
179360 KB |
Output is correct |
28 |
Correct |
781 ms |
184476 KB |
Output is correct |
29 |
Correct |
454 ms |
184904 KB |
Output is correct |
30 |
Correct |
434 ms |
177976 KB |
Output is correct |
31 |
Correct |
848 ms |
189304 KB |
Output is correct |
32 |
Correct |
426 ms |
176360 KB |
Output is correct |
33 |
Correct |
814 ms |
188364 KB |
Output is correct |
34 |
Correct |
3 ms |
1108 KB |
Output is correct |
35 |
Correct |
1 ms |
1236 KB |
Output is correct |
36 |
Correct |
2 ms |
980 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
38 |
Correct |
1 ms |
1236 KB |
Output is correct |
39 |
Correct |
2 ms |
1108 KB |
Output is correct |
40 |
Correct |
1 ms |
1108 KB |
Output is correct |
41 |
Correct |
3 ms |
1108 KB |
Output is correct |
42 |
Correct |
3 ms |
1108 KB |
Output is correct |
43 |
Correct |
1 ms |
1108 KB |
Output is correct |
44 |
Correct |
3 ms |
1364 KB |
Output is correct |
45 |
Correct |
2 ms |
1108 KB |
Output is correct |
46 |
Correct |
1 ms |
1108 KB |
Output is correct |
47 |
Correct |
1 ms |
1236 KB |
Output is correct |
48 |
Correct |
1 ms |
1108 KB |
Output is correct |
49 |
Correct |
3 ms |
1236 KB |
Output is correct |
50 |
Correct |
1 ms |
1108 KB |
Output is correct |
51 |
Correct |
4 ms |
1236 KB |
Output is correct |
52 |
Correct |
3 ms |
1184 KB |
Output is correct |
53 |
Correct |
3 ms |
1236 KB |
Output is correct |
54 |
Correct |
1 ms |
1108 KB |
Output is correct |
55 |
Correct |
3 ms |
1108 KB |
Output is correct |
56 |
Correct |
1 ms |
1108 KB |
Output is correct |
57 |
Correct |
3 ms |
1236 KB |
Output is correct |
58 |
Correct |
761 ms |
169272 KB |
Output is correct |
59 |
Correct |
840 ms |
184928 KB |
Output is correct |
60 |
Correct |
634 ms |
151760 KB |
Output is correct |
61 |
Correct |
755 ms |
172352 KB |
Output is correct |
62 |
Correct |
327 ms |
184152 KB |
Output is correct |
63 |
Correct |
748 ms |
181400 KB |
Output is correct |
64 |
Correct |
458 ms |
183376 KB |
Output is correct |
65 |
Correct |
812 ms |
180932 KB |
Output is correct |
66 |
Correct |
847 ms |
188584 KB |
Output is correct |
67 |
Correct |
424 ms |
175364 KB |
Output is correct |
68 |
Correct |
784 ms |
186728 KB |
Output is correct |
69 |
Correct |
748 ms |
180992 KB |
Output is correct |
70 |
Correct |
428 ms |
176792 KB |
Output is correct |
71 |
Correct |
450 ms |
183120 KB |
Output is correct |
72 |
Correct |
434 ms |
180668 KB |
Output is correct |
73 |
Correct |
867 ms |
188844 KB |
Output is correct |
74 |
Correct |
427 ms |
177984 KB |
Output is correct |
75 |
Correct |
821 ms |
183484 KB |
Output is correct |
76 |
Correct |
836 ms |
186952 KB |
Output is correct |
77 |
Correct |
820 ms |
181940 KB |
Output is correct |
78 |
Correct |
445 ms |
181168 KB |
Output is correct |
79 |
Correct |
806 ms |
181180 KB |
Output is correct |
80 |
Correct |
433 ms |
177560 KB |
Output is correct |
81 |
Correct |
805 ms |
184896 KB |
Output is correct |