Submission #719340

#TimeUsernameProblemLanguageResultExecution timeMemory
719340rainboyMin-max tree (BOI18_minmaxtree)C11
22 / 100
96 ms17352 KiB
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 70000 #define M 70000 unsigned int X = 12345; int rand_() { return (X *= 3) >> 1; } int *ej[N], eo[N], *ei[M], eo_[M]; 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 pp[N], dd[N]; void dfs1(int p, int i, int d) { int o; pp[i] = p, dd[i] = d; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p) dfs1(i, j, d + 1); } } char type[M]; int ii[M], jj[M], ww[M]; void sort(int *hh, int l, int r) { while (l < r) { int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp; while (j < k) if (ww[hh[j]] == ww[h]) j++; else if (ww[hh[j]] < ww[h]) { tmp = hh[i], hh[i] = hh[j], hh[j] = tmp; i++, j++; } else { k--; tmp = hh[j], hh[j] = hh[k], hh[k] = tmp; } sort(hh, l, i); l = k; } } char used[N]; int pp_[N]; int find_(int i) { return !used[i] ? i : (pp_[i] = find_(pp_[i])); } int ds[M]; int find(int i) { return ds[i] < 0 ? i : (ds[i] = find(ds[i])); } int join(int i, int j) { i = find(i); j = find(j); if (i == j) return 0; if (ds[i] > ds[j]) ds[i] = j; else { if (ds[i] == ds[j]) ds[i]--; ds[j] = i; } return 1; } int xx[N], hh1[N], hh2[N]; char visited[M]; void dfs2(int p, int h) { int o; if (p != -1) xx[p] = ww[h]; if (visited[h]) return; visited[h] = 1; for (o = eo_[h]; o--; ) { int i = ei[h][o], h_ = h ^ hh1[i] ^ hh2[i]; if (i != p) dfs2(i, h_); } } int main() { static int hh[M]; int n, m, h, h_, i, j; scanf("%d", &n); for (i = 0; i < n; i++) ej[i] = (int *) malloc(2 * sizeof *ej[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); scanf("%d", &m); for (h = 0; h < m; h++) { static char s[2]; scanf("%s%d%d%d", s, &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--; type[h] = s[0] == 'm' ? 0 : 1; } for (h = 0; h < m; h++) hh[h] = h; sort(hh, 0, m); memset(used, 0, n * sizeof *used); memcpy(pp_, pp, n * sizeof *pp_); memset(hh1, -1, n * sizeof *hh1); for (h = m - 1; h >= 0; h--) { h_ = hh[h]; if (type[h_] == 0) { i = ii[h_], j = jj[h_]; while (1) { i = find_(i), j = find_(j); if (i == j) break; if (dd[i] > dd[j]) hh1[i] = h_, used[i] = 1; else hh1[j] = h_, used[j] = 1; } } } memset(used, 0, n * sizeof *used); memcpy(pp_, pp, n * sizeof *pp_); memset(hh2, -1, n * sizeof *hh2); for (h = 0; h < m; h++) { h_ = hh[h]; if (type[h_] == 1) { i = ii[h_], j = jj[h_]; while (1) { i = find_(i), j = find_(j); if (i == j) break; if (dd[i] > dd[j]) hh2[i] = h_, used[i] = 1; else hh2[j] = h_, used[j] = 1; } } } for (i = 0; i < n; i++) if (hh1[i] == -1) hh1[i] = hh2[i]; else if (hh2[i] == -1) hh2[i] = hh1[i]; for (h = 0; h < m; h++) ei[h] = (int *) malloc(2 * sizeof *ei[h]); for (i = 0; i < n; i++) if (hh1[i] != -1) append(ei, eo_, hh1[i], i), append(ei, eo_, hh2[i], i); memset(ds, -1, m * sizeof *ds); for (i = 0; i < n; i++) if (hh1[i] != -1 && !join(hh1[i], hh2[i]) && !visited[hh1[i]]) dfs2(-1, hh1[i]); for (i = 1; i < n; i++) printf("%d %d %d\n", pp[i] + 1, i + 1, xx[i]); return 0; }

Compilation message (stderr)

minmaxtree.c: In function 'append':
minmaxtree.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   19 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
minmaxtree.c: In function 'main':
minmaxtree.c:108:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
minmaxtree.c:112:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
minmaxtree.c:116:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |  scanf("%d", &m);
      |  ^~~~~~~~~~~~~~~
minmaxtree.c:120:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |   scanf("%s%d%d%d", s, &ii[h], &jj[h], &ww[h]), ii[h]--, jj[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...