#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
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;
vector<pair<int,pii>> e;
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);
if(rng()%2)swap(a,b);
f[a]=b;
}
}D;
int make(int a,pii x){
if(D.get(x.F)!=D.get(x.S)){
D.unite(x.F,x.S);
return a;
}
return 0;
}
ll solve(int d){
int r=lb(all(e),make_pair(d,make_pair(0,0)))-e.begin();
int l=r-1;
D.init(n);
ll ans=0;
int cnt=0;
for(int i=1;i<=m,cnt<n-1;i++){
if((r<sz(e)&&l>=0&&e[r].F-d<=d-e[l].F)||l<0){
if(D.get(e[r].S.F)!=D.get(e[r].S.S)){
ans+=e[r].F-d;
D.unite(e[r].S.F,e[r].S.S);
cnt++;
}
r++;
}
else{
if(D.get(e[l].S.F)!=D.get(e[l].S.S)){
ans+=d-e[l].F;
D.unite(e[l].S.F,e[l].S.S);
cnt++;
}
l--;
}
}
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));
// cout<<e[0].F<<" "<<e[1].F<<"\n";
int q;
cin>>q;
while(q--){
int d;
cin>>d;
cout<<solve(d)<<"\n";
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--)solve();
}
Compilation message
reconstruction.cpp: In function 'long long int solve(int)':
reconstruction.cpp:58:18: warning: left operand of comma operator has no effect [-Wunused-value]
58 | for(int i=1;i<=m,cnt<n-1;i++){
| ~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
604 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 |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
604 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 |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
23 ms |
3284 KB |
Output is correct |
21 |
Correct |
29 ms |
3280 KB |
Output is correct |
22 |
Correct |
26 ms |
3492 KB |
Output is correct |
23 |
Correct |
24 ms |
3472 KB |
Output is correct |
24 |
Correct |
23 ms |
3416 KB |
Output is correct |
25 |
Correct |
23 ms |
3284 KB |
Output is correct |
26 |
Correct |
23 ms |
3284 KB |
Output is correct |
27 |
Correct |
22 ms |
3280 KB |
Output is correct |
28 |
Correct |
30 ms |
3264 KB |
Output is correct |
29 |
Correct |
27 ms |
3284 KB |
Output is correct |
30 |
Correct |
22 ms |
3280 KB |
Output is correct |
31 |
Correct |
24 ms |
3408 KB |
Output is correct |
32 |
Correct |
22 ms |
3280 KB |
Output is correct |
33 |
Correct |
23 ms |
3284 KB |
Output is correct |
34 |
Correct |
25 ms |
3540 KB |
Output is correct |
35 |
Correct |
22 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
5054 ms |
5620 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
604 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 |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
5080 ms |
5276 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
604 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 |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
23 ms |
3284 KB |
Output is correct |
21 |
Correct |
29 ms |
3280 KB |
Output is correct |
22 |
Correct |
26 ms |
3492 KB |
Output is correct |
23 |
Correct |
24 ms |
3472 KB |
Output is correct |
24 |
Correct |
23 ms |
3416 KB |
Output is correct |
25 |
Correct |
23 ms |
3284 KB |
Output is correct |
26 |
Correct |
23 ms |
3284 KB |
Output is correct |
27 |
Correct |
22 ms |
3280 KB |
Output is correct |
28 |
Correct |
30 ms |
3264 KB |
Output is correct |
29 |
Correct |
27 ms |
3284 KB |
Output is correct |
30 |
Correct |
22 ms |
3280 KB |
Output is correct |
31 |
Correct |
24 ms |
3408 KB |
Output is correct |
32 |
Correct |
22 ms |
3280 KB |
Output is correct |
33 |
Correct |
23 ms |
3284 KB |
Output is correct |
34 |
Correct |
25 ms |
3540 KB |
Output is correct |
35 |
Correct |
22 ms |
3420 KB |
Output is correct |
36 |
Correct |
886 ms |
3680 KB |
Output is correct |
37 |
Execution timed out |
5063 ms |
3740 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
604 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 |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
23 ms |
3284 KB |
Output is correct |
21 |
Correct |
29 ms |
3280 KB |
Output is correct |
22 |
Correct |
26 ms |
3492 KB |
Output is correct |
23 |
Correct |
24 ms |
3472 KB |
Output is correct |
24 |
Correct |
23 ms |
3416 KB |
Output is correct |
25 |
Correct |
23 ms |
3284 KB |
Output is correct |
26 |
Correct |
23 ms |
3284 KB |
Output is correct |
27 |
Correct |
22 ms |
3280 KB |
Output is correct |
28 |
Correct |
30 ms |
3264 KB |
Output is correct |
29 |
Correct |
27 ms |
3284 KB |
Output is correct |
30 |
Correct |
22 ms |
3280 KB |
Output is correct |
31 |
Correct |
24 ms |
3408 KB |
Output is correct |
32 |
Correct |
22 ms |
3280 KB |
Output is correct |
33 |
Correct |
23 ms |
3284 KB |
Output is correct |
34 |
Correct |
25 ms |
3540 KB |
Output is correct |
35 |
Correct |
22 ms |
3420 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 |
5054 ms |
5620 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |