#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
const ll N = 507;
const ll M = 1e5 + 50;
const ll Q = 1e6 + 50;
const ll Mod = 1e9 + 7;
int n, m, q, lt[M], rt[M], qu[Q], par[N], sz[N];
ll zp[Q], sum[Q];
vector<pair<int, pii>> edge;
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;
int u, v, w;
for(int i = 1; i <= m; i++){
cin >> u >> v >> w;
edge.pb({w, {u, v}});
}
sort(all(edge));
cin >> q;
for(int i = 0; i < q; i++) cin >> qu[i];
for(int i = 0; i < m; i++) rt[i] = m;
for(int i = 0; i < m; i++){
for(int j = 1; j <= n; j++){
sz[j] = 1; par[j] = j;
}
int u = edge[i].S.F;
int v = edge[i].S.S;
int w = edge[i].F;
lt[i] = -1;
for(int j = i-1; j >= 0; j--){
bool ch = Union(edge[j].S.F, edge[j].S.S);
if(ch && getPar(u) == getPar(v)){
lt[i] = j; break;
}
}
if(lt[i] != -1) rt[lt[i]] = i;
}
//for(int i = 0; i < m; i++) cout << lt[i] << sep << rt[i] << endl;
for(int i = 0; i < m; i++){
int p1 = 0;
if(lt[i] != -1) p1 = lower_bound(qu, qu + q, (edge[i].F + edge[lt[i]].F + 1)/2) - qu;
int p2 = lower_bound(qu, qu + q, edge[i].F) - qu;
int p3 = q;
if(rt[i] != m) p3 = lower_bound(qu, qu + q, (edge[i].F + edge[rt[i]].F - 1) / 2 + 1) - qu;
//cout << edge[i].F << sep << edge[i].S.F << sep << edge[i].S.S << sep << p1 << sep << p2 << sep << p3 << endl;
sum[p1] += edge[i].F;
sum[p2] -= 2*edge[i].F;
sum[p3] += edge[i].F;
zp[p1]--;
zp[p2] += 2;
zp[p3]--;
}
for(int i = 1; i < q; i++){
sum[i] += sum[i-1]; zp[i] += zp[i-1];
}
for(int i = 0; i < q; i++){
ll res = sum[i] + zp[i] * qu[i];
cout << res << endl;
//cout << res << sep << sum[i] << sep << zp[i] << endl;
}
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:70:13: warning: unused variable 'w' [-Wunused-variable]
70 | int w = edge[i].F;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
356 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 |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
352 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
356 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 |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
352 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
522 ms |
4144 KB |
Output is correct |
21 |
Correct |
410 ms |
4032 KB |
Output is correct |
22 |
Correct |
478 ms |
4244 KB |
Output is correct |
23 |
Correct |
482 ms |
4032 KB |
Output is correct |
24 |
Correct |
520 ms |
4056 KB |
Output is correct |
25 |
Correct |
542 ms |
4036 KB |
Output is correct |
26 |
Correct |
529 ms |
4196 KB |
Output is correct |
27 |
Correct |
548 ms |
4040 KB |
Output is correct |
28 |
Correct |
522 ms |
4132 KB |
Output is correct |
29 |
Correct |
403 ms |
4036 KB |
Output is correct |
30 |
Correct |
553 ms |
4260 KB |
Output is correct |
31 |
Correct |
529 ms |
4032 KB |
Output is correct |
32 |
Correct |
564 ms |
4208 KB |
Output is correct |
33 |
Correct |
512 ms |
4036 KB |
Output is correct |
34 |
Correct |
239 ms |
4292 KB |
Output is correct |
35 |
Correct |
597 ms |
4160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
593 ms |
44344 KB |
Output is correct |
5 |
Correct |
630 ms |
44452 KB |
Output is correct |
6 |
Correct |
580 ms |
44512 KB |
Output is correct |
7 |
Correct |
397 ms |
46276 KB |
Output is correct |
8 |
Correct |
330 ms |
46532 KB |
Output is correct |
9 |
Correct |
272 ms |
46372 KB |
Output is correct |
10 |
Correct |
593 ms |
44484 KB |
Output is correct |
11 |
Correct |
295 ms |
46448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
356 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 |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
352 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
133 ms |
41812 KB |
Output is correct |
21 |
Correct |
134 ms |
42068 KB |
Output is correct |
22 |
Correct |
137 ms |
41924 KB |
Output is correct |
23 |
Correct |
130 ms |
41916 KB |
Output is correct |
24 |
Correct |
144 ms |
41920 KB |
Output is correct |
25 |
Correct |
134 ms |
41932 KB |
Output is correct |
26 |
Correct |
126 ms |
41808 KB |
Output is correct |
27 |
Correct |
138 ms |
42048 KB |
Output is correct |
28 |
Correct |
131 ms |
41812 KB |
Output is correct |
29 |
Correct |
137 ms |
41840 KB |
Output is correct |
30 |
Correct |
129 ms |
42068 KB |
Output is correct |
31 |
Correct |
132 ms |
41816 KB |
Output is correct |
32 |
Correct |
147 ms |
42580 KB |
Output is correct |
33 |
Correct |
126 ms |
41816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
356 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 |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
352 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
522 ms |
4144 KB |
Output is correct |
21 |
Correct |
410 ms |
4032 KB |
Output is correct |
22 |
Correct |
478 ms |
4244 KB |
Output is correct |
23 |
Correct |
482 ms |
4032 KB |
Output is correct |
24 |
Correct |
520 ms |
4056 KB |
Output is correct |
25 |
Correct |
542 ms |
4036 KB |
Output is correct |
26 |
Correct |
529 ms |
4196 KB |
Output is correct |
27 |
Correct |
548 ms |
4040 KB |
Output is correct |
28 |
Correct |
522 ms |
4132 KB |
Output is correct |
29 |
Correct |
403 ms |
4036 KB |
Output is correct |
30 |
Correct |
553 ms |
4260 KB |
Output is correct |
31 |
Correct |
529 ms |
4032 KB |
Output is correct |
32 |
Correct |
564 ms |
4208 KB |
Output is correct |
33 |
Correct |
512 ms |
4036 KB |
Output is correct |
34 |
Correct |
239 ms |
4292 KB |
Output is correct |
35 |
Correct |
597 ms |
4160 KB |
Output is correct |
36 |
Correct |
562 ms |
5028 KB |
Output is correct |
37 |
Correct |
422 ms |
5004 KB |
Output is correct |
38 |
Correct |
444 ms |
4928 KB |
Output is correct |
39 |
Correct |
534 ms |
5060 KB |
Output is correct |
40 |
Correct |
523 ms |
4948 KB |
Output is correct |
41 |
Correct |
560 ms |
4948 KB |
Output is correct |
42 |
Correct |
566 ms |
4808 KB |
Output is correct |
43 |
Correct |
568 ms |
4924 KB |
Output is correct |
44 |
Correct |
529 ms |
4864 KB |
Output is correct |
45 |
Correct |
411 ms |
4980 KB |
Output is correct |
46 |
Correct |
591 ms |
4940 KB |
Output is correct |
47 |
Correct |
528 ms |
4860 KB |
Output is correct |
48 |
Correct |
545 ms |
4936 KB |
Output is correct |
49 |
Correct |
602 ms |
4928 KB |
Output is correct |
50 |
Correct |
253 ms |
5080 KB |
Output is correct |
51 |
Correct |
544 ms |
4800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
356 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 |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
352 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
522 ms |
4144 KB |
Output is correct |
21 |
Correct |
410 ms |
4032 KB |
Output is correct |
22 |
Correct |
478 ms |
4244 KB |
Output is correct |
23 |
Correct |
482 ms |
4032 KB |
Output is correct |
24 |
Correct |
520 ms |
4056 KB |
Output is correct |
25 |
Correct |
542 ms |
4036 KB |
Output is correct |
26 |
Correct |
529 ms |
4196 KB |
Output is correct |
27 |
Correct |
548 ms |
4040 KB |
Output is correct |
28 |
Correct |
522 ms |
4132 KB |
Output is correct |
29 |
Correct |
403 ms |
4036 KB |
Output is correct |
30 |
Correct |
553 ms |
4260 KB |
Output is correct |
31 |
Correct |
529 ms |
4032 KB |
Output is correct |
32 |
Correct |
564 ms |
4208 KB |
Output is correct |
33 |
Correct |
512 ms |
4036 KB |
Output is correct |
34 |
Correct |
239 ms |
4292 KB |
Output is correct |
35 |
Correct |
597 ms |
4160 KB |
Output is correct |
36 |
Correct |
1 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 |
Correct |
593 ms |
44344 KB |
Output is correct |
40 |
Correct |
630 ms |
44452 KB |
Output is correct |
41 |
Correct |
580 ms |
44512 KB |
Output is correct |
42 |
Correct |
397 ms |
46276 KB |
Output is correct |
43 |
Correct |
330 ms |
46532 KB |
Output is correct |
44 |
Correct |
272 ms |
46372 KB |
Output is correct |
45 |
Correct |
593 ms |
44484 KB |
Output is correct |
46 |
Correct |
295 ms |
46448 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
133 ms |
41812 KB |
Output is correct |
49 |
Correct |
134 ms |
42068 KB |
Output is correct |
50 |
Correct |
137 ms |
41924 KB |
Output is correct |
51 |
Correct |
130 ms |
41916 KB |
Output is correct |
52 |
Correct |
144 ms |
41920 KB |
Output is correct |
53 |
Correct |
134 ms |
41932 KB |
Output is correct |
54 |
Correct |
126 ms |
41808 KB |
Output is correct |
55 |
Correct |
138 ms |
42048 KB |
Output is correct |
56 |
Correct |
131 ms |
41812 KB |
Output is correct |
57 |
Correct |
137 ms |
41840 KB |
Output is correct |
58 |
Correct |
129 ms |
42068 KB |
Output is correct |
59 |
Correct |
132 ms |
41816 KB |
Output is correct |
60 |
Correct |
147 ms |
42580 KB |
Output is correct |
61 |
Correct |
126 ms |
41816 KB |
Output is correct |
62 |
Correct |
562 ms |
5028 KB |
Output is correct |
63 |
Correct |
422 ms |
5004 KB |
Output is correct |
64 |
Correct |
444 ms |
4928 KB |
Output is correct |
65 |
Correct |
534 ms |
5060 KB |
Output is correct |
66 |
Correct |
523 ms |
4948 KB |
Output is correct |
67 |
Correct |
560 ms |
4948 KB |
Output is correct |
68 |
Correct |
566 ms |
4808 KB |
Output is correct |
69 |
Correct |
568 ms |
4924 KB |
Output is correct |
70 |
Correct |
529 ms |
4864 KB |
Output is correct |
71 |
Correct |
411 ms |
4980 KB |
Output is correct |
72 |
Correct |
591 ms |
4940 KB |
Output is correct |
73 |
Correct |
528 ms |
4860 KB |
Output is correct |
74 |
Correct |
545 ms |
4936 KB |
Output is correct |
75 |
Correct |
602 ms |
4928 KB |
Output is correct |
76 |
Correct |
253 ms |
5080 KB |
Output is correct |
77 |
Correct |
544 ms |
4800 KB |
Output is correct |
78 |
Correct |
648 ms |
43360 KB |
Output is correct |
79 |
Correct |
543 ms |
45496 KB |
Output is correct |
80 |
Correct |
554 ms |
44392 KB |
Output is correct |
81 |
Correct |
633 ms |
44480 KB |
Output is correct |
82 |
Correct |
652 ms |
43536 KB |
Output is correct |
83 |
Correct |
654 ms |
43452 KB |
Output is correct |
84 |
Correct |
715 ms |
43488 KB |
Output is correct |
85 |
Correct |
672 ms |
43456 KB |
Output is correct |
86 |
Correct |
660 ms |
43552 KB |
Output is correct |
87 |
Correct |
516 ms |
44948 KB |
Output is correct |
88 |
Correct |
679 ms |
43396 KB |
Output is correct |
89 |
Correct |
703 ms |
43456 KB |
Output is correct |
90 |
Correct |
664 ms |
43604 KB |
Output is correct |
91 |
Correct |
690 ms |
43360 KB |
Output is correct |
92 |
Correct |
377 ms |
46280 KB |
Output is correct |
93 |
Correct |
677 ms |
44484 KB |
Output is correct |