#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define al3 array<ll,3>
const int mxn = 1e5+10;
int N,M,Q;
array<ll,3> edges[mxn];
struct DSU{
int dsu[550],sz[550];
void init(int n){
for(int i = 0;i<=n;i++){
dsu[i] = i;
sz[i] = 1;
}
}
int root(int k){
return k == dsu[k]?k:dsu[k] = root(dsu[k]);
}
void onion(int a,int b){
a = root(a),b = root(b);
if(a == b)return;
if(sz[a]<sz[b])swap(a,b);
dsu[b] = a;
sz[a] += sz[b];
return;
}
};
DSU dsu;
void solve(){
ll K;
cin>>K;
sort(edges,edges+M,[&](al3 &a,al3 &b){return abs(a[0]-K)<abs(b[0]-K);});
dsu.init(N);
ll ans = 0;
for(int i = 0;i<M;i++){
auto [w,a,b] = edges[i];
if(dsu.root(a) == dsu.root(b))continue;
ans += abs(w-K);
dsu.onion(a,b);
}
cout<<ans<<'\n';
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N>>M;
for(int i = 0;i<M;i++){
int a,b,c;
cin>>a>>b>>c;
edges[i] = array<ll,3>({c,a,b});
}
cin>>Q;
while(Q--)solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 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 |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 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 |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
64 ms |
2788 KB |
Output is correct |
21 |
Correct |
67 ms |
2652 KB |
Output is correct |
22 |
Correct |
76 ms |
2904 KB |
Output is correct |
23 |
Correct |
66 ms |
2652 KB |
Output is correct |
24 |
Correct |
66 ms |
2788 KB |
Output is correct |
25 |
Correct |
64 ms |
2652 KB |
Output is correct |
26 |
Correct |
79 ms |
2648 KB |
Output is correct |
27 |
Correct |
70 ms |
2684 KB |
Output is correct |
28 |
Correct |
48 ms |
2684 KB |
Output is correct |
29 |
Correct |
46 ms |
2784 KB |
Output is correct |
30 |
Correct |
63 ms |
2784 KB |
Output is correct |
31 |
Correct |
71 ms |
2652 KB |
Output is correct |
32 |
Correct |
72 ms |
2652 KB |
Output is correct |
33 |
Correct |
65 ms |
2788 KB |
Output is correct |
34 |
Correct |
43 ms |
2648 KB |
Output is correct |
35 |
Correct |
48 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5048 ms |
2904 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 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 |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Execution timed out |
5020 ms |
4416 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 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 |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
64 ms |
2788 KB |
Output is correct |
21 |
Correct |
67 ms |
2652 KB |
Output is correct |
22 |
Correct |
76 ms |
2904 KB |
Output is correct |
23 |
Correct |
66 ms |
2652 KB |
Output is correct |
24 |
Correct |
66 ms |
2788 KB |
Output is correct |
25 |
Correct |
64 ms |
2652 KB |
Output is correct |
26 |
Correct |
79 ms |
2648 KB |
Output is correct |
27 |
Correct |
70 ms |
2684 KB |
Output is correct |
28 |
Correct |
48 ms |
2684 KB |
Output is correct |
29 |
Correct |
46 ms |
2784 KB |
Output is correct |
30 |
Correct |
63 ms |
2784 KB |
Output is correct |
31 |
Correct |
71 ms |
2652 KB |
Output is correct |
32 |
Correct |
72 ms |
2652 KB |
Output is correct |
33 |
Correct |
65 ms |
2788 KB |
Output is correct |
34 |
Correct |
43 ms |
2648 KB |
Output is correct |
35 |
Correct |
48 ms |
2652 KB |
Output is correct |
36 |
Execution timed out |
5060 ms |
2792 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 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 |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
64 ms |
2788 KB |
Output is correct |
21 |
Correct |
67 ms |
2652 KB |
Output is correct |
22 |
Correct |
76 ms |
2904 KB |
Output is correct |
23 |
Correct |
66 ms |
2652 KB |
Output is correct |
24 |
Correct |
66 ms |
2788 KB |
Output is correct |
25 |
Correct |
64 ms |
2652 KB |
Output is correct |
26 |
Correct |
79 ms |
2648 KB |
Output is correct |
27 |
Correct |
70 ms |
2684 KB |
Output is correct |
28 |
Correct |
48 ms |
2684 KB |
Output is correct |
29 |
Correct |
46 ms |
2784 KB |
Output is correct |
30 |
Correct |
63 ms |
2784 KB |
Output is correct |
31 |
Correct |
71 ms |
2652 KB |
Output is correct |
32 |
Correct |
72 ms |
2652 KB |
Output is correct |
33 |
Correct |
65 ms |
2788 KB |
Output is correct |
34 |
Correct |
43 ms |
2648 KB |
Output is correct |
35 |
Correct |
48 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
500 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Execution timed out |
5048 ms |
2904 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |