#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
#define pb push_back
#define F first
#define S second
#define sep ' '
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define set_dec(x) cout << fixed << setprecision(x);
const ll N = 507;
const ll M = 1e5 + 10;
const ll Q = 1e6 + 10;
const ll S = 507;
const ll S2 = 200;
int n, m, q, x[Q], c, sz[N], par[N];
vector<pair<int, pii>> edge, block[S], g, lft, rit;
vector<int> qu[S];
int getPar(int v){
return (par[v] == v ? v : par[v] = getPar(par[v]));
}
bool Union(int u, int v){
u = getPar(u);
v = getPar(v);
if(u == v) return false;
if(sz[v] < sz[u]) swap(u, v);
sz[v] += sz[u]; par[u] = v;
return true;
}
int main(){
fast_io;
cin >> n >> m;
for(int i = 1; i <= m; i++){
int u, v, w;
cin >> u >> v >> w;
edge.pb({w, {u, v}});
}
sort(all(edge));
for(int i = 0; i < m; i++) block[i/S2].pb(edge[i]);
c = (m + S2 - 1) / S2;
cin >> q;
for(int i = 0; i < q; i++) cin >> x[i];
int cur = 0;
for(int i = 0; i < q; i++){
while(cur < c && x[i] >= block[cur][0].F) cur++;
qu[cur].pb(x[i]);
}
for(int i = 0; i <= c; i++){
lft.clear();
rit.clear();
for(int j = 1; j <= n; j++){
sz[j] = 1; par[j] = j;
}
for(int j = i; j < c; j++){
for(auto [w, e]: block[j]){
if(Union(e.F, e.S)) rit.pb({w, e});
}
}
for(int j = 1; j <= n; j++){
sz[j] = 1; par[j] = j;
}
for(int j = i-2; j >= 0; j--){
int SZ = block[j].size();
for(int k = SZ-1; k >= 0; k--){
auto y = block[j][k];
auto w = y.F;
auto e = y.S;
if(Union(e.F, e.S)) lft.pb({w, e});
}
}
for(int j: qu[i]){
g.clear();
for(auto [w, e]: lft) g.pb({j-w, e});
for(auto [w, e]: rit) g.pb({w-j, e});
for(auto [w, e]: block[i-1]) g.pb({abs(w-j), e});
sort(all(g));
for(int k = 1; k <= n; k++){
sz[k] = 1; par[k] = k;
}
ll sum = 0;
for(auto [w, e]: g) if(Union(e.F, e.S)) sum += w;
cout << sum << endl;
}
}
}
/*
5 10
1 2 8
1 3 13
1 4 5
1 5 11
1 5 3
2 3 7
2 4 15
3 4 6
3 5 6
4 5 2
6
3
6
8
10
13
17
*/
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:76:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
76 | for(auto [w, e]: block[j]){
| ^
reconstruction.cpp:95:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
95 | for(auto [w, e]: lft) g.pb({j-w, e});
| ^
reconstruction.cpp:96:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
96 | for(auto [w, e]: rit) g.pb({w-j, e});
| ^
reconstruction.cpp:97:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
97 | for(auto [w, e]: block[i-1]) g.pb({abs(w-j), e});
| ^
reconstruction.cpp:104:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
104 | for(auto [w, e]: g) if(Union(e.F, e.S)) sum += w;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
496 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
496 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
263 ms |
4816 KB |
Output is correct |
21 |
Correct |
335 ms |
4804 KB |
Output is correct |
22 |
Correct |
290 ms |
4808 KB |
Output is correct |
23 |
Correct |
271 ms |
4804 KB |
Output is correct |
24 |
Correct |
274 ms |
4840 KB |
Output is correct |
25 |
Correct |
319 ms |
4808 KB |
Output is correct |
26 |
Correct |
272 ms |
4816 KB |
Output is correct |
27 |
Correct |
264 ms |
4936 KB |
Output is correct |
28 |
Correct |
257 ms |
4816 KB |
Output is correct |
29 |
Correct |
263 ms |
4816 KB |
Output is correct |
30 |
Correct |
267 ms |
4812 KB |
Output is correct |
31 |
Correct |
253 ms |
4812 KB |
Output is correct |
32 |
Correct |
256 ms |
4908 KB |
Output is correct |
33 |
Correct |
238 ms |
4788 KB |
Output is correct |
34 |
Correct |
277 ms |
4808 KB |
Output is correct |
35 |
Correct |
276 ms |
4900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5084 ms |
24356 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
496 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Execution timed out |
5055 ms |
20952 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
496 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
263 ms |
4816 KB |
Output is correct |
21 |
Correct |
335 ms |
4804 KB |
Output is correct |
22 |
Correct |
290 ms |
4808 KB |
Output is correct |
23 |
Correct |
271 ms |
4804 KB |
Output is correct |
24 |
Correct |
274 ms |
4840 KB |
Output is correct |
25 |
Correct |
319 ms |
4808 KB |
Output is correct |
26 |
Correct |
272 ms |
4816 KB |
Output is correct |
27 |
Correct |
264 ms |
4936 KB |
Output is correct |
28 |
Correct |
257 ms |
4816 KB |
Output is correct |
29 |
Correct |
263 ms |
4816 KB |
Output is correct |
30 |
Correct |
267 ms |
4812 KB |
Output is correct |
31 |
Correct |
253 ms |
4812 KB |
Output is correct |
32 |
Correct |
256 ms |
4908 KB |
Output is correct |
33 |
Correct |
238 ms |
4788 KB |
Output is correct |
34 |
Correct |
277 ms |
4808 KB |
Output is correct |
35 |
Correct |
276 ms |
4900 KB |
Output is correct |
36 |
Correct |
1306 ms |
5572 KB |
Output is correct |
37 |
Correct |
1327 ms |
5572 KB |
Output is correct |
38 |
Correct |
1392 ms |
5572 KB |
Output is correct |
39 |
Correct |
1336 ms |
5404 KB |
Output is correct |
40 |
Correct |
1389 ms |
5572 KB |
Output is correct |
41 |
Correct |
1290 ms |
5324 KB |
Output is correct |
42 |
Correct |
1390 ms |
5328 KB |
Output is correct |
43 |
Correct |
1380 ms |
5316 KB |
Output is correct |
44 |
Correct |
1241 ms |
5316 KB |
Output is correct |
45 |
Correct |
1392 ms |
5528 KB |
Output is correct |
46 |
Correct |
1337 ms |
5584 KB |
Output is correct |
47 |
Correct |
1423 ms |
5404 KB |
Output is correct |
48 |
Correct |
1331 ms |
5568 KB |
Output is correct |
49 |
Correct |
1342 ms |
5328 KB |
Output is correct |
50 |
Correct |
552 ms |
5492 KB |
Output is correct |
51 |
Correct |
1000 ms |
5368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
496 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
263 ms |
4816 KB |
Output is correct |
21 |
Correct |
335 ms |
4804 KB |
Output is correct |
22 |
Correct |
290 ms |
4808 KB |
Output is correct |
23 |
Correct |
271 ms |
4804 KB |
Output is correct |
24 |
Correct |
274 ms |
4840 KB |
Output is correct |
25 |
Correct |
319 ms |
4808 KB |
Output is correct |
26 |
Correct |
272 ms |
4816 KB |
Output is correct |
27 |
Correct |
264 ms |
4936 KB |
Output is correct |
28 |
Correct |
257 ms |
4816 KB |
Output is correct |
29 |
Correct |
263 ms |
4816 KB |
Output is correct |
30 |
Correct |
267 ms |
4812 KB |
Output is correct |
31 |
Correct |
253 ms |
4812 KB |
Output is correct |
32 |
Correct |
256 ms |
4908 KB |
Output is correct |
33 |
Correct |
238 ms |
4788 KB |
Output is correct |
34 |
Correct |
277 ms |
4808 KB |
Output is correct |
35 |
Correct |
276 ms |
4900 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Execution timed out |
5084 ms |
24356 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |