Submission #716318

#TimeUsernameProblemLanguageResultExecution timeMemory
716318rainboyPower Plant (JOI20_power)C11
100 / 100
173 ms26288 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...