#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// My bug list :
// integer overflow
// 0based, 1based forgotten
// index out of bound
// n, m, i, j typo
// some cases are missing
const int MAX_N = 300010;
int n, m;
vector<int> stk[MAX_N], emp;
vector<int> at[MAX_N];
void fail() {
cout << -1 << '\n';
exit(0);
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 0;i < m;++i) {
int b, t;
cin >> b >> t;
for (int j : {b, t}) if (j) {
stk[i].pb(j);
at[j].pb(i);
}
if (b == 0) emp.pb(i);
}
vector<pair<int,int>> res;
// all full or empty
//
// vector<pair<int,int>> ud;
auto hp = [&]() {
DE("stk");
for (int i = 0;i < m;++i)
debug(AI(stk[i]));
};
auto atob = [&](int a, int b) {
int v = stk[a].back(); stk[a].pop_back();
stk[b].pb(v);
*find(AI(at[v]), a) = b;
res.pb(a, b);
};
auto other = [&](int x, int p) {
return at[x][0] + at[x][1] - p;
};
auto shift = [&](int p) {
assert(stk[p].size() == 1);
int v = stk[p][0], o = other(v, p);
while (stk[o].size() == 2 && stk[o][1] == v) {
atob(o, p);
p = o;
v = stk[p][0];
o = other(v, p);
}
return p;
};
auto eqtop = [&](int p) {
int v = stk[p][1], o = other(v, p);
while (stk[o][0] == v) {
p = o;
v = stk[p][1], o = other(v, p);
}
return p;
};
function<void(int)> take_care = [&](int p) {
if (emp.empty()) fail();
if (stk[p].empty()) {
emp.pb(p);
return;
}
assert(stk[p].size() == 1);
int v = stk[p][0];
int e = emp.back(), o = other(v, p);
if (stk[o].size() == 1 || stk[o][1] == v) {
atob(o, p);
take_care(o);
return;
}
int g = eqtop(o);
atob(g, e);
shift(g);
atob(o, p);
emp.back() = o;
take_care(e);
};
function<void(int)> no_more_care = [&](int p) {
if (stk[p].empty()) {
emp.pb(p);
return;
}
assert(stk[p].size() == 1);
int v = stk[p][0];
int o = other(v, p);
if (stk[o].back() == v) {
atob(o, p);
no_more_care(o);
}
};
for (int i = 0;i < m;++i) if (stk[i].size() == 1) {
no_more_care(i);
}
//hp();
for (int i = 0;i < m;++i) if (stk[i].size() == 1)
take_care(i);
// int x = stk[i][0];
// if (stk[other(x, i)].back() == x)
// take_
for (int i = 1;i <= n;++i) {
int a = at[i][0], b = at[i][1];
if (a == b) continue;
if (stk[a].size() + stk[b].size() == 4 &&
stk[a].back() == i && stk[b].back() == i) {
if (emp.empty()) fail();
int e = emp.back(); emp.pop_back();
atob(a, e);
atob(b, e);
a = shift(a), b = shift(b);
if (stk[a][0] == stk[b][0]) {
atob(a, b);
emp.pb(a);
} else {
take_care(a);
}
}
}
for (int i = 1;i <= n;++i) {
int a = at[i][0], b = at[i][1];
if (a == b) continue;
if (emp.empty()) fail();
if (stk[a][0] != i) swap(a, b);
// stk[a][0] = i
int e = emp.back();
atob(a, e);
int g = shift(a);
atob(e, g);
// must be one up and one down
}
for (int i = 1;i <= n;++i) assert(at[i][0] == at[i][1]);
cout << res.size() << '\n';
for (auto [a, b] : res) {
++a, ++b;
cout << a << ' ' << b << '\n';
}
}
Compilation message
Main.cpp: In lambda function:
Main.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
Main.cpp:51:5: note: in expansion of macro 'DE'
51 | DE("stk");
| ^~
Main.cpp:15:20: warning: statement has no effect [-Wunused-value]
15 | #define debug(...) 0
| ^
Main.cpp:53:7: note: in expansion of macro 'debug'
53 | debug(AI(stk[i]));
| ^~~~~
Main.cpp: In function 'int32_t main()':
Main.cpp:50:8: warning: variable 'hp' set but not used [-Wunused-but-set-variable]
50 | auto hp = [&]() {
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14536 KB |
Output is correct |
3 |
Correct |
3 ms |
14424 KB |
Output is correct |
4 |
Correct |
4 ms |
14428 KB |
Output is correct |
5 |
Correct |
4 ms |
14424 KB |
Output is correct |
6 |
Correct |
4 ms |
14428 KB |
Output is correct |
7 |
Correct |
4 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
14428 KB |
Output is correct |
9 |
Correct |
3 ms |
14424 KB |
Output is correct |
10 |
Correct |
3 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
23112 KB |
Output is correct |
2 |
Correct |
112 ms |
26164 KB |
Output is correct |
3 |
Correct |
65 ms |
22336 KB |
Output is correct |
4 |
Correct |
59 ms |
22328 KB |
Output is correct |
5 |
Correct |
100 ms |
25928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14424 KB |
Output is correct |
2 |
Correct |
4 ms |
14424 KB |
Output is correct |
3 |
Correct |
4 ms |
14424 KB |
Output is correct |
4 |
Correct |
4 ms |
14624 KB |
Output is correct |
5 |
Correct |
5 ms |
14428 KB |
Output is correct |
6 |
Correct |
4 ms |
14424 KB |
Output is correct |
7 |
Correct |
5 ms |
14424 KB |
Output is correct |
8 |
Correct |
4 ms |
14424 KB |
Output is correct |
9 |
Correct |
4 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14424 KB |
Output is correct |
2 |
Correct |
4 ms |
14424 KB |
Output is correct |
3 |
Correct |
4 ms |
14424 KB |
Output is correct |
4 |
Correct |
4 ms |
14624 KB |
Output is correct |
5 |
Correct |
5 ms |
14428 KB |
Output is correct |
6 |
Correct |
4 ms |
14424 KB |
Output is correct |
7 |
Correct |
5 ms |
14424 KB |
Output is correct |
8 |
Correct |
4 ms |
14424 KB |
Output is correct |
9 |
Correct |
4 ms |
14428 KB |
Output is correct |
10 |
Correct |
176 ms |
31908 KB |
Output is correct |
11 |
Correct |
66 ms |
26628 KB |
Output is correct |
12 |
Correct |
95 ms |
26616 KB |
Output is correct |
13 |
Correct |
160 ms |
30948 KB |
Output is correct |
14 |
Correct |
78 ms |
26964 KB |
Output is correct |
15 |
Correct |
88 ms |
26488 KB |
Output is correct |
16 |
Correct |
207 ms |
32196 KB |
Output is correct |
17 |
Correct |
74 ms |
26388 KB |
Output is correct |
18 |
Correct |
155 ms |
31684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14424 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
4 ms |
14424 KB |
Output is correct |
4 |
Correct |
3 ms |
14424 KB |
Output is correct |
5 |
Correct |
5 ms |
14560 KB |
Output is correct |
6 |
Correct |
4 ms |
14424 KB |
Output is correct |
7 |
Correct |
4 ms |
14428 KB |
Output is correct |
8 |
Correct |
4 ms |
14424 KB |
Output is correct |
9 |
Correct |
4 ms |
14428 KB |
Output is correct |
10 |
Correct |
4 ms |
14428 KB |
Output is correct |
11 |
Correct |
4 ms |
14428 KB |
Output is correct |
12 |
Correct |
4 ms |
14424 KB |
Output is correct |
13 |
Correct |
4 ms |
14428 KB |
Output is correct |
14 |
Correct |
4 ms |
14428 KB |
Output is correct |
15 |
Correct |
4 ms |
14424 KB |
Output is correct |
16 |
Correct |
4 ms |
14428 KB |
Output is correct |
17 |
Correct |
3 ms |
14424 KB |
Output is correct |
18 |
Correct |
4 ms |
14428 KB |
Output is correct |
19 |
Correct |
4 ms |
14428 KB |
Output is correct |
20 |
Correct |
4 ms |
14428 KB |
Output is correct |
21 |
Correct |
4 ms |
14532 KB |
Output is correct |
22 |
Correct |
4 ms |
14428 KB |
Output is correct |
23 |
Correct |
4 ms |
14428 KB |
Output is correct |
24 |
Correct |
4 ms |
14424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14536 KB |
Output is correct |
3 |
Correct |
3 ms |
14424 KB |
Output is correct |
4 |
Correct |
4 ms |
14428 KB |
Output is correct |
5 |
Correct |
4 ms |
14424 KB |
Output is correct |
6 |
Correct |
4 ms |
14428 KB |
Output is correct |
7 |
Correct |
4 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
14428 KB |
Output is correct |
9 |
Correct |
3 ms |
14424 KB |
Output is correct |
10 |
Correct |
3 ms |
14428 KB |
Output is correct |
11 |
Correct |
75 ms |
23112 KB |
Output is correct |
12 |
Correct |
112 ms |
26164 KB |
Output is correct |
13 |
Correct |
65 ms |
22336 KB |
Output is correct |
14 |
Correct |
59 ms |
22328 KB |
Output is correct |
15 |
Correct |
100 ms |
25928 KB |
Output is correct |
16 |
Correct |
4 ms |
14424 KB |
Output is correct |
17 |
Correct |
4 ms |
14424 KB |
Output is correct |
18 |
Correct |
4 ms |
14424 KB |
Output is correct |
19 |
Correct |
4 ms |
14624 KB |
Output is correct |
20 |
Correct |
5 ms |
14428 KB |
Output is correct |
21 |
Correct |
4 ms |
14424 KB |
Output is correct |
22 |
Correct |
5 ms |
14424 KB |
Output is correct |
23 |
Correct |
4 ms |
14424 KB |
Output is correct |
24 |
Correct |
4 ms |
14428 KB |
Output is correct |
25 |
Correct |
176 ms |
31908 KB |
Output is correct |
26 |
Correct |
66 ms |
26628 KB |
Output is correct |
27 |
Correct |
95 ms |
26616 KB |
Output is correct |
28 |
Correct |
160 ms |
30948 KB |
Output is correct |
29 |
Correct |
78 ms |
26964 KB |
Output is correct |
30 |
Correct |
88 ms |
26488 KB |
Output is correct |
31 |
Correct |
207 ms |
32196 KB |
Output is correct |
32 |
Correct |
74 ms |
26388 KB |
Output is correct |
33 |
Correct |
155 ms |
31684 KB |
Output is correct |
34 |
Correct |
4 ms |
14424 KB |
Output is correct |
35 |
Correct |
5 ms |
14428 KB |
Output is correct |
36 |
Correct |
4 ms |
14424 KB |
Output is correct |
37 |
Correct |
3 ms |
14424 KB |
Output is correct |
38 |
Correct |
5 ms |
14560 KB |
Output is correct |
39 |
Correct |
4 ms |
14424 KB |
Output is correct |
40 |
Correct |
4 ms |
14428 KB |
Output is correct |
41 |
Correct |
4 ms |
14424 KB |
Output is correct |
42 |
Correct |
4 ms |
14428 KB |
Output is correct |
43 |
Correct |
4 ms |
14428 KB |
Output is correct |
44 |
Correct |
4 ms |
14428 KB |
Output is correct |
45 |
Correct |
4 ms |
14424 KB |
Output is correct |
46 |
Correct |
4 ms |
14428 KB |
Output is correct |
47 |
Correct |
4 ms |
14428 KB |
Output is correct |
48 |
Correct |
4 ms |
14424 KB |
Output is correct |
49 |
Correct |
4 ms |
14428 KB |
Output is correct |
50 |
Correct |
3 ms |
14424 KB |
Output is correct |
51 |
Correct |
4 ms |
14428 KB |
Output is correct |
52 |
Correct |
4 ms |
14428 KB |
Output is correct |
53 |
Correct |
4 ms |
14428 KB |
Output is correct |
54 |
Correct |
4 ms |
14532 KB |
Output is correct |
55 |
Correct |
4 ms |
14428 KB |
Output is correct |
56 |
Correct |
4 ms |
14428 KB |
Output is correct |
57 |
Correct |
4 ms |
14424 KB |
Output is correct |
58 |
Correct |
174 ms |
32780 KB |
Output is correct |
59 |
Correct |
177 ms |
34692 KB |
Output is correct |
60 |
Correct |
134 ms |
31288 KB |
Output is correct |
61 |
Correct |
142 ms |
32856 KB |
Output is correct |
62 |
Correct |
71 ms |
29468 KB |
Output is correct |
63 |
Correct |
216 ms |
32960 KB |
Output is correct |
64 |
Correct |
77 ms |
29264 KB |
Output is correct |
65 |
Correct |
188 ms |
33704 KB |
Output is correct |
66 |
Correct |
169 ms |
34760 KB |
Output is correct |
67 |
Correct |
78 ms |
28768 KB |
Output is correct |
68 |
Correct |
226 ms |
33468 KB |
Output is correct |
69 |
Correct |
169 ms |
32964 KB |
Output is correct |
70 |
Correct |
75 ms |
28680 KB |
Output is correct |
71 |
Correct |
80 ms |
29268 KB |
Output is correct |
72 |
Correct |
72 ms |
29092 KB |
Output is correct |
73 |
Correct |
160 ms |
34632 KB |
Output is correct |
74 |
Correct |
77 ms |
29000 KB |
Output is correct |
75 |
Correct |
181 ms |
34160 KB |
Output is correct |
76 |
Correct |
159 ms |
34428 KB |
Output is correct |
77 |
Correct |
192 ms |
33980 KB |
Output is correct |
78 |
Correct |
115 ms |
29228 KB |
Output is correct |
79 |
Correct |
177 ms |
33916 KB |
Output is correct |
80 |
Correct |
74 ms |
28848 KB |
Output is correct |
81 |
Correct |
150 ms |
34040 KB |
Output is correct |