#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;
#define pb push_back
#define all(v) v.begin(),v.end()
#define sz(s) (int)s.size()
#define ppb pop_back
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define lb lower_bound
// #define int ll
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAX=2e5+10;
int n,m;
struct DSU{
int f[MAX];
void init(int n){
for(int i=1;i<=n;i++)f[i]=i;
}
int get(int v){
return (f[v]==v?v:f[v]=get(f[v]));
}
void unite(int a,int b){
a=get(a);
b=get(b);
f[a]=b;
}
}D;
vector<int> L[MAX],R[MAX];
vector<pair<int,pii>> e;
ll solve(int d){
int pos=lb(all(e),make_pair(d,make_pair(0,0)))-e.begin();
int p1=0,p2=0;
ll ans=0;
D.init(n);
// cerr<<sz(L[pos])<<" "<<sz(R[pos])<<"\n";
// for(auto x:L[pos])cout<<e[x].F<<" "<<e[x].S.F<<" "<<e[x].S.S<<"\n";
// for(auto x:R[pos])cout<<e[x].F<<" "<<e[x].S.F<<" "<<e[x].S.S<<"\n";
for(int c=0;c<n-1;){
if((p2<sz(R[pos])&&p1<sz(L[pos])&&d-e[L[pos][p1]].F<e[R[pos][p2]].F-d)||p2>=sz(R[pos])){
if(D.get(e[L[pos][p1]].S.F)!=D.get(e[L[pos][p1]].S.S)){
D.unite(e[L[pos][p1]].S.F,e[L[pos][p1]].S.S);
ans+=d-e[L[pos][p1]].F;
c++;
}
p1++;
}
else{
if(D.get(e[R[pos][p2]].S.F)!=D.get(e[R[pos][p2]].S.S)){
D.unite(e[R[pos][p2]].S.F,e[R[pos][p2]].S.S);
ans+=e[R[pos][p2]].F-d;
c++;
}
p2++;
}
}
return ans;
}
void solve(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int a,b,c;
cin>>a>>b>>c;
e.pb({c,{a,b}});
}
sort(all(e));
for(int i=0;i<=m;i++){
D.init(n);
for(int j=i-1;j>=0;j--){
if(D.get(e[j].S.F)!=D.get(e[j].S.S)){
L[i].pb(j);
D.unite(e[j].S.F,e[j].S.S);
}
}
}
for(int i=0;i<=m;i++){
D.init(n);
for(int j=i;j<m;j++){
if(D.get(e[j].S.F)!=D.get(e[j].S.S)){
R[i].pb(j);
D.unite(e[j].S.F,e[j].S.S);
}
}
}
int q;
cin>>q;
while(q--){
int d;
cin>>d;
solve(d);
// cout<<solve(d)<<"\n";
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
int init=clock();
while(t--)solve();
// #ifdef LOCAL
// cout.precision(10);
// cout<<fixed<<1.0*(clock()-init)/CLOCKS_PER_SEC<<"\n";
// #endif
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:117:9: warning: unused variable 'init' [-Wunused-variable]
117 | int init=clock();
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |