Submission #481425

#TimeUsernameProblemLanguageResultExecution timeMemory
481425rainboyPutovanje (COCI20_putovanje)C11
110 / 110
70 ms16004 KiB
#include <stdio.h> #include <stdlib.h> #define N 200000 long long min(long long a, long long b) { return a < b ? a : b; } int ij[N - 1], aa[N - 1], bb[N - 1]; int *eh[N], eo[N]; void append(int i, int j) { int o = eo[i]++; if (o >= 2 && (o & o - 1) == 0) eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]); eh[i][o] = j; } int pp[N], qq[N], dd[N]; int dfs1(int p, int i, int d) { int o, j_, s, k_; pp[i] = p, dd[i] = d; s = 1, k_ = 0, j_ = -1; for (o = eo[i]; o--; ) { int h = eh[i][o], j = i ^ ij[h]; if (j != p) { int k = dfs1(i, j, d + 1); if (k_ < k) k_ = k, j_ = j; s += k; } } qq[i] = j_; return s; } void dfs2(int p, int i, int q) { int o, j_; j_ = qq[i], qq[i] = q; for (o = eo[i]; o--; ) { int h = eh[i][o], j = i ^ ij[h]; if (j != p) { dfs2(i, j, j == j_ ? q : j); } } } 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; } int kk[N]; long long ans; void dfs3(int p, int i) { int o; for (o = eo[i]; o--; ) { int h = eh[i][o], j = i ^ ij[h]; if (j != p) { dfs3(i, j); ans += min((long long) aa[h] * kk[j], bb[h]); kk[i] += kk[j]; } } } int main() { int n, h, i, j; scanf("%d", &n); for (i = 0; i < n; i++) eh[i] = (int *) malloc(2 * sizeof *eh[i]); for (h = 0; h < n - 1; h++) { scanf("%d%d%d%d", &i, &j, &aa[h], &bb[h]), i--, j--; ij[h] = i ^ j; append(i, h), append(j, h); } dfs1(-1, 0, 0); dfs2(-1, 0, 0); for (i = 0; i + 1 < n; i++) kk[i]++, kk[i + 1]++, kk[lca(i, i + 1)] -= 2; dfs3(-1, 0); printf("%lld\n", ans); return 0; }

Compilation message (stderr)

putovanje.c: In function 'append':
putovanje.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   14 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
putovanje.c: In function 'main':
putovanje.c:82:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
putovanje.c:86:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |   scanf("%d%d%d%d", &i, &j, &aa[h], &bb[h]), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...