Submission #774572

#TimeUsernameProblemLanguageResultExecution timeMemory
774572rainboyPhone Plans (CCO22_day2problem2)C11
25 / 25
727 ms106944 KiB
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 200000 #define M 200000 #define N_ (N * 4) #define MD 0x7fffffff int min(int a, int b) { return a < b ? a : b; } unsigned int Z = 12345; int rand_() { return (Z *= 3) >> 1; } long long cnt; long long *ek[N_]; int *ev[N_], eo[N_], eo_[N_], X = 123456789; void init() { int h; for (h = 0; h < N_; h++) { eo_[h] = 2; ek[h] = (long long *) malloc(eo_[h] * sizeof *ek[h]); ev[h] = (int *) malloc(eo_[h] * sizeof *ev[h]); } } int hash(long long k) { int k1 = k / MD, k2 = k % MD; return ((long long) k1 * X % MD + k2) % MD * X % MD % N_; } void put(long long k, int v) { int h = hash(k), o; for (o = eo[h]; o--; ) if (ek[h][o] == k) { int v_ = ev[h][o]; cnt += (long long) v_ * (v_ - 1) / 2; v_ += v; cnt -= (long long) v_ * (v_ - 1) / 2; if (v_ == 0) { eo[h]--; while (o < eo[h]) ek[h][o] = ek[h][o + 1], ev[h][o] = ev[h][o + 1], o++; } else ev[h][o] = v_; return; } o = eo[h]++; if (o == eo_[h]) { eo_[h] *= 2; ek[h] = (long long *) realloc(ek[h], eo_[h] * sizeof *ek[h]); ev[h] = (int *) realloc(ev[h], eo_[h] * sizeof *ev[h]); } ek[h][o] = k, ev[h][o] = v; cnt -= (long long) v * (v - 1) / 2; } int n; int ddx[N], ddy[N]; void add(int x, int y, int v) { cnt -= (long long) ddx[x] * (ddx[x] - 1) / 2, ddx[x] += v, cnt += (long long) ddx[x] * (ddx[x] - 1) / 2; cnt -= (long long) ddy[y] * (ddy[y] - 1) / 2, ddy[y] += v, cnt += (long long) ddy[y] * (ddy[y] - 1) / 2; put((long long) x * n + y, v); } int *ww; 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; } } int ds[N]; int find(int i) { return ds[i] < 0 ? i : (ds[i] = find(ds[i])); } 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 *ej1[N], eo1[N], *ej2[N], eo2[N], xx[N], yy[N]; void dfs1(int i, int x) { int o; add(xx[i], yy[i], -1), xx[i] = x, add(xx[i], yy[i], 1); for (o = eo1[i]; o--; ) { int j = ej1[i][o]; dfs1(j, x); } } void dfs2(int i, int y) { int o; add(xx[i], yy[i], -1), yy[i] = y, add(xx[i], yy[i], 1); for (o = eo2[i]; o--; ) { int j = ej2[i][o]; dfs2(j, y); } } int main() { static int ii1[M], jj1[M], ww1[M], hh1[M], ii2[M], jj2[M], ww2[M], hh2[M]; int m1, m1_, m2, m2_, h, h1, h2, i, j, tmp, ans; long long cnt_; init(); scanf("%d%d%d%lld", &n, &m1, &m2, &cnt_); for (h = 0; h < m1; h++) scanf("%d%d%d", &ii1[h], &jj1[h], &ww1[h]), ii1[h]--, jj1[h]--; for (h = 0; h < m2; h++) scanf("%d%d%d", &ii2[h], &jj2[h], &ww2[h]), ii2[h]--, jj2[h]--; for (h = 0; h < m1; h++) hh1[h] = h; ww = ww1, sort(hh1, 0, m1); memset(ds, -1, n * sizeof *ds); for (i = 0; i < n; i++) ej1[i] = (int *) malloc(2 * sizeof *ej1[i]); m1_ = 0; for (h = 0; h < m1; h++) { h1 = hh1[h], i = find(ii1[h1]), j = find(jj1[h1]); if (i == j) continue; if (ds[i] > ds[j]) tmp = i, i = j, j = tmp; if (ds[i] == ds[j]) ds[i]--; ds[j] = i, append(ej1, eo1, i, j); ii1[h1] = i, jj1[h1] = j, hh1[m1_++] = h1; } m1 = m1_; for (h = 0; h < m2; h++) hh2[h] = h; ww = ww2, sort(hh2, 0, m2); memset(ds, -1, n * sizeof *ds); for (i = 0; i < n; i++) ej2[i] = (int *) malloc(2 * sizeof *ej2[i]); m2_ = 0; for (h = 0; h < m2; h++) { h2 = hh2[h], i = find(ii2[h2]), j = find(jj2[h2]); if (i == j) continue; if (ds[i] > ds[j]) tmp = i, i = j, j = tmp; if (ds[i] == ds[j]) ds[i]--; ds[j] = i, append(ej2, eo2, i, j); ii2[h2] = i, jj2[h2] = j, hh2[m2_++] = h2; } m2 = m2_; for (i = 0; i < n; i++) { xx[i] = i, yy[i] = find(i); add(xx[i], yy[i], 1); } ans = MD; for (h1 = -1, h2 = m2 - 1; h1 < m1; h1++) { if (h1 >= 0) dfs1(jj1[hh1[h1]], ii1[hh1[h1]]); while (h2 >= -1 && cnt >= cnt_) { ans = min(ans, (h1 < 0 ? 0 : ww1[hh1[h1]]) + (h2 < 0 ? 0 : ww2[hh2[h2]])); if (h2 >= 0) dfs2(jj2[hh2[h2]], jj2[hh2[h2]]); h2--; } } if (ans == MD) ans = -1; printf("%d\n", ans); return 0; }

Compilation message (stderr)

Main.c: In function 'append':
Main.c:106:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
  106 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:141:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |  scanf("%d%d%d%lld", &n, &m1, &m2, &cnt_);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:143:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  143 |   scanf("%d%d%d", &ii1[h], &jj1[h], &ww1[h]), ii1[h]--, jj1[h]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:145:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  145 |   scanf("%d%d%d", &ii2[h], &jj2[h], &ww2[h]), ii2[h]--, jj2[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...