#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200000
#define M 200000
int ij[M * 2], eh[N][2], eo[N]; int *hhh[N], mm[N], n_, h1_, h2_;
int hh1[N * 2], hh2[N * 2], cnt;
void move(int h1, int h2) {
int i;
hh1[cnt] = h1, hh2[cnt] = h2, cnt++;
if (ij[h1 << 1 | 1] != -1)
i = ij[h1 << 1 | 1], ij[h1 << 1 | 1] = -1;
else
i = ij[h1 << 1 | 0], ij[h1 << 1 | 0] = -1;
if (ij[h2 << 1 | 0] == -1)
ij[h2 << 1 | 0] = i;
else
ij[h2 << 1 | 1] = i;
}
int solve1(int *hh, int m) {
int h, h_, h1;
if (h1_ == -1) {
h = 0;
while (h < m && (hh[h] & 1) == 1)
h++;
while (h < m && (hh[h] & 1) == 0)
h++;
if (h < m)
return 0;
}
h = 0;
while (1) {
h_ = h;
while (h_ < m && (hh[h_] & 1) == 1)
h_++;
for (h1 = h + 1; h1 < h_; h1++)
move(hh[h1] >> 1, hh[h1 - 1] >> 1);
h = h_;
while (h_ < m && (hh[h_] & 1) == 0)
h_++;
if (h_ == m) {
for (h1 = h_ - 1; h1 >= h; h1--)
move(hh[h1 - 1] >> 1, hh[h1] >> 1);
if (h1_ == -1)
h1_ = hh[h - 1] >> 1;
else if (h2_ == -1)
h2_ = hh[h - 1] >> 1;
break;
} else {
move(hh[h_] >> 1, h1_);
move(hh[h_ - 1] >> 1, h1_);
for (h1 = h_ - 1; h1 >= h; h1--)
move(hh[h1 - 1] >> 1, hh[h1] >> 1);
h1_ = hh[h - 1] >> 1;
}
h = h_;
}
return 1;
}
int solve2(int *hh, int m) {
static int hh_[N + 1];
int h, h_, tmp;
if (m == 1)
return 1;
if (h1_ == -1)
return 0;
for (h = 0; h + 1 < m; h++)
if ((hh[h] & 1) == 0 && (hh[h + 1] & 1) == 1)
break;
if (h2_ == -1)
h2_ = h1_, h1_ = -1;
move(hh[(h + 1) % m] >> 1, h2_);
for (h_ = 0; h_ < m; h_++)
hh_[h_] = hh[(h + 1 + h_) % m];
hh_[m] = h2_ << 1 | 0;
h2_ = -1;
if ((hh_[0] & 1) == 0) {
tmp = hh_[0], hh_[0] = hh_[m], hh_[m] = tmp;
hh_[0] ^= 1;
}
return solve1(hh_, m + 1);
}
int main() {
static char visited[N];
static int hh[N + 1];
int n, m, m_, g, h, h_, i, j, good;
scanf("%d%d", &n, &m);
h1_ = h2_ = -1;
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), i--, j--;
ij[h << 1 | 0] = i, ij[h << 1 | 1] = j;
if (i != -1)
eh[i][eo[i]++] = h << 1 | 0;
if (j != -1)
eh[j][eo[j]++] = h << 1 | 1;
if (i == -1) {
if (h1_ == -1)
h1_ = h;
else if (h2_ == -1)
h2_ = h;
}
}
for (h = 0; h < m; h++)
if (ij[h << 1 | 1] == -1 && ij[h << 1 | 0] != -1 && !visited[ij[h << 1 | 0]]) {
m_ = 0;
for (h_ = h << 1 | 1; (i = ij[h_ ^ 1]) != -1; h_ ^= eh[i][0] ^ eh[i][1] ^ 1)
hh[m_++] = h_, visited[i] = 1;
hh[m_++] = h_;
hhh[n_] = (int *) malloc((mm[n_] = m_) * sizeof *hhh[n_]);
memcpy(hhh[n_], hh, m_ * sizeof *hh), n_++;
}
if (n_ > 0) {
good = 0;
for (i = 0; i < n_; i++)
if (solve1(hhh[i], mm[i])) {
good = 1;
for (j = 0; j < n_; j++)
if (j != i)
solve1(hhh[j], mm[j]);
break;
}
if (!good) {
printf("-1\n");
return 0;
}
}
for (i = 0; i < n; i++)
if (!visited[i]) {
h = eh[i][0];
m_ = 0;
for (h_ = h; (j = ij[h_ ^ 1]) != i; h_ ^= eh[j][0] ^ eh[j][1] ^ 1)
hh[m_++] = h_, visited[j] = 1;
hh[m_++] = h_, visited[i] = 1;
if (!solve2(hh, m_)) {
printf("-1\n");
return 0;
}
}
printf("%d\n", cnt);
for (g = 0; g < cnt; g++)
printf("%d %d\n", hh1[g] + 1, hh2[g] + 1);
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:97:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:100:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
6472 KB |
Output is correct |
2 |
Correct |
66 ms |
8340 KB |
Output is correct |
3 |
Correct |
50 ms |
6564 KB |
Output is correct |
4 |
Correct |
47 ms |
6268 KB |
Output is correct |
5 |
Correct |
83 ms |
8280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
91 ms |
9432 KB |
Output is correct |
11 |
Correct |
45 ms |
4304 KB |
Output is correct |
12 |
Correct |
45 ms |
4228 KB |
Output is correct |
13 |
Correct |
83 ms |
8392 KB |
Output is correct |
14 |
Correct |
64 ms |
4372 KB |
Output is correct |
15 |
Correct |
42 ms |
4172 KB |
Output is correct |
16 |
Correct |
99 ms |
9468 KB |
Output is correct |
17 |
Correct |
42 ms |
4172 KB |
Output is correct |
18 |
Correct |
90 ms |
9004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
296 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
52 ms |
6472 KB |
Output is correct |
12 |
Correct |
66 ms |
8340 KB |
Output is correct |
13 |
Correct |
50 ms |
6564 KB |
Output is correct |
14 |
Correct |
47 ms |
6268 KB |
Output is correct |
15 |
Correct |
83 ms |
8280 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
91 ms |
9432 KB |
Output is correct |
26 |
Correct |
45 ms |
4304 KB |
Output is correct |
27 |
Correct |
45 ms |
4228 KB |
Output is correct |
28 |
Correct |
83 ms |
8392 KB |
Output is correct |
29 |
Correct |
64 ms |
4372 KB |
Output is correct |
30 |
Correct |
42 ms |
4172 KB |
Output is correct |
31 |
Correct |
99 ms |
9468 KB |
Output is correct |
32 |
Correct |
42 ms |
4172 KB |
Output is correct |
33 |
Correct |
90 ms |
9004 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
296 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
296 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
88 ms |
11012 KB |
Output is correct |
59 |
Correct |
96 ms |
11320 KB |
Output is correct |
60 |
Correct |
92 ms |
10272 KB |
Output is correct |
61 |
Correct |
87 ms |
10460 KB |
Output is correct |
62 |
Correct |
54 ms |
5604 KB |
Output is correct |
63 |
Correct |
91 ms |
10192 KB |
Output is correct |
64 |
Correct |
74 ms |
6432 KB |
Output is correct |
65 |
Correct |
93 ms |
11096 KB |
Output is correct |
66 |
Correct |
115 ms |
11528 KB |
Output is correct |
67 |
Correct |
58 ms |
5272 KB |
Output is correct |
68 |
Correct |
89 ms |
9620 KB |
Output is correct |
69 |
Correct |
87 ms |
9396 KB |
Output is correct |
70 |
Correct |
49 ms |
5648 KB |
Output is correct |
71 |
Correct |
50 ms |
5460 KB |
Output is correct |
72 |
Correct |
51 ms |
5456 KB |
Output is correct |
73 |
Correct |
100 ms |
10572 KB |
Output is correct |
74 |
Correct |
49 ms |
5364 KB |
Output is correct |
75 |
Correct |
104 ms |
10396 KB |
Output is correct |
76 |
Correct |
112 ms |
10540 KB |
Output is correct |
77 |
Correct |
100 ms |
10420 KB |
Output is correct |
78 |
Correct |
72 ms |
5880 KB |
Output is correct |
79 |
Correct |
105 ms |
10680 KB |
Output is correct |
80 |
Correct |
54 ms |
5392 KB |
Output is correct |
81 |
Correct |
93 ms |
10024 KB |
Output is correct |