Submission #741147

#TimeUsernameProblemLanguageResultExecution timeMemory
741147rainboyMountains and Valleys (CCO20_day1problem3)C11
13 / 25
8 ms1492 KiB
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 500000 #define M 2000000 #define INF 0x3f3f3f3f int min(int a, int b) { return a < b ? a : b; } int max(int a, int b) { return a > b ? a : b; } int *ej[N], eo[N], n; int ii[M], jj[M], ww[M], m; 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 d_, i_; void dfs1(int p, int i, int d) { int o; if (d_ < d) d_ = d, i_ = i; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p) dfs1(i, j, d + 1); } } int u, v; int dfs2(int p, int i) { int o, s; s = 1; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p) s += dfs2(i, j); } if (s * 3 <= n) return s; if (u == -1) u = i; else v = i; return 0; } int rr[N], dd[N], dp1[N], jj1[N][4], dp2[N], jj2[N][4], dq1[N], dq2[N]; void dfs3(int p, int i, int r, int d) { int o, g, g_; rr[i] = r, dd[i] = d, dp1[i] = 0; memset(jj1[i], -1, sizeof jj1[i]), memset(jj2[i], -1, sizeof jj2[i]); for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p) { dfs3(i, j, r == -1 ? j : r, d + 1); for (g = 0; g < 4; g++) if (jj1[i][g] == -1 || dp1[jj1[i][g]] < dp1[j]) { for (g_ = 3; g_ > g; g_--) jj1[i][g_] = jj1[i][g_ - 1]; jj1[i][g] = j; break; } for (g = 0; g < 4; g++) if (jj2[i][g] == -1 || dp2[jj2[i][g]] < dp2[j]) { for (g_ = 3; g_ > g; g_--) jj2[i][g_] = jj2[i][g_ - 1]; jj2[i][g] = j; break; } } } dp1[i] = dp2[i] = 0; dp1[i] = max(dp1[i], (jj1[i][0] == -1 ? 0 : dp1[jj1[i][0]] + 1)); dp2[i] = max(dp2[i], (jj2[i][0] == -1 ? 0 : dp2[jj2[i][0]])); dp2[i] = max(dp2[i], (jj1[i][0] == -1 ? 0 : dp1[jj1[i][0]] + 1) + (jj1[i][1] == -1 ? 0 : dp1[jj1[i][1]] + 1)); } void dfs4(int p, int i, int w, int x, int y, int z) { int o; if (p == -1) dq1[i] = dq2[i] = -INF; else dq1[i] = max(w, dp1[i] - dd[i]), dq2[i] = max(max(x + dp1[i], y) + 2, max(dp2[i], z)); for (o = eo[i]; o--; ) { int j = ej[i][o], j_, j1, j2; if (j != p) { int w_, x_, y_, z_; w_ = w, x_ = x, y_ = y, z_ = z; if (p != -1) { j_ = j == jj1[i][0] ? jj1[i][1] : jj1[i][0]; w_ = max(w_, (j_ == -1 ? 0 : dp1[j_] + 1) - dd[i]); x_ = max(x_, j_ == -1 ? 0 : dp1[j_] + 1); y_ = max(y_, x + (j_ == -1 ? 0 : dp1[j_] + 1)); j_ = j == jj2[i][0] ? jj2[i][1] : jj2[i][0]; z_ = max(z_, j_ == -1 ? 0 : dp2[j_]); if (j == jj1[i][0]) j1 = jj1[i][1], j2 = jj1[i][2]; else if (j == jj1[i][1]) j1 = jj1[i][0], j2 = jj1[i][2]; else j1 = jj1[i][0], j2 = jj1[i][1]; z_ = max(z_, (j1 == -1 ? 0 : dp1[j1] + 1) + (j2 == -1 ? 0 : dp1[j2] + 1)); } x_--; dfs4(i, j, w_, x_, y_, z_); } } } int ans; void solve(int s) { int g, h, i, j, j1, j2, x; dfs3(-1, s, -1, 0); dfs4(-1, s, -INF, -INF, -INF, -INF); for (h = 0; h < m; h++) { i = ii[h], j = jj[h]; if (ww[h] != 1 && rr[i] != rr[j]) { x = max(max(dq2[i], dq2[j]), dq1[i] + dq1[j] + 2); j1 = j2 = -1; for (g = 0; g < 4; g++) if (jj1[s][g] != rr[i] && jj1[s][g] != rr[j]) { if (j1 == -1) j1 = jj1[s][g]; else if (j2 == -1) j2 = jj1[s][g]; } x = max(x, max(dq1[i], dq1[j]) + (j1 == -1 ? 0 : dp1[j1] + 1) + 2); x = max(x, (j1 == -1 ? 0 : dp1[j1] + 1) + (j2 == -1 ? 0 : dp2[j2] + 1)); ans = min(ans, (n - 1) * 2 - dd[i] - dd[j] + ww[h] - x); } } } int main() { int h, i, j; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) ej[i] = (int *) malloc(2 * sizeof *ej[i]); for (h = 0; h < m; h++) { scanf("%d%d%d", &i, &j, &ww[h]), ii[h] = i, jj[h] = j; if (ww[h] == 1) append(i, j), append(j, i); } d_ = -1, dfs1(-1, 0, 0); d_ = -1, dfs1(-1, i_, 0); ans = (n - 1) * 2 - d_; u = -1, v = -1, dfs2(-1, 0); if (u != -1) solve(u); if (v != -1) solve(v); printf("%d\n", ans); return 0; }

Compilation message (stderr)

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