답안 #895366

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
895366 2023-12-29T19:38:19 Z rainboy 족보 (KOI18_family) C
100 / 100
221 ms 44832 KB
#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 {
		for (o = eo2[i]; o--; ) {
			int j = ej2[i][o];

			if (!dfs2(j))
				return 0;
		}
		aa[i] = -1;
		for (o = eo2[i]; o--; ) {
			int j = ej2[i][o];

			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

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:88:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |  scanf("%d%d%*d", &n1, &n2);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~
family.c:93:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |   scanf("%d", &i), i--;
      |   ^~~~~~~~~~~~~~~
family.c:103:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |   scanf("%d", &i), i--;
      |   ^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6744 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6488 KB Output is correct
36 Correct 1 ms 6492 KB Output is correct
37 Correct 1 ms 6492 KB Output is correct
38 Correct 1 ms 6488 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 1 ms 6492 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 1 ms 6492 KB Output is correct
45 Correct 1 ms 6492 KB Output is correct
46 Correct 1 ms 6492 KB Output is correct
47 Correct 1 ms 6492 KB Output is correct
48 Correct 1 ms 6492 KB Output is correct
49 Correct 1 ms 6572 KB Output is correct
50 Correct 1 ms 6496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6744 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6488 KB Output is correct
36 Correct 1 ms 6492 KB Output is correct
37 Correct 1 ms 6492 KB Output is correct
38 Correct 1 ms 6488 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 1 ms 6492 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 1 ms 6492 KB Output is correct
45 Correct 1 ms 6492 KB Output is correct
46 Correct 1 ms 6492 KB Output is correct
47 Correct 1 ms 6492 KB Output is correct
48 Correct 1 ms 6492 KB Output is correct
49 Correct 1 ms 6572 KB Output is correct
50 Correct 1 ms 6496 KB Output is correct
51 Correct 2 ms 7012 KB Output is correct
52 Correct 2 ms 7004 KB Output is correct
53 Correct 2 ms 6756 KB Output is correct
54 Correct 2 ms 6756 KB Output is correct
55 Correct 2 ms 7012 KB Output is correct
56 Correct 2 ms 7004 KB Output is correct
57 Correct 2 ms 6756 KB Output is correct
58 Correct 2 ms 6756 KB Output is correct
59 Correct 2 ms 7000 KB Output is correct
60 Correct 2 ms 7012 KB Output is correct
61 Correct 2 ms 7012 KB Output is correct
62 Correct 3 ms 7012 KB Output is correct
63 Correct 2 ms 6756 KB Output is correct
64 Correct 2 ms 6756 KB Output is correct
65 Correct 1 ms 6492 KB Output is correct
66 Correct 2 ms 6784 KB Output is correct
67 Correct 2 ms 7012 KB Output is correct
68 Correct 2 ms 6756 KB Output is correct
69 Correct 2 ms 6748 KB Output is correct
70 Correct 2 ms 6756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6744 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6488 KB Output is correct
36 Correct 1 ms 6492 KB Output is correct
37 Correct 1 ms 6492 KB Output is correct
38 Correct 1 ms 6488 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 1 ms 6492 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 1 ms 6492 KB Output is correct
45 Correct 1 ms 6492 KB Output is correct
46 Correct 1 ms 6492 KB Output is correct
47 Correct 1 ms 6492 KB Output is correct
48 Correct 1 ms 6492 KB Output is correct
49 Correct 1 ms 6572 KB Output is correct
50 Correct 1 ms 6496 KB Output is correct
51 Correct 2 ms 7012 KB Output is correct
52 Correct 2 ms 7004 KB Output is correct
53 Correct 2 ms 6756 KB Output is correct
54 Correct 2 ms 6756 KB Output is correct
55 Correct 2 ms 7012 KB Output is correct
56 Correct 2 ms 7004 KB Output is correct
57 Correct 2 ms 6756 KB Output is correct
58 Correct 2 ms 6756 KB Output is correct
59 Correct 2 ms 7000 KB Output is correct
60 Correct 2 ms 7012 KB Output is correct
61 Correct 2 ms 7012 KB Output is correct
62 Correct 3 ms 7012 KB Output is correct
63 Correct 2 ms 6756 KB Output is correct
64 Correct 2 ms 6756 KB Output is correct
65 Correct 1 ms 6492 KB Output is correct
66 Correct 2 ms 6784 KB Output is correct
67 Correct 2 ms 7012 KB Output is correct
68 Correct 2 ms 6756 KB Output is correct
69 Correct 2 ms 6748 KB Output is correct
70 Correct 2 ms 6756 KB Output is correct
71 Correct 15 ms 12148 KB Output is correct
72 Correct 15 ms 12644 KB Output is correct
73 Correct 9 ms 10932 KB Output is correct
74 Correct 17 ms 13912 KB Output is correct
75 Correct 17 ms 13392 KB Output is correct
76 Correct 217 ms 44832 KB Output is correct
77 Correct 184 ms 34604 KB Output is correct
78 Correct 129 ms 33364 KB Output is correct
79 Correct 180 ms 34568 KB Output is correct
80 Correct 164 ms 32084 KB Output is correct
81 Correct 13 ms 12120 KB Output is correct
82 Correct 14 ms 12516 KB Output is correct
83 Correct 8 ms 10844 KB Output is correct
84 Correct 19 ms 13764 KB Output is correct
85 Correct 19 ms 13144 KB Output is correct
86 Correct 131 ms 42512 KB Output is correct
87 Correct 115 ms 42408 KB Output is correct
88 Correct 89 ms 33248 KB Output is correct
89 Correct 221 ms 33496 KB Output is correct
90 Correct 151 ms 32252 KB Output is correct