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 300000
int *ej1[N], eo1[N], *ej2[N], eo2[N];
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;
}
int pp[N], dd[N];
void dfs1(int p, int i, int d) {
int o;
pp[i] = p, dd[i] = d;
for (o = eo1[i]; o--; ) {
int j = ej1[i][o];
dfs1(i, j, d + 1);
}
}
int aa[N]; char marked[N];
int dfs2(int i) {
int o, i_, j_;
if (eo2[i] == 0)
aa[i] = i;
else {
aa[i] = -1;
for (o = eo2[i]; o--; ) {
int j = ej2[i][o];
if (!dfs2(j))
return 0;
if (aa[i] == -1)
aa[i] = aa[j];
else {
i_ = aa[i], j_ = aa[j];
while (i_ != j_)
if (dd[i_] > dd[j_]) {
if (!marked[i_])
marked[i_] = -1;
else {
if (marked[i_] != -1)
return 0;
break;
}
i_ = pp[i_];
} else {
if (!marked[j_])
marked[j_] = -1;
else {
if (marked[j_] != -1)
return 0;
break;
}
j_ = pp[j_];
}
aa[i] = i_;
}
}
for (o = eo2[i]; o--; ) {
int j = ej2[i][o];
j_ = aa[j];
while (j_ != aa[i] && marked[j_] == -1)
marked[j_] = 1, j_ = pp[j_];
}
}
return 1;
}
int main() {
int n1, n2, i, j, r1, r2;
scanf("%d%d%*d", &n1, &n2);
for (i = 0; i < n1; i++)
ej1[i] = (int *) malloc(2 * sizeof *ej1[i]);
r1 = 0;
for (j = 0; j < n1; j++) {
scanf("%d", &i), i--;
if (i == -1)
r1 = j;
else
append(ej1, eo1, i, j);
}
for (i = 0; i < n2; i++)
ej2[i] = (int *) malloc(2 * sizeof *ej2[i]);
r2 = 0;
for (j = 0; j < n2; j++) {
scanf("%d", &i), i--;
if (i == -1)
r2 = j;
else
append(ej2, eo2, i, j);
}
dfs1(-1, r1, 0);
printf(dfs2(r2) ? "YES\n" : "NO\n");
return 0;
}
Compilation message (stderr)
family.c: In function 'append':
family.c:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
11 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
family.c: In function 'main':
family.c:84:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%d%d%*d", &n1, &n2);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
family.c:89:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
family.c:99:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
# | 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... |