This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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--;
| ^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |