#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ii = pair<int, int>;
int main() {
cin.tie(0);
int n, m;
cin >> n >> m;
vector<set<int> > L(n), G(n);
vi A(2 * m, -1), P(2 * n, -1);
set<int> LinLib;
for(int i = 0; i < 2 * m; ++i) {
int v;
cin >> v;
if(!v) continue;
--v;
v *= 2;
if(P[v] != -1) ++v;
P[v] = i;
A[i] = v;
}
//set<int> Scoase;
vi InDeg(n, 0), OutDeg(n, 0);
for(int i = 0; i < m; ++i) {
if(A[2 * i] == -1) LinLib.insert(i);
if(A[2 * i + 1] != -1) {
if(A[2 * i] == A[2 * i + 1]) continue;
L[A[2 * i] / 2].insert(A[2 * i + 1] / 2);
G[A[2 * i + 1] / 2].insert(A[2 * i] / 2);
++OutDeg[A[2 * i] / 2];
++InDeg[A[2 * i + 1] / 2];
}
}
set<int> S[3][3]; /// culori dupa indeg/ outdeg
for(int i = 0; i < n; ++i)
S[InDeg[i]][OutDeg[i]].insert(i);
vector<vi> LMono, LBi;
vector<vi> Cicluri;
vi viz(n, 0);
function<int(int, int)> dfs0 = [&](int u, int p) {
if(viz[u] == 1)
return u;
viz[u] = 1;
for(auto it : L[u])
if(it != p) return dfs0(it, u);
for(auto it : G[u])
if(it != p) return dfs0(it, u);
return u;
};
function<void(int, int, bool&, int&, vi&)> dfscomp = [&](int u, int p, bool &cyc, int& dir, vi &comp) {
if(viz[u] == 2) {
cyc = 1;
return;
}
viz[u] = 2;
dir = max(dir, InDeg[u]);
comp.push_back(u);
int nr = 0;
for(auto it : L[u])
if(it != p) {
dfscomp(it, u, cyc, dir, comp);
} else ++nr;
for(auto it : G[u])
if(it != p) {
dfscomp(it, u, cyc, dir, comp);
} else ++nr;
if(nr == 2) cyc = 1;
};
for(int i = 0; i < n; ++i) {
if(!viz[i]) {
int u = dfs0(i, -1), dir = 0;
bool cyc = 0;
vi comp;
dfscomp(u, -1, cyc, dir, comp);
if(cyc) Cicluri.push_back(comp);
else {
if(dir == 2) LBi.push_back(comp);
else LMono.push_back(comp);
}
}
}
int ok = 1;
vector<ii> Sol;
auto move = [&](int from, int to) {
Sol.push_back({from, to});
int pf = 2 * from, pdest = 2 * to;
if(A[pf + 1] != -1) {
L[A[pf] / 2].erase(A[pf + 1] / 2);
G[A[pf + 1] / 2].erase(A[pf] / 2);
--OutDeg[A[pf] / 2];
--InDeg[A[pf + 1] / 2];
++pf;
} else LinLib.insert(from);
if(A[pdest] != -1) {
L[A[pdest] / 2].insert(A[pf] / 2);
G[A[pf] / 2].insert(A[pdest] / 2);
++InDeg[A[pf] / 2];
++OutDeg[A[pdest] / 2];
++pdest;
} else LinLib.erase(to);
A[pdest] = A[pf];
A[pf] = -1;
P[A[pdest]] = pdest;
};
auto scotdeg2 = [&](int u) {
int liber;
if(LinLib.empty()) {
cout << "-1\n";
exit(0);
}
liber = *LinLib.begin();
move(P[2 * u] / 2, liber);
move(P[2 * u + 1] / 2, liber);
};
auto solveSir = [&](vi S) {
if(S.empty()) return;
auto singur = [&](int u) {
if(P[2 * u] / 2 != P[2 * u + 1] / 2)
move(P[2 * u] / 2, P[2 * u + 1] / 2);
};
if(S.size() == 1) {
int u = S[0];
singur(u);
return;
}
deque<int> D;
for(auto it : S) D.push_back(it);
auto scoate = [&](int u) {
int a = P[2 * u], b = P[2 * u + 1];
if(b & 1) swap(a, b);
move(a / 2, b / 2);
};
while(!D.empty()) {
if(InDeg[D.back()] == 0 && OutDeg[D.back()] == 0) {
singur(D.back());
D.pop_back();
continue;
}
if(InDeg[D.front()] == 1) {
scoate(D.front());
D.pop_front();
continue;
}
if(InDeg[D.back()] == 1) {
scoate(D.back());
D.pop_back();
continue;
}
int u = D.back(), p = -1;
vi Acum;
while(InDeg[u] != 2) {
Acum.push_back(D.back());
D.pop_back();
u = D.back();
}
//il scot pe u acum
scotdeg2(u);
D.pop_back();
reverse(Acum.begin(), Acum.end());
for(int i = 0; i + 1 < Acum.size(); ++i)
scoate(Acum[i]);
if(!Acum.empty())
singur(Acum.back());
else {
singur(D.back());
D.pop_back();
}
}
};
auto solveCyc = [&](vi C) {
if(C.size() == 1) {
return; /// e deja bun daca e identificat ca ciclu
}
deque<int> D;
for(auto it : C)
D.push_back(it);
for(int i = 0; i < n; ++i) {
if(InDeg[D.back()] == 2) {
scotdeg2(D.back());
D.pop_back();
vi S;
for(auto it : D) S.push_back(it);
solveSir(S);
return;
}
D.push_front(D.back());
D.pop_back();
}
///e full ciclic
int u = D.back(), a = P[2 * u], b = P[2 * u + 1];
if(b & 1) swap(a, b);
int liber;
if(LinLib.empty()) {
cout << "-1\n";
exit(0);
}
liber = *LinLib.begin();
int urm = A[a - 1];
move(a / 2, liber);
if(D.front() == urm) {
D.push_back(D.front());
D.pop_front();
}
else {
D.push_front(D.back());
D.pop_back();
}
vi S;
for(auto it : D) S.push_back(it);
solveSir(S);
return;
};
for(auto it : LMono) solveSir(it);
for(auto it : LBi) solveSir(it);
for(auto it : Cicluri) solveCyc(it);
if(!ok) {
cout << "-1\n";
return 0;
}
cout << Sol.size() << "\n";
for(auto [a, b] : Sol)
cout << a + 1 << " " << b + 1 << "\n";
return 0;
}
Compilation message
Main.cpp: In lambda function:
Main.cpp:175:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
175 | for(int i = 0; i + 1 < Acum.size(); ++i)
| ~~~~~~^~~~~~~~~~~~~
Main.cpp:163:31: warning: unused variable 'p' [-Wunused-variable]
163 | int u = D.back(), p = -1;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
32516 KB |
Output is correct |
2 |
Correct |
153 ms |
36868 KB |
Output is correct |
3 |
Correct |
139 ms |
27144 KB |
Output is correct |
4 |
Correct |
98 ms |
25860 KB |
Output is correct |
5 |
Correct |
147 ms |
36652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
370 ms |
61144 KB |
Output is correct |
11 |
Correct |
219 ms |
65540 KB |
Output is correct |
12 |
Correct |
181 ms |
54480 KB |
Output is correct |
13 |
Correct |
448 ms |
59072 KB |
Output is correct |
14 |
Correct |
215 ms |
56144 KB |
Output is correct |
15 |
Correct |
188 ms |
54004 KB |
Output is correct |
16 |
Correct |
330 ms |
61116 KB |
Output is correct |
17 |
Correct |
192 ms |
53584 KB |
Output is correct |
18 |
Correct |
394 ms |
60608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
148 ms |
32516 KB |
Output is correct |
12 |
Correct |
153 ms |
36868 KB |
Output is correct |
13 |
Correct |
139 ms |
27144 KB |
Output is correct |
14 |
Correct |
98 ms |
25860 KB |
Output is correct |
15 |
Correct |
147 ms |
36652 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
2 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
370 ms |
61144 KB |
Output is correct |
26 |
Correct |
219 ms |
65540 KB |
Output is correct |
27 |
Correct |
181 ms |
54480 KB |
Output is correct |
28 |
Correct |
448 ms |
59072 KB |
Output is correct |
29 |
Correct |
215 ms |
56144 KB |
Output is correct |
30 |
Correct |
188 ms |
54004 KB |
Output is correct |
31 |
Correct |
330 ms |
61116 KB |
Output is correct |
32 |
Correct |
192 ms |
53584 KB |
Output is correct |
33 |
Correct |
394 ms |
60608 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
600 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
860 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
1 ms |
600 KB |
Output is correct |
49 |
Correct |
1 ms |
600 KB |
Output is correct |
50 |
Correct |
1 ms |
600 KB |
Output is correct |
51 |
Correct |
1 ms |
604 KB |
Output is correct |
52 |
Correct |
1 ms |
604 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
317 ms |
55424 KB |
Output is correct |
59 |
Correct |
334 ms |
60416 KB |
Output is correct |
60 |
Correct |
258 ms |
50748 KB |
Output is correct |
61 |
Correct |
325 ms |
56256 KB |
Output is correct |
62 |
Correct |
227 ms |
66564 KB |
Output is correct |
63 |
Correct |
290 ms |
57860 KB |
Output is correct |
64 |
Correct |
185 ms |
55632 KB |
Output is correct |
65 |
Correct |
327 ms |
58236 KB |
Output is correct |
66 |
Correct |
354 ms |
60828 KB |
Output is correct |
67 |
Correct |
164 ms |
53332 KB |
Output is correct |
68 |
Correct |
489 ms |
59844 KB |
Output is correct |
69 |
Correct |
462 ms |
58052 KB |
Output is correct |
70 |
Correct |
224 ms |
53584 KB |
Output is correct |
71 |
Correct |
191 ms |
55640 KB |
Output is correct |
72 |
Correct |
191 ms |
54712 KB |
Output is correct |
73 |
Correct |
353 ms |
61104 KB |
Output is correct |
74 |
Correct |
181 ms |
54096 KB |
Output is correct |
75 |
Correct |
308 ms |
59356 KB |
Output is correct |
76 |
Correct |
364 ms |
60300 KB |
Output is correct |
77 |
Correct |
320 ms |
58564 KB |
Output is correct |
78 |
Correct |
181 ms |
54864 KB |
Output is correct |
79 |
Correct |
323 ms |
58376 KB |
Output is correct |
80 |
Correct |
169 ms |
53844 KB |
Output is correct |
81 |
Correct |
399 ms |
59424 KB |
Output is correct |