#include <stdio.h>
#include <stdlib.h>
#define N 300000
#define M 300000
#define K (N * 5 / 2)
int ii[M], jj[M];
int *eh[N], eo[N];
void append(int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
int cc[N], dd[N]; char visited[N], flip[M];
void dfs(int i) {
int o;
visited[i] = 1;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ii[h] ^ jj[h];
if (!visited[j]) {
dfs(j);
if (cc[j])
flip[h] = 1, cc[i] ^= 1, cc[j] ^= 1;
}
}
}
int main() {
static int tt[K], uu[K], vv[K];
int n, m, k, h, i, j;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), i--, j--;
ii[h] = i, jj[h] = j;
append(i, h), append(j, h);
cc[i] ^= 1, dd[i] ^= 1, dd[j] ^= 1;
}
for (i = 0; i < n; i++)
if (!visited[i])
dfs(i);
for (i = 0; i < n; i++)
if (cc[i]) {
printf("-1\n");
return 0;
}
k = 0;
for (h = 0; h < m; h++)
if (flip[h])
tt[k] = 1, uu[k] = ii[h], vv[k] = jj[h], k++;
if (n > 1)
for (i = 0; i < n; i++)
tt[k] = 2, uu[k] = i, vv[k] = (i + 1) % n, k++;
for (i = -1, j = 0; j < n; j++)
if (dd[j] % 2 != 0) {
if (i == -1)
i = j;
else
tt[k] = 2, uu[k] = i, vv[k] = j, k++, i = -1;
}
printf("%d\n", k);
while (k--)
printf("%d %d %d\n", tt[k], uu[k] + 1, vv[k] + 1);
return 0;
}
Compilation message
main.c: In function 'append':
main.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
14 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
main.c: In function 'main':
main.c:40:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
main.c:44:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
296 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
424 KB |
Output is correct |
12 |
Correct |
1 ms |
468 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 |
468 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 |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
1 ms |
468 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 |
468 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 |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
225 ms |
29204 KB |
Output is correct |
22 |
Correct |
290 ms |
37708 KB |
Output is correct |
23 |
Correct |
99 ms |
24728 KB |
Output is correct |
24 |
Correct |
42 ms |
6904 KB |
Output is correct |
25 |
Correct |
54 ms |
8208 KB |
Output is correct |
26 |
Correct |
148 ms |
23240 KB |
Output is correct |
27 |
Correct |
70 ms |
10448 KB |
Output is correct |
28 |
Correct |
58 ms |
9764 KB |
Output is correct |
29 |
Correct |
211 ms |
44204 KB |
Output is correct |
30 |
Correct |
223 ms |
41352 KB |
Output is correct |
31 |
Correct |
57 ms |
8624 KB |
Output is correct |
32 |
Correct |
209 ms |
42912 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
207 ms |
29224 KB |
Output is correct |
36 |
Correct |
77 ms |
10188 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |