#include <stdio.h>
#include <string.h>
#define N 500000
#define M 5000000
#define INF 0x3f3f3f3f
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], ww_[N];
int find(int i) {
return ds[i] < 0 ? i : find(ds[i]);
}
void join(int i, int j, int w) {
i = find(i);
j = find(j);
if (i == j)
return;
if (ds[i] > ds[j])
ds[i] = j, ww_[i] = w;
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i, ww_[j] = w;
}
}
int query(int i, int j) {
int w = INF;
while (i != j)
if (ww_[i] > ww_[j])
w = ww_[i], i = ds[i];
else
w = ww_[j], j = ds[j];
return w;
}
int main() {
static int hh[M];
int n, m, q, h, h_, i;
scanf("%d%d%d", &n, &m, &q);
for (h = 0; h < m; h++) {
scanf("%d%d%d", &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--;
hh[h] = h;
}
sort(hh, 0, m);
memset(ds, -1, n * sizeof *ds), memset(ww_, -1, n * sizeof *ww_);
for (h = m - 1; h >= 0; h--) {
h_ = hh[h];
join(ii[h_], jj[h_], ww[h_]);
}
while (q--) {
scanf("%d", &i), i--;
printf("%d\n", query(0, i));
}
return 0;
}
Compilation message
sightseeing.c: In function 'main':
sightseeing.c:70:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d%d%d", &n, &m, &q);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
sightseeing.c:72:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d%d%d", &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sightseeing.c:82:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2320 KB |
Output is correct |
2 |
Correct |
20 ms |
1944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1764 ms |
110320 KB |
Output is correct |
2 |
Correct |
2788 ms |
185044 KB |
Output is correct |