답안 #674256

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
674256 2022-12-23T14:45:02 Z rainboy Mergers (JOI19_mergers) C
100 / 100
591 ms 77296 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	500000

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

int *ej[N], eo[N];

void append(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 aa[N], pp[N], ta[N], tb[N], ll[N], rr[N], ll_[N], rr_[N];

void dfs1(int p, int i) {
	static int time;
	int o;

	pp[i] = p,
		ta[i] = time++;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p)
			dfs1(i, j);
	}
	tb[i] = time;
}

char cut[N];

void dfs2(int p, int i) {
	int o;

	ll_[i] = ll[aa[i]], rr_[i] = rr[aa[i]];
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			dfs2(i, j);
			ll_[i] = min(ll_[i], ll_[j]), rr_[i] = max(rr_[i], rr_[j]);
			cut[j] = ta[j] <= ll_[j] && rr_[j] < tb[j];
		}
	}
}

int ds[N];

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 main() {
	static int dd[N];
	int n, k, h, i, j, l;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (h = 0; h < n - 1; h++) {
		scanf("%d%d", &i, &j), i--, j--;
		append(i, j), append(j, i);
	}
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]), aa[i]--;
	dfs1(-1, 0);
	memset(ll, 0x3f, n * sizeof *ll), memset(rr, -1, n * sizeof *rr);
	for (i = 0; i < n; i++)
		ll[aa[i]] = min(ll[aa[i]], ta[i]), rr[aa[i]] = max(rr[aa[i]], ta[i]);
	dfs2(-1, 0);
	memset(ds, -1, n * sizeof *ds);
	for (i = 1; i < n; i++)
		if (!cut[i])
			join(i, pp[i]);
	for (i = 1; i < n; i++)
		if (cut[i])
			dd[find(i)]++, dd[find(pp[i])]++;
	l = 0;
	for (i = 0; i < n; i++)
		if (dd[i] == 1)
			l++;
	printf("%d\n", (l + 1) / 2);
	return 0;
}

Compilation message

mergers.c: In function 'append':
mergers.c:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   15 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
mergers.c: In function 'main':
mergers.c:78:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
mergers.c:82:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
mergers.c:86:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |   scanf("%d", &aa[i]), aa[i]--;
      |   ^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 296 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 296 KB Output is correct
14 Correct 0 ms 296 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 296 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 296 KB Output is correct
14 Correct 0 ms 296 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 292 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 2 ms 600 KB Output is correct
28 Correct 2 ms 692 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 2 ms 564 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 2 ms 724 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 2 ms 596 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 2 ms 596 KB Output is correct
38 Correct 0 ms 296 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 3 ms 600 KB Output is correct
41 Correct 2 ms 596 KB Output is correct
42 Correct 2 ms 596 KB Output is correct
43 Correct 2 ms 848 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 2 ms 724 KB Output is correct
46 Correct 2 ms 596 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 2 ms 596 KB Output is correct
49 Correct 1 ms 692 KB Output is correct
50 Correct 2 ms 724 KB Output is correct
51 Correct 2 ms 596 KB Output is correct
52 Correct 1 ms 596 KB Output is correct
53 Correct 2 ms 596 KB Output is correct
54 Correct 2 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 296 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 296 KB Output is correct
14 Correct 0 ms 296 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 48 ms 10216 KB Output is correct
27 Correct 55 ms 10008 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 56 ms 10092 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 64 ms 16892 KB Output is correct
34 Correct 60 ms 10004 KB Output is correct
35 Correct 2 ms 596 KB Output is correct
36 Correct 73 ms 10588 KB Output is correct
37 Correct 2 ms 596 KB Output is correct
38 Correct 2 ms 560 KB Output is correct
39 Correct 48 ms 10104 KB Output is correct
40 Correct 2 ms 852 KB Output is correct
41 Correct 60 ms 9844 KB Output is correct
42 Correct 59 ms 12108 KB Output is correct
43 Correct 1 ms 292 KB Output is correct
44 Correct 64 ms 17268 KB Output is correct
45 Correct 63 ms 13440 KB Output is correct
46 Correct 1 ms 568 KB Output is correct
47 Correct 2 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 10216 KB Output is correct
2 Correct 55 ms 10940 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 61 ms 10716 KB Output is correct
9 Correct 2 ms 560 KB Output is correct
10 Correct 59 ms 10356 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 60 ms 10080 KB Output is correct
13 Correct 59 ms 10484 KB Output is correct
14 Correct 58 ms 10608 KB Output is correct
15 Correct 46 ms 10144 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 50 ms 10816 KB Output is correct
19 Correct 68 ms 14436 KB Output is correct
20 Correct 2 ms 596 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 51 ms 10816 KB Output is correct
23 Correct 2 ms 596 KB Output is correct
24 Correct 59 ms 10188 KB Output is correct
25 Correct 61 ms 13028 KB Output is correct
26 Correct 2 ms 596 KB Output is correct
27 Correct 2 ms 736 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 2 ms 692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 296 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 296 KB Output is correct
14 Correct 0 ms 296 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 292 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 2 ms 600 KB Output is correct
28 Correct 2 ms 692 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 2 ms 564 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 2 ms 724 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 2 ms 596 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 2 ms 596 KB Output is correct
38 Correct 0 ms 296 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 3 ms 600 KB Output is correct
41 Correct 2 ms 596 KB Output is correct
42 Correct 2 ms 596 KB Output is correct
43 Correct 2 ms 848 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 2 ms 724 KB Output is correct
46 Correct 2 ms 596 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 2 ms 596 KB Output is correct
49 Correct 1 ms 692 KB Output is correct
50 Correct 2 ms 724 KB Output is correct
51 Correct 2 ms 596 KB Output is correct
52 Correct 1 ms 596 KB Output is correct
53 Correct 2 ms 596 KB Output is correct
54 Correct 2 ms 596 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 48 ms 10216 KB Output is correct
57 Correct 55 ms 10008 KB Output is correct
58 Correct 1 ms 596 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 0 ms 340 KB Output is correct
61 Correct 56 ms 10092 KB Output is correct
62 Correct 2 ms 596 KB Output is correct
63 Correct 64 ms 16892 KB Output is correct
64 Correct 60 ms 10004 KB Output is correct
65 Correct 2 ms 596 KB Output is correct
66 Correct 73 ms 10588 KB Output is correct
67 Correct 2 ms 596 KB Output is correct
68 Correct 2 ms 560 KB Output is correct
69 Correct 48 ms 10104 KB Output is correct
70 Correct 2 ms 852 KB Output is correct
71 Correct 60 ms 9844 KB Output is correct
72 Correct 59 ms 12108 KB Output is correct
73 Correct 1 ms 292 KB Output is correct
74 Correct 64 ms 17268 KB Output is correct
75 Correct 63 ms 13440 KB Output is correct
76 Correct 1 ms 568 KB Output is correct
77 Correct 2 ms 596 KB Output is correct
78 Correct 46 ms 10216 KB Output is correct
79 Correct 55 ms 10940 KB Output is correct
80 Correct 2 ms 596 KB Output is correct
81 Correct 2 ms 596 KB Output is correct
82 Correct 0 ms 340 KB Output is correct
83 Correct 1 ms 340 KB Output is correct
84 Correct 2 ms 596 KB Output is correct
85 Correct 61 ms 10716 KB Output is correct
86 Correct 2 ms 560 KB Output is correct
87 Correct 59 ms 10356 KB Output is correct
88 Correct 0 ms 340 KB Output is correct
89 Correct 60 ms 10080 KB Output is correct
90 Correct 59 ms 10484 KB Output is correct
91 Correct 58 ms 10608 KB Output is correct
92 Correct 46 ms 10144 KB Output is correct
93 Correct 1 ms 596 KB Output is correct
94 Correct 1 ms 340 KB Output is correct
95 Correct 50 ms 10816 KB Output is correct
96 Correct 68 ms 14436 KB Output is correct
97 Correct 2 ms 596 KB Output is correct
98 Correct 1 ms 340 KB Output is correct
99 Correct 51 ms 10816 KB Output is correct
100 Correct 2 ms 596 KB Output is correct
101 Correct 59 ms 10188 KB Output is correct
102 Correct 61 ms 13028 KB Output is correct
103 Correct 2 ms 596 KB Output is correct
104 Correct 2 ms 736 KB Output is correct
105 Correct 2 ms 596 KB Output is correct
106 Correct 2 ms 596 KB Output is correct
107 Correct 2 ms 692 KB Output is correct
108 Correct 375 ms 53732 KB Output is correct
109 Correct 566 ms 66008 KB Output is correct
110 Correct 563 ms 77296 KB Output is correct
111 Correct 591 ms 75432 KB Output is correct
112 Correct 587 ms 65812 KB Output is correct
113 Correct 397 ms 53964 KB Output is correct
114 Correct 487 ms 49004 KB Output is correct
115 Correct 471 ms 48928 KB Output is correct
116 Correct 496 ms 52588 KB Output is correct
117 Correct 517 ms 53248 KB Output is correct
118 Correct 500 ms 49824 KB Output is correct
119 Correct 519 ms 53196 KB Output is correct
120 Correct 556 ms 66332 KB Output is correct
121 Correct 505 ms 53340 KB Output is correct
122 Correct 523 ms 53112 KB Output is correct
123 Correct 371 ms 53888 KB Output is correct
124 Correct 539 ms 51248 KB Output is correct