#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);
}
}
}
}
}
}
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:127:77: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
127 | 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;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
293 ms |
87896 KB |
Output is correct |
2 |
Correct |
390 ms |
103816 KB |
Output is correct |
3 |
Correct |
216 ms |
71020 KB |
Output is correct |
4 |
Correct |
214 ms |
66844 KB |
Output is correct |
5 |
Correct |
361 ms |
102460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
2 ms |
1108 KB |
Output is correct |
4 |
Correct |
2 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 |
5 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
3 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
2 ms |
1108 KB |
Output is correct |
4 |
Correct |
2 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 |
5 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 |
835 ms |
186948 KB |
Output is correct |
11 |
Correct |
323 ms |
179140 KB |
Output is correct |
12 |
Correct |
449 ms |
179300 KB |
Output is correct |
13 |
Correct |
794 ms |
184384 KB |
Output is correct |
14 |
Correct |
482 ms |
184804 KB |
Output is correct |
15 |
Correct |
459 ms |
177900 KB |
Output is correct |
16 |
Correct |
865 ms |
189164 KB |
Output is correct |
17 |
Correct |
444 ms |
176320 KB |
Output is correct |
18 |
Correct |
864 ms |
188320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
3 ms |
1108 KB |
Output is correct |
7 |
Correct |
2 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 |
1236 KB |
Output is correct |
12 |
Correct |
3 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 |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
3 ms |
1236 KB |
Output is correct |
19 |
Correct |
3 ms |
1236 KB |
Output is correct |
20 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
293 ms |
87896 KB |
Output is correct |
12 |
Correct |
390 ms |
103816 KB |
Output is correct |
13 |
Correct |
216 ms |
71020 KB |
Output is correct |
14 |
Correct |
214 ms |
66844 KB |
Output is correct |
15 |
Correct |
361 ms |
102460 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
1108 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
19 |
Correct |
2 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 |
5 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 |
835 ms |
186948 KB |
Output is correct |
26 |
Correct |
323 ms |
179140 KB |
Output is correct |
27 |
Correct |
449 ms |
179300 KB |
Output is correct |
28 |
Correct |
794 ms |
184384 KB |
Output is correct |
29 |
Correct |
482 ms |
184804 KB |
Output is correct |
30 |
Correct |
459 ms |
177900 KB |
Output is correct |
31 |
Correct |
865 ms |
189164 KB |
Output is correct |
32 |
Correct |
444 ms |
176320 KB |
Output is correct |
33 |
Correct |
864 ms |
188320 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 |
3 ms |
1108 KB |
Output is correct |
40 |
Correct |
2 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 |
1236 KB |
Output is correct |
45 |
Correct |
3 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 |
2 ms |
1108 KB |
Output is correct |
51 |
Correct |
3 ms |
1236 KB |
Output is correct |
52 |
Correct |
3 ms |
1236 KB |
Output is correct |
53 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
54 |
Halted |
0 ms |
0 KB |
- |