Submission #733435

#TimeUsernameProblemLanguageResultExecution timeMemory
733435rainboy비용 (KOI11_cost)C11
24 / 24
46 ms4276 KiB
#include <stdio.h> #include <string.h> #define N 100000 #define M 100000 #define MD 1000000000 unsigned int X = 12345; int rand_() { return (X *= 3) >> 1; } 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; } } int ds[N], sz[N]; int find(int i) { return ds[i] < 0 ? i : (ds[i] = find(ds[i])); } int join(int i, int j) { int s; i = find(i); j = find(j); if (i == j) return 0; s = (long long) sz[i] * sz[j] % MD; if (ds[i] > ds[j]) ds[i] = j, sz[j] += sz[i]; else { if (ds[i] == ds[j]) ds[i]--; ds[j] = i, sz[i] += sz[j]; } return s; } int main() { static int hh[M]; int n, m, h, h_, i, sum, ans; scanf("%d%d", &n, &m); sum = 0; for (h = 0; h < m; h++) { scanf("%d%d%d", &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--; sum = (sum + ww[h]) % MD; hh[h] = h; } sort(hh, 0, m); memset(ds, -1, n * sizeof *ds); for (i = 0; i < n; i++) sz[i] = 1; ans = 0; for (h = m - 1; h >= 0; h--) { h_ = hh[h]; ans = (ans + (long long) sum * join(ii[h_], jj[h_])) % MD; sum = (sum - ww[h_] + MD) % MD; } printf("%d\n", ans); return 0; }

Compilation message (stderr)

cost.c: In function 'main':
cost.c:63:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
cost.c:66:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |   scanf("%d%d%d", &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...