Submission #716318

# Submission time Handle Problem Language Result Execution time Memory
716318 2023-03-29T16:12:07 Z rainboy Power Plant (JOI20_power) C
100 / 100
173 ms 26288 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000

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

char cc[N + 1];
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 dp[N], ans;

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

	dp[i] = cc[i] == '1' ? -1 : 0;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			dfs(i, j);
			dp[i] += dp[j];
		}
	}
	dp[i] = max(dp[i], cc[i] == '1' ? 1 : 0);
	ans = max(ans, dp[i] + (p != -1 && cc[p] == '1' ? 1 : 0));
}

int main() {
	int n, h, i, j;

	scanf("%d", &n);
	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);
	}
	scanf("%s", cc);
	dfs(-1, 0);
	printf("%d\n", ans);
	return 0;
}

Compilation message

power.c: In function 'append':
power.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   14 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
power.c: In function 'main':
power.c:40:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
power.c:44:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
power.c:47:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |  scanf("%s", cc);
      |  ^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 292 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 292 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 292 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 292 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 432 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 2 ms 304 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 428 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 428 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 408 KB Output is correct
44 Correct 1 ms 412 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 432 KB Output is correct
47 Correct 2 ms 412 KB Output is correct
48 Correct 1 ms 428 KB Output is correct
49 Correct 1 ms 468 KB Output is correct
50 Correct 1 ms 428 KB Output is correct
51 Correct 1 ms 432 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 432 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 468 KB Output is correct
58 Correct 2 ms 340 KB Output is correct
59 Correct 2 ms 420 KB Output is correct
60 Correct 2 ms 468 KB Output is correct
61 Correct 2 ms 432 KB Output is correct
62 Correct 1 ms 468 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 292 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 292 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 432 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 2 ms 304 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 428 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 428 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 408 KB Output is correct
44 Correct 1 ms 412 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 432 KB Output is correct
47 Correct 2 ms 412 KB Output is correct
48 Correct 1 ms 428 KB Output is correct
49 Correct 1 ms 468 KB Output is correct
50 Correct 1 ms 428 KB Output is correct
51 Correct 1 ms 432 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 432 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 468 KB Output is correct
58 Correct 2 ms 340 KB Output is correct
59 Correct 2 ms 420 KB Output is correct
60 Correct 2 ms 468 KB Output is correct
61 Correct 2 ms 432 KB Output is correct
62 Correct 1 ms 468 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 158 ms 12832 KB Output is correct
65 Correct 118 ms 12852 KB Output is correct
66 Correct 142 ms 12844 KB Output is correct
67 Correct 146 ms 12844 KB Output is correct
68 Correct 117 ms 12840 KB Output is correct
69 Correct 129 ms 12832 KB Output is correct
70 Correct 140 ms 12748 KB Output is correct
71 Correct 113 ms 12844 KB Output is correct
72 Correct 136 ms 12836 KB Output is correct
73 Correct 171 ms 25380 KB Output is correct
74 Correct 135 ms 25120 KB Output is correct
75 Correct 116 ms 12564 KB Output is correct
76 Correct 105 ms 12492 KB Output is correct
77 Correct 141 ms 12460 KB Output is correct
78 Correct 130 ms 12448 KB Output is correct
79 Correct 157 ms 12580 KB Output is correct
80 Correct 130 ms 12448 KB Output is correct
81 Correct 111 ms 12884 KB Output is correct
82 Correct 135 ms 12732 KB Output is correct
83 Correct 164 ms 17856 KB Output is correct
84 Correct 139 ms 19988 KB Output is correct
85 Correct 150 ms 19648 KB Output is correct
86 Correct 130 ms 14184 KB Output is correct
87 Correct 173 ms 13620 KB Output is correct
88 Correct 119 ms 14024 KB Output is correct
89 Correct 114 ms 14000 KB Output is correct
90 Correct 142 ms 13728 KB Output is correct
91 Correct 124 ms 14028 KB Output is correct
92 Correct 114 ms 14352 KB Output is correct
93 Correct 114 ms 16572 KB Output is correct
94 Correct 112 ms 18320 KB Output is correct
95 Correct 110 ms 17224 KB Output is correct
96 Correct 113 ms 15416 KB Output is correct
97 Correct 111 ms 13884 KB Output is correct
98 Correct 107 ms 13264 KB Output is correct
99 Correct 116 ms 13476 KB Output is correct
100 Correct 100 ms 16356 KB Output is correct
101 Correct 101 ms 14272 KB Output is correct
102 Correct 112 ms 13396 KB Output is correct
103 Correct 107 ms 13464 KB Output is correct
104 Correct 84 ms 13524 KB Output is correct
105 Correct 126 ms 22092 KB Output is correct
106 Correct 91 ms 13260 KB Output is correct
107 Correct 92 ms 13356 KB Output is correct
108 Correct 163 ms 25936 KB Output is correct
109 Correct 114 ms 13532 KB Output is correct
110 Correct 160 ms 26288 KB Output is correct
111 Correct 126 ms 12464 KB Output is correct