Submission #722695

# Submission time Handle Problem Language Result Execution time Memory
722695 2023-04-12T16:43:01 Z rainboy Winter Driving (CCO19_day1problem3) C
0 / 25
1 ms 340 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000

long long max(long long a, long long 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], c; long long dp[N], dq[N], sum;

void dfs1(int p, int i) {
	int o, centroid;

	dp[i] = aa[i], centroid = 0;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			dfs1(i, j);
			dp[i] += dp[j];
			if (dp[j] * 2 > sum)
				centroid = 0;
		}
	}
	if ((sum - dp[i]) * 2 > sum)
		centroid = 0;
	if (centroid)
		c = i;
}

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

	dp[i] = aa[i], dq[i] = 0;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			dfs2(i, j);
			dp[i] += dp[j], dq[i] += dq[j];
		}
	}
	dq[i] += aa[i] * dp[i];
}

int main() {
	int n, i, j, o, b;
	long long ans, sum, sum1, sum2;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (j = 1; j < n; j++) {
		scanf("%d", &i), i--;
		append(i, j), append(j, i);
	}
	sum = 0;
	for (i = 0; i < n; i++)
		sum += aa[i];
	dfs1(-1, 0);
	dfs2(-1, c);
	ans = 0;
	for (b = 0; b < 1 << eo[c]; b++) {
		sum1 = sum2 = 0;
		for (o = 0; o < eo[c]; o++) {
			j = ej[c][o];
			if ((b & 1 << o) == 0)
				sum1 += dp[j];
			else
				sum2 += dp[j];
		}
		ans = max(ans, dq[c] + (long long) sum1 * sum2);
	}
	ans -= sum;
	printf("%lld\n", ans);
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   13 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:59:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
Main.c:63:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
Main.c:65:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |   scanf("%d", &i), i--;
      |   ^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -