#include <stdio.h>
#include <stdlib.h>
#define N 200000
#define M 500000
int ij[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;
}
char visited[N]; int dd[N][2], ii[N], cc[N], cnt;
void dfs(int i) {
int o;
if (visited[i] || dd[i][0] > 0 && dd[i][1] > 0)
return;
visited[i] = 1;
ii[cnt] = i, cc[cnt] = dd[i][0] == 0 ? 0 : 1, cnt++;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ij[h >> 1];
dd[j][h & 1 ^ 1]--, dfs(j);
}
}
int main() {
int n, m, 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--;
ij[h] = i ^ j;
append(i, h << 1 | 0), dd[i][0]++;
append(j, h << 1 | 1), dd[j][1]++;
}
for (i = 0; i < n; i++)
dfs(i);
for (i = 0; i < n; i++)
if (!visited[i]) {
printf("-1\n");
return 0;
}
printf("%d\n", cnt);
while (cnt--)
printf("%d %d\n", ii[cnt] + 1, cc[cnt]);
return 0;
}
Compilation message
naboj.c: In function 'append':
naboj.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
naboj.c: In function 'dfs':
naboj.c:23:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
23 | if (visited[i] || dd[i][0] > 0 && dd[i][1] > 0)
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~
naboj.c:30:11: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
30 | dd[j][h & 1 ^ 1]--, dfs(j);
| ~~^~~
naboj.c: In function 'main':
naboj.c:37:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
naboj.c:41:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
284 KB |
Output is correct |
3 |
Correct |
0 ms |
288 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 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 |
288 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
17532 KB |
Output is correct |
2 |
Correct |
168 ms |
17728 KB |
Output is correct |
3 |
Correct |
86 ms |
9300 KB |
Output is correct |
4 |
Correct |
174 ms |
17576 KB |
Output is correct |
5 |
Correct |
163 ms |
17600 KB |
Output is correct |
6 |
Correct |
210 ms |
17716 KB |
Output is correct |
7 |
Correct |
158 ms |
17764 KB |
Output is correct |
8 |
Correct |
112 ms |
13800 KB |
Output is correct |
9 |
Correct |
167 ms |
17620 KB |
Output is correct |
10 |
Correct |
163 ms |
17596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
284 KB |
Output is correct |
3 |
Correct |
0 ms |
288 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 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 |
288 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
292 KB |
Output is correct |
15 |
Correct |
176 ms |
17532 KB |
Output is correct |
16 |
Correct |
168 ms |
17728 KB |
Output is correct |
17 |
Correct |
86 ms |
9300 KB |
Output is correct |
18 |
Correct |
174 ms |
17576 KB |
Output is correct |
19 |
Correct |
163 ms |
17600 KB |
Output is correct |
20 |
Correct |
210 ms |
17716 KB |
Output is correct |
21 |
Correct |
158 ms |
17764 KB |
Output is correct |
22 |
Correct |
112 ms |
13800 KB |
Output is correct |
23 |
Correct |
167 ms |
17620 KB |
Output is correct |
24 |
Correct |
163 ms |
17596 KB |
Output is correct |
25 |
Correct |
202 ms |
17016 KB |
Output is correct |
26 |
Correct |
205 ms |
17896 KB |
Output is correct |
27 |
Correct |
208 ms |
19416 KB |
Output is correct |
28 |
Correct |
276 ms |
22064 KB |
Output is correct |
29 |
Correct |
146 ms |
17076 KB |
Output is correct |
30 |
Correct |
307 ms |
21972 KB |
Output is correct |
31 |
Correct |
23 ms |
3260 KB |
Output is correct |
32 |
Correct |
183 ms |
18236 KB |
Output is correct |
33 |
Correct |
309 ms |
24312 KB |
Output is correct |
34 |
Correct |
171 ms |
18680 KB |
Output is correct |
35 |
Correct |
316 ms |
24560 KB |
Output is correct |
36 |
Correct |
185 ms |
18092 KB |
Output is correct |
37 |
Correct |
274 ms |
23064 KB |
Output is correct |
38 |
Correct |
257 ms |
21176 KB |
Output is correct |
39 |
Correct |
289 ms |
24316 KB |
Output is correct |
40 |
Correct |
266 ms |
22468 KB |
Output is correct |
41 |
Correct |
257 ms |
21964 KB |
Output is correct |
42 |
Correct |
339 ms |
25088 KB |
Output is correct |
43 |
Correct |
216 ms |
19276 KB |
Output is correct |
44 |
Correct |
386 ms |
24900 KB |
Output is correct |
45 |
Correct |
176 ms |
18636 KB |
Output is correct |
46 |
Correct |
257 ms |
22308 KB |
Output is correct |