#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int,int>
#define fi first
#define se second
#define endl '\n'
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound
#define rep(x,s,e) for (int x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e))?x++:x--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct UFDS{
int p[505];
int r[505];
void reset(){ rep(x,0,505) p[x]=x,r[x]=0; }
int par(int i){
if (p[i]==i) return i;
else return p[i]=par(p[i]);
}
bool unions(int i,int j){
i=par(i),j=par(j);
if (i==j) return false;
if (r[i]>r[j]) swap(i,j);
p[i]=j;
if (r[i]==r[j]) r[j]++;
return true;
}
} ufds;
int n,m,q;
struct E{
int u,v;
int w;
};
E ee[100005];
int qu[1000005];
int split[1000005];
int ans[1000005];
void solve(int l,int r,int optl,int optr,vector<int> vl,vector<int> vr){
int m=l+r>>1;
//[optl,split[m]-1]
//[split[m],optr]
vector<int> tl=vl,tr=vr;
vector<int> vl2,vr2;
rep(x,optl,split[m]) tl.pub(x);
rep(x,optr+1,split[m]) tr.pub(x);
//cout<<"debug: "<<l<<" "<<m<<" "<<r<<" "<<optl<<" "<<optr<<endl;
//for (auto it:tl) cout<<it<<" "; cout<<endl;
//for (auto it:tr) cout<<it<<" "; cout<<endl;
ufds.reset();
while (sz(vl2)+sz(vr2)!=n-1){ //mst is not complete
if (tr.empty() ||
(!tl.empty() && qu[m]-ee[tl.back()].w<=ee[tr.back()].w-qu[m])){
if (ufds.unions(ee[tl.back()].u,ee[tl.back()].v)){
ans[m]+=qu[m]-ee[tl.back()].w;
vl2.pub(tl.back());
}
tl.pob();
}
else{
if (ufds.unions(ee[tr.back()].u,ee[tr.back()].v)){
ans[m]+=ee[tr.back()].w-qu[m];
vr2.pub(tr.back());
}
tr.pob();
}
}
reverse(all(vl2));
reverse(all(vr2));
//for (auto it:vl2) cout<<it<<" "; cout<<endl;
//for (auto it:vr2) cout<<it<<" "; cout<<endl;
if (l!=m) solve(l,m-1,optl,split[m]-1,vl,vr2);
if (m!=r) solve(m+1,r,split[m],optr,vl2,vr);
}
signed main(){
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(false);
cin>>n>>m;
rep(x,0,m) cin>>ee[x].u>>ee[x].v>>ee[x].w;
sort(ee,ee+m,[](E i,E j){
return i.w<j.w;
});
cin>>q;
rep(x,0,q) cin>>qu[x];
split[0]=0;
rep(x,0,q){
while (split[x]<m && ee[split[x]].w<=qu[x]) split[x]++;
split[x+1]=split[x];
}
//rep(x,0,m) cout<<x<<" "; cout<<endl;
//rep(x,0,m) cout<<ee[x].u<<" "; cout<<endl;
//rep(x,0,m) cout<<ee[x].v<<" "; cout<<endl;
//rep(x,0,m) cout<<ee[x].w<<" "; cout<<endl;
//cout<<endl;
//rep(x,0,q) cout<<qu[x]<<" "; cout<<endl;
//rep(x,0,q) cout<<split[x]<<" "; cout<<endl;
solve(0,q-1,0,m-1,{},{});
rep(x,0,q) cout<<ans[x]<<endl;
}
Compilation message
reconstruction.cpp: In function 'void solve(long long int, long long int, long long int, long long int, std::vector<long long int>, std::vector<long long int>)':
reconstruction.cpp:59:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
59 | int m=l+r>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
33 ms |
5260 KB |
Output is correct |
21 |
Correct |
45 ms |
4944 KB |
Output is correct |
22 |
Correct |
42 ms |
4748 KB |
Output is correct |
23 |
Correct |
35 ms |
5136 KB |
Output is correct |
24 |
Correct |
32 ms |
5060 KB |
Output is correct |
25 |
Correct |
35 ms |
5500 KB |
Output is correct |
26 |
Correct |
33 ms |
4676 KB |
Output is correct |
27 |
Correct |
35 ms |
4744 KB |
Output is correct |
28 |
Correct |
33 ms |
4728 KB |
Output is correct |
29 |
Correct |
28 ms |
5564 KB |
Output is correct |
30 |
Correct |
32 ms |
5496 KB |
Output is correct |
31 |
Correct |
34 ms |
4592 KB |
Output is correct |
32 |
Correct |
35 ms |
5200 KB |
Output is correct |
33 |
Correct |
36 ms |
5744 KB |
Output is correct |
34 |
Correct |
33 ms |
6528 KB |
Output is correct |
35 |
Correct |
36 ms |
5360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Execution timed out |
5025 ms |
23784 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Execution timed out |
5056 ms |
21368 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
33 ms |
5260 KB |
Output is correct |
21 |
Correct |
45 ms |
4944 KB |
Output is correct |
22 |
Correct |
42 ms |
4748 KB |
Output is correct |
23 |
Correct |
35 ms |
5136 KB |
Output is correct |
24 |
Correct |
32 ms |
5060 KB |
Output is correct |
25 |
Correct |
35 ms |
5500 KB |
Output is correct |
26 |
Correct |
33 ms |
4676 KB |
Output is correct |
27 |
Correct |
35 ms |
4744 KB |
Output is correct |
28 |
Correct |
33 ms |
4728 KB |
Output is correct |
29 |
Correct |
28 ms |
5564 KB |
Output is correct |
30 |
Correct |
32 ms |
5496 KB |
Output is correct |
31 |
Correct |
34 ms |
4592 KB |
Output is correct |
32 |
Correct |
35 ms |
5200 KB |
Output is correct |
33 |
Correct |
36 ms |
5744 KB |
Output is correct |
34 |
Correct |
33 ms |
6528 KB |
Output is correct |
35 |
Correct |
36 ms |
5360 KB |
Output is correct |
36 |
Correct |
413 ms |
5100 KB |
Output is correct |
37 |
Correct |
384 ms |
5380 KB |
Output is correct |
38 |
Correct |
442 ms |
5176 KB |
Output is correct |
39 |
Correct |
394 ms |
5244 KB |
Output is correct |
40 |
Correct |
425 ms |
5264 KB |
Output is correct |
41 |
Correct |
407 ms |
5296 KB |
Output is correct |
42 |
Correct |
403 ms |
5244 KB |
Output is correct |
43 |
Correct |
429 ms |
5124 KB |
Output is correct |
44 |
Correct |
292 ms |
5156 KB |
Output is correct |
45 |
Correct |
219 ms |
6004 KB |
Output is correct |
46 |
Correct |
388 ms |
5192 KB |
Output is correct |
47 |
Correct |
401 ms |
5184 KB |
Output is correct |
48 |
Correct |
407 ms |
10056 KB |
Output is correct |
49 |
Correct |
387 ms |
10880 KB |
Output is correct |
50 |
Correct |
216 ms |
15748 KB |
Output is correct |
51 |
Correct |
213 ms |
10216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
33 ms |
5260 KB |
Output is correct |
21 |
Correct |
45 ms |
4944 KB |
Output is correct |
22 |
Correct |
42 ms |
4748 KB |
Output is correct |
23 |
Correct |
35 ms |
5136 KB |
Output is correct |
24 |
Correct |
32 ms |
5060 KB |
Output is correct |
25 |
Correct |
35 ms |
5500 KB |
Output is correct |
26 |
Correct |
33 ms |
4676 KB |
Output is correct |
27 |
Correct |
35 ms |
4744 KB |
Output is correct |
28 |
Correct |
33 ms |
4728 KB |
Output is correct |
29 |
Correct |
28 ms |
5564 KB |
Output is correct |
30 |
Correct |
32 ms |
5496 KB |
Output is correct |
31 |
Correct |
34 ms |
4592 KB |
Output is correct |
32 |
Correct |
35 ms |
5200 KB |
Output is correct |
33 |
Correct |
36 ms |
5744 KB |
Output is correct |
34 |
Correct |
33 ms |
6528 KB |
Output is correct |
35 |
Correct |
36 ms |
5360 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Execution timed out |
5025 ms |
23784 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |