#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
#define arr3 array<int, 3>
const int inf = 1e9 + 5;
const int N = 5e2;
struct dsu1{
int p[N + 1], n;
vector<int> ed;
ll W;
dsu1(int ns){
n = ns;
}
int get(int v){
if (p[v] != v){
p[v] = get(p[v]);
}
return p[v];
}
void unite(int x, int y, int w){
x = get(x); y = get(y);
if (x == y) return;
p[x] = y;
W += w;
ed.pb(w);
}
void clear(){
for (int i = 1; i <= n; i++){
p[i] = i;
}
ed.clear();
W = 0;
}
};
struct dsu{
int p[N + 1], n;
ll W;
dsu(int ns){
n = ns;
}
int get(int v){
if (p[v] != v){
p[v] = get(p[v]);
}
return p[v];
}
void unite(int x, int y, int w){
x = get(x); y = get(y);
if (x == y) return;
p[x] = y;
W += w;
}
void clear(){
for (int i = 1; i <= n; i++){
p[i] = i;
}
W = 0;
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m; cin>>n>>m;
vector<arr3> ed = {{0, 0, 0}};
for (int i = 1; i <= m; i++){
int a, b, w; cin>>a>>b>>w;
ed.pb({a, b, w});
}
ed.pb({0, 0, inf});
auto cmp = [&](arr3& x, arr3& y){
return x[2] < y[2];
};
sort(ed.begin(), ed.end(), cmp);
vector<int> actl[m + 2];
dsu1 T1(n);
for (int i = 1; i <= m; i++){
T1.clear();
T1.unite(ed[i][0], ed[i][1], i);
for (int j: actl[i - 1]){
T1.unite(ed[j][0], ed[j][1], j);
}
actl[i] = T1.ed;
}
vector<int> actr[m + 2];
for (int i = m; i > 0; i--){
T1.clear();
T1.unite(ed[i][0], ed[i][1], i);
for (int j: actr[i + 1]){
T1.unite(ed[j][0], ed[j][1], j);
}
actr[i] = T1.ed;
}
int q; cin>>q;
vector<pii> qs;
for (int i = 1; i <= q; i++){
int x; cin>>x;
qs.pb({x, i});
}
sort(qs.begin(), qs.end());
vector<ll> out(q + 1);
int j1 = 0;
dsu T(n);
for (int i = 0; i <= m; i++){
vector<int>& edg1 = actl[i], edg2 = actr[i + 1];
int l = ed[i][2], r = ed[i + 1][2] - 1;
int j2 = j1;
while (j2 < qs.size() && l <= qs[j2].ff && qs[j2].ff <= r){
j2++;
}
while (j1 < j2){
int x = qs[j1].ff;
int i1 = 0, i2 = 0;
T.clear();
while (i1 < edg1.size() || i2 < edg2.size()){
if (i1 == edg1.size()){
int j = edg2[i2];
T.unite(ed[j][0], ed[j][1], abs(ed[j][2] - x));
i2++;
continue;
}
if (i2 == edg2.size()){
int j = edg1[i1];
T.unite(ed[j][0], ed[j][1], abs(ed[j][2] - x));
i1++;
continue;
}
int v1 = abs(ed[edg1[i1]][2] - x);
int v2 = abs(ed[edg2[i2]][2] - x);
if (v1 < v2){
int j = edg1[i1];
T.unite(ed[j][0], ed[j][1], v1);
i1++;
}
else {
int j = edg2[i2];
T.unite(ed[j][0], ed[j][1], v2);
i2++;
}
}
out[qs[j1].ss] = T.W;
j1++;
}
}
for (int i = 1; i <= q; i++) cout<<out[i]<<"\n";
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:125:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | while (j2 < qs.size() && l <= qs[j2].ff && qs[j2].ff <= r){
| ~~~^~~~~~~~~~~
reconstruction.cpp:132:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | while (i1 < edg1.size() || i2 < edg2.size()){
| ~~~^~~~~~~~~~~~~
reconstruction.cpp:132:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | while (i1 < edg1.size() || i2 < edg2.size()){
| ~~~^~~~~~~~~~~~~
reconstruction.cpp:133:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | if (i1 == edg1.size()){
| ~~~^~~~~~~~~~~~~~
reconstruction.cpp:139:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | if (i2 == edg2.size()){
| ~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
0 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 |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
0 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 |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1150 ms |
401328 KB |
Output is correct |
21 |
Correct |
646 ms |
379588 KB |
Output is correct |
22 |
Correct |
738 ms |
396172 KB |
Output is correct |
23 |
Correct |
896 ms |
400320 KB |
Output is correct |
24 |
Correct |
895 ms |
400832 KB |
Output is correct |
25 |
Correct |
1303 ms |
401096 KB |
Output is correct |
26 |
Correct |
1187 ms |
401376 KB |
Output is correct |
27 |
Correct |
1216 ms |
401140 KB |
Output is correct |
28 |
Correct |
1157 ms |
401212 KB |
Output is correct |
29 |
Correct |
1154 ms |
401348 KB |
Output is correct |
30 |
Correct |
1177 ms |
401348 KB |
Output is correct |
31 |
Correct |
1160 ms |
401348 KB |
Output is correct |
32 |
Correct |
1156 ms |
401348 KB |
Output is correct |
33 |
Correct |
1175 ms |
401260 KB |
Output is correct |
34 |
Correct |
1178 ms |
401444 KB |
Output is correct |
35 |
Correct |
1216 ms |
401348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 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 |
5111 ms |
426672 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
0 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 |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
4823 ms |
40848 KB |
Output is correct |
21 |
Correct |
4385 ms |
40724 KB |
Output is correct |
22 |
Correct |
4869 ms |
41192 KB |
Output is correct |
23 |
Correct |
4736 ms |
40888 KB |
Output is correct |
24 |
Correct |
4891 ms |
40852 KB |
Output is correct |
25 |
Correct |
4916 ms |
40844 KB |
Output is correct |
26 |
Correct |
4729 ms |
40952 KB |
Output is correct |
27 |
Correct |
4883 ms |
40876 KB |
Output is correct |
28 |
Correct |
4797 ms |
40876 KB |
Output is correct |
29 |
Correct |
4922 ms |
40852 KB |
Output is correct |
30 |
Correct |
4893 ms |
41216 KB |
Output is correct |
31 |
Execution timed out |
5018 ms |
40740 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
0 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 |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1150 ms |
401328 KB |
Output is correct |
21 |
Correct |
646 ms |
379588 KB |
Output is correct |
22 |
Correct |
738 ms |
396172 KB |
Output is correct |
23 |
Correct |
896 ms |
400320 KB |
Output is correct |
24 |
Correct |
895 ms |
400832 KB |
Output is correct |
25 |
Correct |
1303 ms |
401096 KB |
Output is correct |
26 |
Correct |
1187 ms |
401376 KB |
Output is correct |
27 |
Correct |
1216 ms |
401140 KB |
Output is correct |
28 |
Correct |
1157 ms |
401212 KB |
Output is correct |
29 |
Correct |
1154 ms |
401348 KB |
Output is correct |
30 |
Correct |
1177 ms |
401348 KB |
Output is correct |
31 |
Correct |
1160 ms |
401348 KB |
Output is correct |
32 |
Correct |
1156 ms |
401348 KB |
Output is correct |
33 |
Correct |
1175 ms |
401260 KB |
Output is correct |
34 |
Correct |
1178 ms |
401444 KB |
Output is correct |
35 |
Correct |
1216 ms |
401348 KB |
Output is correct |
36 |
Correct |
1708 ms |
402112 KB |
Output is correct |
37 |
Correct |
1006 ms |
380608 KB |
Output is correct |
38 |
Correct |
1093 ms |
397064 KB |
Output is correct |
39 |
Correct |
1260 ms |
401008 KB |
Output is correct |
40 |
Correct |
1449 ms |
401760 KB |
Output is correct |
41 |
Correct |
1833 ms |
402032 KB |
Output is correct |
42 |
Correct |
1725 ms |
402072 KB |
Output is correct |
43 |
Correct |
1697 ms |
401952 KB |
Output is correct |
44 |
Correct |
1329 ms |
401996 KB |
Output is correct |
45 |
Correct |
1280 ms |
402236 KB |
Output is correct |
46 |
Correct |
1732 ms |
402120 KB |
Output is correct |
47 |
Correct |
1702 ms |
402012 KB |
Output is correct |
48 |
Correct |
1696 ms |
401972 KB |
Output is correct |
49 |
Correct |
1673 ms |
401932 KB |
Output is correct |
50 |
Correct |
1258 ms |
402216 KB |
Output is correct |
51 |
Correct |
1281 ms |
401948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
0 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 |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1150 ms |
401328 KB |
Output is correct |
21 |
Correct |
646 ms |
379588 KB |
Output is correct |
22 |
Correct |
738 ms |
396172 KB |
Output is correct |
23 |
Correct |
896 ms |
400320 KB |
Output is correct |
24 |
Correct |
895 ms |
400832 KB |
Output is correct |
25 |
Correct |
1303 ms |
401096 KB |
Output is correct |
26 |
Correct |
1187 ms |
401376 KB |
Output is correct |
27 |
Correct |
1216 ms |
401140 KB |
Output is correct |
28 |
Correct |
1157 ms |
401212 KB |
Output is correct |
29 |
Correct |
1154 ms |
401348 KB |
Output is correct |
30 |
Correct |
1177 ms |
401348 KB |
Output is correct |
31 |
Correct |
1160 ms |
401348 KB |
Output is correct |
32 |
Correct |
1156 ms |
401348 KB |
Output is correct |
33 |
Correct |
1175 ms |
401260 KB |
Output is correct |
34 |
Correct |
1178 ms |
401444 KB |
Output is correct |
35 |
Correct |
1216 ms |
401348 KB |
Output is correct |
36 |
Correct |
1 ms |
344 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 |
5111 ms |
426672 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |