#include <bits/stdc++.h>
using namespace std;
long long N,M,Q, ufds[505], rnk[505];
tuple<long long,long long,long long> A[100005], B[100005];
long long findSet(long long i) {
return ufds[i] == i ? i : ufds[i] = findSet(ufds[i]);
}
void unionSet(long long i, long long j) {
long long a = findSet(i), b = findSet(j);
if (a == b) return;
if (rnk[a] < rnk[b]) ufds[a] = b;
if (rnk[a] > rnk[b]) ufds[b] = a;
if (rnk[a] == rnk[b]) ufds[a] = b, rnk[b]++;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> N >> M;
for (long long i = 0; i < M; ++i) {
long long u,v,w; cin >> u >> v >> w; u--, v--;
A[i] = make_tuple(u,v,w);
}
cin >> Q;
while (Q--) {
long long x; cin >> x;
for (long long i = 0; i < M; ++i) B[i] = make_tuple(abs(x-get<2>(A[i])),get<0>(A[i]),get<1>(A[i]));
sort(B,B+M);
for (long long i = 0; i < N; ++i) ufds[i] = i, rnk[i] = 1;
long long ans = 0;
for (long long i = 0; i < M; ++i) {
long long u,v,w; tie(w,u,v) = B[i];
if (findSet(u) == findSet(v)) continue;
ans += w;
unionSet(u,v);
}
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
105 ms |
4940 KB |
Output is correct |
21 |
Correct |
112 ms |
6680 KB |
Output is correct |
22 |
Correct |
111 ms |
6640 KB |
Output is correct |
23 |
Correct |
110 ms |
6672 KB |
Output is correct |
24 |
Correct |
106 ms |
6520 KB |
Output is correct |
25 |
Correct |
105 ms |
6616 KB |
Output is correct |
26 |
Correct |
103 ms |
6596 KB |
Output is correct |
27 |
Correct |
107 ms |
6604 KB |
Output is correct |
28 |
Correct |
128 ms |
6592 KB |
Output is correct |
29 |
Correct |
139 ms |
6680 KB |
Output is correct |
30 |
Correct |
112 ms |
6844 KB |
Output is correct |
31 |
Correct |
106 ms |
6600 KB |
Output is correct |
32 |
Correct |
106 ms |
6728 KB |
Output is correct |
33 |
Correct |
105 ms |
6476 KB |
Output is correct |
34 |
Correct |
130 ms |
6856 KB |
Output is correct |
35 |
Correct |
108 ms |
6700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Execution timed out |
5040 ms |
5008 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Execution timed out |
5034 ms |
2176 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
105 ms |
4940 KB |
Output is correct |
21 |
Correct |
112 ms |
6680 KB |
Output is correct |
22 |
Correct |
111 ms |
6640 KB |
Output is correct |
23 |
Correct |
110 ms |
6672 KB |
Output is correct |
24 |
Correct |
106 ms |
6520 KB |
Output is correct |
25 |
Correct |
105 ms |
6616 KB |
Output is correct |
26 |
Correct |
103 ms |
6596 KB |
Output is correct |
27 |
Correct |
107 ms |
6604 KB |
Output is correct |
28 |
Correct |
128 ms |
6592 KB |
Output is correct |
29 |
Correct |
139 ms |
6680 KB |
Output is correct |
30 |
Correct |
112 ms |
6844 KB |
Output is correct |
31 |
Correct |
106 ms |
6600 KB |
Output is correct |
32 |
Correct |
106 ms |
6728 KB |
Output is correct |
33 |
Correct |
105 ms |
6476 KB |
Output is correct |
34 |
Correct |
130 ms |
6856 KB |
Output is correct |
35 |
Correct |
108 ms |
6700 KB |
Output is correct |
36 |
Execution timed out |
5040 ms |
6904 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
105 ms |
4940 KB |
Output is correct |
21 |
Correct |
112 ms |
6680 KB |
Output is correct |
22 |
Correct |
111 ms |
6640 KB |
Output is correct |
23 |
Correct |
110 ms |
6672 KB |
Output is correct |
24 |
Correct |
106 ms |
6520 KB |
Output is correct |
25 |
Correct |
105 ms |
6616 KB |
Output is correct |
26 |
Correct |
103 ms |
6596 KB |
Output is correct |
27 |
Correct |
107 ms |
6604 KB |
Output is correct |
28 |
Correct |
128 ms |
6592 KB |
Output is correct |
29 |
Correct |
139 ms |
6680 KB |
Output is correct |
30 |
Correct |
112 ms |
6844 KB |
Output is correct |
31 |
Correct |
106 ms |
6600 KB |
Output is correct |
32 |
Correct |
106 ms |
6728 KB |
Output is correct |
33 |
Correct |
105 ms |
6476 KB |
Output is correct |
34 |
Correct |
130 ms |
6856 KB |
Output is correct |
35 |
Correct |
108 ms |
6700 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Execution timed out |
5040 ms |
5008 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |