#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3xxEYjy ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
struct dsu{
int n,cmps;
vector<int> par,siz;
dsu(int m){init(m);}
void init(int m){
n=m;
cmps=m;
par.resize(n,0);
siz.resize(n,0);
for(int i=0;i<n;i++){
par[i]=i;
siz[i]=1;
}
}
void merge(int v,int u){
v=parent(v),u=parent(u);
if(v==u)return;
cmps--;
if(siz[v]<siz[u])swap(v,u);
siz[v]+=siz[u];
par[u]=v;
}
int parent(int v){
return par[v]==v?v:parent(par[v]);
}
bool same(int v, int u){
return parent(v)==parent(u);
}
int size(int v=-1){
return (v==-1?cmps:siz[parent(v)]);
}
};
signed main(){
_3xxEYjy;
int n,m;
cin>>n>>m;
using tup=tuple<int,int,int>;
vec(tup) edges;
rep(i,m){
int u,v,w;
cin>>u>>v>>w;
u-=1,v-=1;
edges.pb({w,u,v});
}
int q;
cin>>q;
rep(i,q){
int x;
cin>>x;
vec(tup) nedges;
for(auto [w,u,v]:edges){
nedges.pb({abs(w-x),u,v});
}
sort(nedges.begin(),nedges.end());
dsu uf(n);
ll res=0;
for(auto &[w,u,v]:nedges){
if(uf.same(u,v)) continue;
uf.merge(u,v);
res+=w*1ll;
}
print(res);
}
//
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
136 ms |
5796 KB |
Output is correct |
21 |
Correct |
131 ms |
4200 KB |
Output is correct |
22 |
Correct |
151 ms |
4264 KB |
Output is correct |
23 |
Correct |
130 ms |
4228 KB |
Output is correct |
24 |
Correct |
134 ms |
4216 KB |
Output is correct |
25 |
Correct |
132 ms |
4212 KB |
Output is correct |
26 |
Correct |
133 ms |
5920 KB |
Output is correct |
27 |
Correct |
139 ms |
5724 KB |
Output is correct |
28 |
Correct |
172 ms |
5776 KB |
Output is correct |
29 |
Correct |
162 ms |
5772 KB |
Output is correct |
30 |
Correct |
133 ms |
5908 KB |
Output is correct |
31 |
Correct |
132 ms |
5812 KB |
Output is correct |
32 |
Correct |
136 ms |
5868 KB |
Output is correct |
33 |
Correct |
142 ms |
4328 KB |
Output is correct |
34 |
Correct |
150 ms |
5788 KB |
Output is correct |
35 |
Correct |
135 ms |
5896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Execution timed out |
5050 ms |
6300 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Execution timed out |
5039 ms |
2788 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
136 ms |
5796 KB |
Output is correct |
21 |
Correct |
131 ms |
4200 KB |
Output is correct |
22 |
Correct |
151 ms |
4264 KB |
Output is correct |
23 |
Correct |
130 ms |
4228 KB |
Output is correct |
24 |
Correct |
134 ms |
4216 KB |
Output is correct |
25 |
Correct |
132 ms |
4212 KB |
Output is correct |
26 |
Correct |
133 ms |
5920 KB |
Output is correct |
27 |
Correct |
139 ms |
5724 KB |
Output is correct |
28 |
Correct |
172 ms |
5776 KB |
Output is correct |
29 |
Correct |
162 ms |
5772 KB |
Output is correct |
30 |
Correct |
133 ms |
5908 KB |
Output is correct |
31 |
Correct |
132 ms |
5812 KB |
Output is correct |
32 |
Correct |
136 ms |
5868 KB |
Output is correct |
33 |
Correct |
142 ms |
4328 KB |
Output is correct |
34 |
Correct |
150 ms |
5788 KB |
Output is correct |
35 |
Correct |
135 ms |
5896 KB |
Output is correct |
36 |
Execution timed out |
5014 ms |
6344 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
136 ms |
5796 KB |
Output is correct |
21 |
Correct |
131 ms |
4200 KB |
Output is correct |
22 |
Correct |
151 ms |
4264 KB |
Output is correct |
23 |
Correct |
130 ms |
4228 KB |
Output is correct |
24 |
Correct |
134 ms |
4216 KB |
Output is correct |
25 |
Correct |
132 ms |
4212 KB |
Output is correct |
26 |
Correct |
133 ms |
5920 KB |
Output is correct |
27 |
Correct |
139 ms |
5724 KB |
Output is correct |
28 |
Correct |
172 ms |
5776 KB |
Output is correct |
29 |
Correct |
162 ms |
5772 KB |
Output is correct |
30 |
Correct |
133 ms |
5908 KB |
Output is correct |
31 |
Correct |
132 ms |
5812 KB |
Output is correct |
32 |
Correct |
136 ms |
5868 KB |
Output is correct |
33 |
Correct |
142 ms |
4328 KB |
Output is correct |
34 |
Correct |
150 ms |
5788 KB |
Output is correct |
35 |
Correct |
135 ms |
5896 KB |
Output is correct |
36 |
Correct |
1 ms |
320 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
320 KB |
Output is correct |
39 |
Execution timed out |
5050 ms |
6300 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |