Submission #722703

#TimeUsernameProblemLanguageResultExecution timeMemory
722703rainboyWinter Driving (CCO19_day1problem3)C11
25 / 25
130 ms30828 KiB
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 200000 #define M 36 #define K (1 << M / 2) 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, sum_; void dfs1(int p, int i) { int o, centroid; dp[i] = aa[i], centroid = 1; 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]; } void brute(long long *ww, long long *ss, int m) { static long long ss_[K]; int g1, g2, g, h; ss[0] = 0; for (h = 0; h < m; h++) { g1 = 0, g2 = 0, g = 0; while (g < (1 << h + 1)) ss_[g++] = g2 == (1 << h) || g1 < (1 << h) && ss[g1] < ss[g2] + ww[h] ? ss[g1++] : ss[g2++] + ww[h]; memcpy(ss, ss_, (1 << h + 1) * sizeof *ss_); } } int main() { static long long ww[M], ss1[K], ss2[K]; int n, m, m1, m2, g1, g2, h, i, j; long long ans; 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); } for (i = 0; i < n; i++) sum += aa[i]; dfs1(-1, 0); dfs2(-1, c); m = eo[c], m1 = m / 2, m2 = m - m1; for (h = 0; h < m; h++) { j = ej[c][h]; sum_ += ww[h] = dp[j]; } brute(ww, ss1, m1); brute(ww + m1, ss2, m2); g1 = 0, g2 = (1 << m2) - 1, ans = 0; while (g1 < 1 << m1 && g2 >= 0) { ans = max(ans, dq[c] + (ss1[g1] + ss2[g2]) * (sum_ - (ss1[g1] + ss2[g2])) - sum); if ((ss1[g1] + ss2[g2]) * 2 <= sum_) g1++; else g2--; } printf("%lld\n", ans); return 0; }

Compilation message (stderr)

Main.c: In function 'append':
Main.c:16:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   16 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'brute':
Main.c:65:22: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   65 |   while (g < (1 << h + 1))
      |                    ~~^~~
Main.c:66:47: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   66 |    ss_[g++] = g2 == (1 << h) || g1 < (1 << h) && ss[g1] < ss[g2] + ww[h] ? ss[g1++] : ss[g2++] + ww[h];
      |                                 ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:67:27: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   67 |   memcpy(ss, ss_, (1 << h + 1) * sizeof *ss_);
      |                         ~~^~~
Main.c: In function 'main':
Main.c:76:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
Main.c:80:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
Main.c:82:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |   scanf("%d", &i), i--;
      |   ^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...