#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 5e5 + 23;
const ll inf = 1e18;
#define F first
#define S second
#define pb push_back
#define sz(x) ((int)x.size())
#define kill(x) cout<< x , exit(0);
#define all(x) x.begin(),x.end()
#define lc (v<<1)
#define rc ((v<<1)|1)
struct DSU {
int par[N];
int s[N];
void clear(int n) {
iota(par,par+n+1,0);
fill(s,s+n+1,1);
}
int getPar(int v) {
return (par[v] == v ? v : par[v] = getPar(par[v]));
}
bool merge(int v,int u) {
v = getPar(v),u = getPar(u);
if(v == u) return false;
if(s[v] > s[u]) swap(v,u);
par[v] = u;
s[u] += s[v];
return true;
}
} ds;
int n,m;
vector<int> comp;
vector<pair<int,pii>> edges;
pair<int,pii> E[N];
ll calc(int W) {
edges.clear();
ds.clear(n);
for(int i =0 ; i < m;i ++) {
edges.pb( { abs(E[i].F - W) , E[i].S});
}
sort(all(edges));
ll ans= 0;
for(auto [w,e]: edges) {
if(ds.merge(e.F,e.S)) ans += w;
}
return ans;
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n>>m;
for(int i = 0 ; i < m; i ++) {
int v,u,w; cin>>v>>u>>w;
comp.pb(w);
E[i]= {w,{v,u}};
}
int q; cin>>q;
while(q--) {
int x; cin>>x; cout<<calc(x) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
2 ms |
4436 KB |
Output is correct |
6 |
Correct |
2 ms |
4572 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4564 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4572 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4564 KB |
Output is correct |
18 |
Correct |
1 ms |
4568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
2 ms |
4436 KB |
Output is correct |
6 |
Correct |
2 ms |
4572 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4564 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4572 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4564 KB |
Output is correct |
18 |
Correct |
1 ms |
4568 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
99 ms |
10696 KB |
Output is correct |
21 |
Correct |
93 ms |
10576 KB |
Output is correct |
22 |
Correct |
99 ms |
10840 KB |
Output is correct |
23 |
Correct |
93 ms |
10812 KB |
Output is correct |
24 |
Correct |
102 ms |
10644 KB |
Output is correct |
25 |
Correct |
98 ms |
10840 KB |
Output is correct |
26 |
Correct |
97 ms |
10572 KB |
Output is correct |
27 |
Correct |
91 ms |
10840 KB |
Output is correct |
28 |
Correct |
121 ms |
10580 KB |
Output is correct |
29 |
Correct |
138 ms |
10840 KB |
Output is correct |
30 |
Correct |
95 ms |
10576 KB |
Output is correct |
31 |
Correct |
89 ms |
10840 KB |
Output is correct |
32 |
Correct |
95 ms |
10840 KB |
Output is correct |
33 |
Correct |
91 ms |
10840 KB |
Output is correct |
34 |
Correct |
126 ms |
10832 KB |
Output is correct |
35 |
Correct |
95 ms |
10572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Execution timed out |
5050 ms |
10840 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
2 ms |
4436 KB |
Output is correct |
6 |
Correct |
2 ms |
4572 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4564 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4572 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4564 KB |
Output is correct |
18 |
Correct |
1 ms |
4568 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Execution timed out |
5045 ms |
8568 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
2 ms |
4436 KB |
Output is correct |
6 |
Correct |
2 ms |
4572 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4564 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4572 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4564 KB |
Output is correct |
18 |
Correct |
1 ms |
4568 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
99 ms |
10696 KB |
Output is correct |
21 |
Correct |
93 ms |
10576 KB |
Output is correct |
22 |
Correct |
99 ms |
10840 KB |
Output is correct |
23 |
Correct |
93 ms |
10812 KB |
Output is correct |
24 |
Correct |
102 ms |
10644 KB |
Output is correct |
25 |
Correct |
98 ms |
10840 KB |
Output is correct |
26 |
Correct |
97 ms |
10572 KB |
Output is correct |
27 |
Correct |
91 ms |
10840 KB |
Output is correct |
28 |
Correct |
121 ms |
10580 KB |
Output is correct |
29 |
Correct |
138 ms |
10840 KB |
Output is correct |
30 |
Correct |
95 ms |
10576 KB |
Output is correct |
31 |
Correct |
89 ms |
10840 KB |
Output is correct |
32 |
Correct |
95 ms |
10840 KB |
Output is correct |
33 |
Correct |
91 ms |
10840 KB |
Output is correct |
34 |
Correct |
126 ms |
10832 KB |
Output is correct |
35 |
Correct |
95 ms |
10572 KB |
Output is correct |
36 |
Execution timed out |
5040 ms |
10828 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
2 ms |
4436 KB |
Output is correct |
6 |
Correct |
2 ms |
4572 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4564 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4572 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4564 KB |
Output is correct |
18 |
Correct |
1 ms |
4568 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
99 ms |
10696 KB |
Output is correct |
21 |
Correct |
93 ms |
10576 KB |
Output is correct |
22 |
Correct |
99 ms |
10840 KB |
Output is correct |
23 |
Correct |
93 ms |
10812 KB |
Output is correct |
24 |
Correct |
102 ms |
10644 KB |
Output is correct |
25 |
Correct |
98 ms |
10840 KB |
Output is correct |
26 |
Correct |
97 ms |
10572 KB |
Output is correct |
27 |
Correct |
91 ms |
10840 KB |
Output is correct |
28 |
Correct |
121 ms |
10580 KB |
Output is correct |
29 |
Correct |
138 ms |
10840 KB |
Output is correct |
30 |
Correct |
95 ms |
10576 KB |
Output is correct |
31 |
Correct |
89 ms |
10840 KB |
Output is correct |
32 |
Correct |
95 ms |
10840 KB |
Output is correct |
33 |
Correct |
91 ms |
10840 KB |
Output is correct |
34 |
Correct |
126 ms |
10832 KB |
Output is correct |
35 |
Correct |
95 ms |
10572 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4440 KB |
Output is correct |
39 |
Execution timed out |
5050 ms |
10840 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |