#pragma GCC optimize("O3,unroll-loops")
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <array>
using namespace std;
using ll = long long;
template <typename T, int sz>
using ar = array<T, sz>;
const int N = 505, M = 101000;
int n, m, q, ds[N];
ll mst;
ar<int, 3> e[M];
int ds_find(int i) {
return (ds[i] < 0) ? i : (ds[i] = ds_find(ds[i]));
}
void ds_unite(int i, int j, int w) {
i = ds_find(i);
j = ds_find(j);
if (i == j) return;
mst += w;
if (-ds[i] > -ds[j]) swap(i, j);
ds[j] += ds[i];
ds[i] = j;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i)
scanf("%d%d%d", &e[i][1], &e[i][2], &e[i][0]);
sort(e, e+m);
scanf("%d", &q);
int k = 0;
for (int x, i = 0; i < q; ++i) {
scanf("%d", &x);
memset(ds, -1, sizeof * ds * (n + 1));
mst = 0;
while (k < m && e[k][0] < x) ++k;
int pl, pr;
pr = k;
pl = pr - 1;
while (pl >= 0 or pr < m)
if (pl == -1 or pr < m and e[pr][0] - x < x - e[pl][0])
ds_unite(e[pr][1], e[pr][2], e[pr][0] - x), ++pr;
else
ds_unite(e[pl][1], e[pl][2], x - e[pl][0]), --pl;
printf("%lld\n", mst);
}
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:54:36: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
54 | if (pl == -1 or pr < m and e[pr][0] - x < x - e[pl][0])
reconstruction.cpp:36:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
reconstruction.cpp:38:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d%d%d", &e[i][1], &e[i][2], &e[i][0]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
reconstruction.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
1372 KB |
Output is correct |
21 |
Correct |
31 ms |
1372 KB |
Output is correct |
22 |
Correct |
32 ms |
1368 KB |
Output is correct |
23 |
Correct |
32 ms |
1368 KB |
Output is correct |
24 |
Correct |
32 ms |
1368 KB |
Output is correct |
25 |
Correct |
30 ms |
1368 KB |
Output is correct |
26 |
Correct |
35 ms |
1372 KB |
Output is correct |
27 |
Correct |
30 ms |
1612 KB |
Output is correct |
28 |
Correct |
33 ms |
1372 KB |
Output is correct |
29 |
Correct |
33 ms |
1368 KB |
Output is correct |
30 |
Correct |
30 ms |
1372 KB |
Output is correct |
31 |
Correct |
33 ms |
1420 KB |
Output is correct |
32 |
Correct |
32 ms |
1404 KB |
Output is correct |
33 |
Correct |
38 ms |
1372 KB |
Output is correct |
34 |
Correct |
32 ms |
1576 KB |
Output is correct |
35 |
Correct |
34 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Execution timed out |
5043 ms |
1700 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
5086 ms |
11324 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
1372 KB |
Output is correct |
21 |
Correct |
31 ms |
1372 KB |
Output is correct |
22 |
Correct |
32 ms |
1368 KB |
Output is correct |
23 |
Correct |
32 ms |
1368 KB |
Output is correct |
24 |
Correct |
32 ms |
1368 KB |
Output is correct |
25 |
Correct |
30 ms |
1368 KB |
Output is correct |
26 |
Correct |
35 ms |
1372 KB |
Output is correct |
27 |
Correct |
30 ms |
1612 KB |
Output is correct |
28 |
Correct |
33 ms |
1372 KB |
Output is correct |
29 |
Correct |
33 ms |
1368 KB |
Output is correct |
30 |
Correct |
30 ms |
1372 KB |
Output is correct |
31 |
Correct |
33 ms |
1420 KB |
Output is correct |
32 |
Correct |
32 ms |
1404 KB |
Output is correct |
33 |
Correct |
38 ms |
1372 KB |
Output is correct |
34 |
Correct |
32 ms |
1576 KB |
Output is correct |
35 |
Correct |
34 ms |
1368 KB |
Output is correct |
36 |
Execution timed out |
5019 ms |
1596 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
1372 KB |
Output is correct |
21 |
Correct |
31 ms |
1372 KB |
Output is correct |
22 |
Correct |
32 ms |
1368 KB |
Output is correct |
23 |
Correct |
32 ms |
1368 KB |
Output is correct |
24 |
Correct |
32 ms |
1368 KB |
Output is correct |
25 |
Correct |
30 ms |
1368 KB |
Output is correct |
26 |
Correct |
35 ms |
1372 KB |
Output is correct |
27 |
Correct |
30 ms |
1612 KB |
Output is correct |
28 |
Correct |
33 ms |
1372 KB |
Output is correct |
29 |
Correct |
33 ms |
1368 KB |
Output is correct |
30 |
Correct |
30 ms |
1372 KB |
Output is correct |
31 |
Correct |
33 ms |
1420 KB |
Output is correct |
32 |
Correct |
32 ms |
1404 KB |
Output is correct |
33 |
Correct |
38 ms |
1372 KB |
Output is correct |
34 |
Correct |
32 ms |
1576 KB |
Output is correct |
35 |
Correct |
34 ms |
1368 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
600 KB |
Output is correct |
39 |
Execution timed out |
5043 ms |
1700 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |