# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
733435 |
2023-04-30T17:34:50 Z |
rainboy |
비용 (KOI11_cost) |
C |
|
46 ms |
4276 KB |
#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
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 time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
980 KB |
Output is correct |
2 |
Correct |
21 ms |
1908 KB |
Output is correct |
3 |
Correct |
42 ms |
3368 KB |
Output is correct |
4 |
Correct |
8 ms |
996 KB |
Output is correct |
5 |
Correct |
46 ms |
3500 KB |
Output is correct |
6 |
Correct |
21 ms |
2260 KB |
Output is correct |
7 |
Correct |
44 ms |
3864 KB |
Output is correct |
8 |
Correct |
44 ms |
4260 KB |
Output is correct |
9 |
Correct |
42 ms |
4216 KB |
Output is correct |
10 |
Correct |
41 ms |
4276 KB |
Output is correct |