#include "bits/stdc++.h"
using namespace std;
#define int long long
#define ar array
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, m; cin>>n>>m;
vector<int> a(m), b(m), w(m), p(m);
for(int i=0;i<m;i++){
p[i] = i;
cin>>a[i]>>b[i]>>w[i];
a[i]--, b[i]--;
}
int q; cin>>q;
while(q--){
int x; cin>>x;
sort(p.begin(), p.end(), [&](int i, int j){
return abs(w[i] - x) < abs(w[j] - x);
});
vector<int> par(n), sz(n, 1);
iota(par.begin(), par.end(), 0);
function<int(int)> f = [&](int x){
return (par[x] == x ? x : par[x] = f(par[x]));
};
auto merge = [&](int a, int b){
a = f(a), b = f(b);
if(a == b) return false;
if(sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b], par[b] = a;
return true;
};
int res = 0;
for(auto i : p){
if(merge(a[i], b[i])){
res += abs(w[i] - x);
}
}
cout<<res<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
123 ms |
3592 KB |
Output is correct |
21 |
Correct |
114 ms |
3472 KB |
Output is correct |
22 |
Correct |
119 ms |
3472 KB |
Output is correct |
23 |
Correct |
117 ms |
3600 KB |
Output is correct |
24 |
Correct |
112 ms |
3580 KB |
Output is correct |
25 |
Correct |
112 ms |
3412 KB |
Output is correct |
26 |
Correct |
121 ms |
3540 KB |
Output is correct |
27 |
Correct |
131 ms |
3596 KB |
Output is correct |
28 |
Correct |
93 ms |
3540 KB |
Output is correct |
29 |
Correct |
84 ms |
5052 KB |
Output is correct |
30 |
Correct |
111 ms |
5112 KB |
Output is correct |
31 |
Correct |
123 ms |
5084 KB |
Output is correct |
32 |
Correct |
118 ms |
4960 KB |
Output is correct |
33 |
Correct |
115 ms |
3460 KB |
Output is correct |
34 |
Correct |
64 ms |
5184 KB |
Output is correct |
35 |
Correct |
96 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Execution timed out |
5059 ms |
3524 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 |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Execution timed out |
5022 ms |
2056 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 |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
123 ms |
3592 KB |
Output is correct |
21 |
Correct |
114 ms |
3472 KB |
Output is correct |
22 |
Correct |
119 ms |
3472 KB |
Output is correct |
23 |
Correct |
117 ms |
3600 KB |
Output is correct |
24 |
Correct |
112 ms |
3580 KB |
Output is correct |
25 |
Correct |
112 ms |
3412 KB |
Output is correct |
26 |
Correct |
121 ms |
3540 KB |
Output is correct |
27 |
Correct |
131 ms |
3596 KB |
Output is correct |
28 |
Correct |
93 ms |
3540 KB |
Output is correct |
29 |
Correct |
84 ms |
5052 KB |
Output is correct |
30 |
Correct |
111 ms |
5112 KB |
Output is correct |
31 |
Correct |
123 ms |
5084 KB |
Output is correct |
32 |
Correct |
118 ms |
4960 KB |
Output is correct |
33 |
Correct |
115 ms |
3460 KB |
Output is correct |
34 |
Correct |
64 ms |
5184 KB |
Output is correct |
35 |
Correct |
96 ms |
5112 KB |
Output is correct |
36 |
Execution timed out |
5066 ms |
5336 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 |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
123 ms |
3592 KB |
Output is correct |
21 |
Correct |
114 ms |
3472 KB |
Output is correct |
22 |
Correct |
119 ms |
3472 KB |
Output is correct |
23 |
Correct |
117 ms |
3600 KB |
Output is correct |
24 |
Correct |
112 ms |
3580 KB |
Output is correct |
25 |
Correct |
112 ms |
3412 KB |
Output is correct |
26 |
Correct |
121 ms |
3540 KB |
Output is correct |
27 |
Correct |
131 ms |
3596 KB |
Output is correct |
28 |
Correct |
93 ms |
3540 KB |
Output is correct |
29 |
Correct |
84 ms |
5052 KB |
Output is correct |
30 |
Correct |
111 ms |
5112 KB |
Output is correct |
31 |
Correct |
123 ms |
5084 KB |
Output is correct |
32 |
Correct |
118 ms |
4960 KB |
Output is correct |
33 |
Correct |
115 ms |
3460 KB |
Output is correct |
34 |
Correct |
64 ms |
5184 KB |
Output is correct |
35 |
Correct |
96 ms |
5112 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Execution timed out |
5059 ms |
3524 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |