#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;
for(int i=1;i<=m;i++){
if(r<sz(e)&&l>=0){
if(e[r].F-d<=d-e[l].F){
ans+=make(e[r].F-d,e[r].S);
r++;
}
else{
ans+=make(d-e[l].F,e[l].S);
l--;
}
}
else if(r<sz(e)){
ans+=make(e[r].F-d,e[r].S);
r++;
}
else{
ans+=make(d-e[l].F,e[l].S);
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();
}
# |
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 |
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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
1 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 |
# |
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 |
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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
1 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 |
344 KB |
Output is correct |
20 |
Correct |
28 ms |
2004 KB |
Output is correct |
21 |
Correct |
35 ms |
2004 KB |
Output is correct |
22 |
Correct |
29 ms |
2004 KB |
Output is correct |
23 |
Correct |
28 ms |
2004 KB |
Output is correct |
24 |
Correct |
26 ms |
2000 KB |
Output is correct |
25 |
Correct |
28 ms |
2004 KB |
Output is correct |
26 |
Correct |
26 ms |
2004 KB |
Output is correct |
27 |
Correct |
30 ms |
1956 KB |
Output is correct |
28 |
Correct |
28 ms |
2088 KB |
Output is correct |
29 |
Correct |
31 ms |
2004 KB |
Output is correct |
30 |
Correct |
27 ms |
2068 KB |
Output is correct |
31 |
Correct |
29 ms |
2004 KB |
Output is correct |
32 |
Correct |
32 ms |
2000 KB |
Output is correct |
33 |
Correct |
28 ms |
2088 KB |
Output is correct |
34 |
Correct |
24 ms |
1956 KB |
Output is correct |
35 |
Correct |
29 ms |
1944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
Execution timed out |
5097 ms |
2064 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 |
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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
1 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 |
5086 ms |
3052 KB |
Time limit exceeded |
21 |
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 |
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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
1 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 |
344 KB |
Output is correct |
20 |
Correct |
28 ms |
2004 KB |
Output is correct |
21 |
Correct |
35 ms |
2004 KB |
Output is correct |
22 |
Correct |
29 ms |
2004 KB |
Output is correct |
23 |
Correct |
28 ms |
2004 KB |
Output is correct |
24 |
Correct |
26 ms |
2000 KB |
Output is correct |
25 |
Correct |
28 ms |
2004 KB |
Output is correct |
26 |
Correct |
26 ms |
2004 KB |
Output is correct |
27 |
Correct |
30 ms |
1956 KB |
Output is correct |
28 |
Correct |
28 ms |
2088 KB |
Output is correct |
29 |
Correct |
31 ms |
2004 KB |
Output is correct |
30 |
Correct |
27 ms |
2068 KB |
Output is correct |
31 |
Correct |
29 ms |
2004 KB |
Output is correct |
32 |
Correct |
32 ms |
2000 KB |
Output is correct |
33 |
Correct |
28 ms |
2088 KB |
Output is correct |
34 |
Correct |
24 ms |
1956 KB |
Output is correct |
35 |
Correct |
29 ms |
1944 KB |
Output is correct |
36 |
Execution timed out |
5056 ms |
2004 KB |
Time limit exceeded |
37 |
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 |
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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
1 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 |
344 KB |
Output is correct |
20 |
Correct |
28 ms |
2004 KB |
Output is correct |
21 |
Correct |
35 ms |
2004 KB |
Output is correct |
22 |
Correct |
29 ms |
2004 KB |
Output is correct |
23 |
Correct |
28 ms |
2004 KB |
Output is correct |
24 |
Correct |
26 ms |
2000 KB |
Output is correct |
25 |
Correct |
28 ms |
2004 KB |
Output is correct |
26 |
Correct |
26 ms |
2004 KB |
Output is correct |
27 |
Correct |
30 ms |
1956 KB |
Output is correct |
28 |
Correct |
28 ms |
2088 KB |
Output is correct |
29 |
Correct |
31 ms |
2004 KB |
Output is correct |
30 |
Correct |
27 ms |
2068 KB |
Output is correct |
31 |
Correct |
29 ms |
2004 KB |
Output is correct |
32 |
Correct |
32 ms |
2000 KB |
Output is correct |
33 |
Correct |
28 ms |
2088 KB |
Output is correct |
34 |
Correct |
24 ms |
1956 KB |
Output is correct |
35 |
Correct |
29 ms |
1944 KB |
Output is correct |
36 |
Correct |
0 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 |
5097 ms |
2064 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |