Submission #557027

#TimeUsernameProblemLanguageResultExecution timeMemory
557027rainboyElection Campaign (JOI15_election_campaign)C11
100 / 100
149 ms25848 KiB
#include <stdio.h> #include <stdlib.h> #define N 100000 #define M 100000 long long max(long long a, long long b) { return a > b ? a : b; } void append(int **ej, int *eo, 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 uu[M], vv[M], ww[M]; int *ej[N], eo[N], *eh[N], eo_[N], dd[N], pp[N], qq[N], ta[N], tb[N], n; long long dp[N]; int dfs1(int p, int i, int d) { int o, s, k_, j_; dd[i] = d, pp[i] = p; s = 1, k_ = 0, j_ = -1; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p) { int k = dfs1(i, j, d + 1); s += k; if (k_ < k) k_ = k, j_ = j; } } qq[i] = j_; return s; } void dfs2(int p, int i, int q) { static int time; int o, j_; ta[i] = time++; j_ = qq[i], qq[i] = q; if (j_ != -1) dfs2(i, j_, q); for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p && j != j_) dfs2(i, j, j); } tb[i] = time; } int lca(int i, int j) { while (qq[i] != qq[j]) if (dd[qq[i]] > dd[qq[j]]) i = pp[qq[i]]; else j = pp[qq[j]]; return dd[i] < dd[j] ? i : j; } long long ft[N]; void update(int i, int n, long long x) { while (i < n) { ft[i] += x; i |= i + 1; } } long long query(int i) { long long x = 0; while (i >= 0) { x += ft[i]; i &= i + 1, i--; } return x; } void dfs3(int p, int i) { int o; long long x, y; x = 0; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p) { dfs3(i, j); x += dp[j]; } } y = 0; for (o = eo_[i]; o--; ) { int h = eh[i][o]; y = max(y, ww[h] - (uu[h] == i ? 0 : query(ta[uu[h]])) - (vv[h] == i ? 0 : query(ta[vv[h]]))); } dp[i] = x + y; update(ta[i], n, y), update(tb[i], n, -y); } int main() { int m, h, i, j; scanf("%d", &n); for (i = 0; i < n; i++) { ej[i] = (int *) malloc(2 * sizeof *ej[i]); eh[i] = (int *) malloc(2 * sizeof *eh[i]); } for (h = 0; h < n - 1; h++) { scanf("%d%d", &i, &j), i--, j--; append(ej, eo, i, j), append(ej, eo, j, i); } dfs1(-1, 0, 0); dfs2(-1, 0, 0); scanf("%d", &m); for (h = 0; h < m; h++) { scanf("%d%d%d", &uu[h], &vv[h], &ww[h]), uu[h]--, vv[h]--; append(eh, eo_, lca(uu[h], vv[h]), h); } dfs3(-1, 0); printf("%lld\n", dp[0]); return 0; }

Compilation message (stderr)

election_campaign.c: In function 'append':
election_campaign.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   12 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
election_campaign.c: In function 'main':
election_campaign.c:111:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
election_campaign.c:117:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
election_campaign.c:122:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |  scanf("%d", &m);
      |  ^~~~~~~~~~~~~~~
election_campaign.c:124:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |   scanf("%d%d%d", &uu[h], &vv[h], &ww[h]), uu[h]--, vv[h]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...