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>
#include <string.h>
#define N 500000
#define M 500000
int *ej[N], eo[N], *fi[N], fo[N], rr[N], qu[N], ii_[N], cnt;
void append(int **ej, int *eo, int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
void dfs1(int i) {
int o;
if (rr[i])
return;
rr[i] = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
dfs1(j);
}
qu[--cnt] = i;
}
void dfs2(int j, int r) {
int o;
if (rr[j] != -1)
return;
rr[j] = r;
for (o = fo[j]; o--; ) {
int i = fi[j][o];
dfs2(i, r);
}
}
int ds[N * 2];
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
void join(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return;
if (ds[i] > ds[j])
ds[i] = j;
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i;
}
}
int cc[N]; char done[N];
void mark(int j, int c) {
int o;
if (done[j])
return;
cc[j] = c, done[j] = 1;
if (c == 0) {
for (o = fo[j]; o--; ) {
int i = fi[j][o];
if (!done[i] && --eo[i] == 0)
ii_[cnt++] = i;
}
} else {
for (o = fo[j]; o--; ) {
int i = fi[j][o];
mark(i, 0);
}
}
}
int main() {
static int ii[M], jj[M];
int n, m, h, h_, h1, i, j;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
fi[i] = (int *) malloc(2 * sizeof *fi[i]);
}
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), i--, j--;
ii[h] = i, jj[h] = j;
append(ej, eo, i, j), append(fi, fo, j, i);
}
cnt = n;
for (i = 0; i < n; i++)
dfs1(i);
for (h = 0; h < n; h++) {
i = qu[h];
dfs2(i, i);
}
memset(ds, -1, n * 2 * sizeof *ds);
for (h = 0; h < m; h++) {
i = ii[h], j = jj[h];
if (rr[i] == rr[j])
join(i << 1 | 0, j << 1 | 1), join(j << 1 | 0, i << 1 | 1);
}
for (i = 0; i < n; i++)
cc[i] = find(i << 1 | 0) < find(i << 1 | 1) ? 0 : 1;
cnt = 0;
for (i = 0; i < n; i++)
if (eo[i] == 0)
ii_[cnt++] = i;
for (h = n - 1; h >= 0; h = h_) {
while (cnt)
mark(ii_[--cnt], 1);
h_ = h - 1;
while (h_ >= 0 && rr[ii_[h_]] == rr[ii_[h]])
h_--;
for (h1 = h; h1 > h_; h1--)
mark(ii_[h1], cc[ii_[h1]]);
}
cnt = 0;
for (i = 0; i < n; i++)
if (cc[i])
ii_[cnt++] = i;
printf("%d\n", cnt);
while (cnt--)
printf("%d ", ii_[cnt] + 1);
printf("\n");
return 0;
}
Compilation message (stderr)
Main.c: In function 'append':
Main.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:93:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:99:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |