#include<bits/stdc++.h>
#define int long long
using namespace std;
vector<pair<int,pair<int,int>>>edge;
vector<int>qr;
vector<int>ans;
vector<int>order;
vector<int>rorder;
int p[505];
int n,m;
int fp(int a){return p[a]==a?a:p[a]=fp(p[a]);}
void un(int a,int b){p[fp(b)]=fp(a);}
int get_res(int x){
//cerr<<"x:"<<x<<"\n";
int r=lower_bound(edge.begin(),edge.end(),make_pair(x,make_pair(0LL,0LL)))-edge.begin();
int l=r-1;
int ans=0;
while(l>=0&&r<m){
while(l>=0&&(fp(edge[l].second.first)==fp(edge[l].second.second)))l--;
while(r<m&&(fp(edge[r].second.first)==fp(edge[r].second.second)))r++;
if(l<0||r>=m)break;
if(abs(x-edge[l].first)<=abs(x-edge[r].first))/*cerr<<edge[l].second.first<<" "<<edge[l].second.second<<" "<<abs(x-edge[l].first)<<"\n",*/un(edge[l].second.first,edge[l].second.second),ans+=abs(x-edge[l].first),l--;
else /*cerr<<edge[r].second.first<<" "<<edge[r].second.second<<" "<<abs(x-edge[r].first)<<"\n",*/un(edge[r].second.first,edge[r].second.second),ans+=abs(x-edge[r].first),r++;
}
while(l>=0){
if(fp(edge[l].second.first)!=fp(edge[l].second.second))/*cerr<<edge[l].second.first<<" "<<edge[l].second.second<<" "<<abs(x-edge[l].first)<<"\n",*/un(edge[l].second.first,edge[l].second.second),ans+=abs(x-edge[l].first);
l--;
}
while(r<m){
if(fp(edge[r].second.first)!=fp(edge[r].second.second))/*cerr<<edge[r].second.first<<" "<<edge[r].second.second<<" "<<abs(x-edge[r].first)<<"\n",*/un(edge[r].second.first,edge[r].second.second),ans+=abs(x-edge[r].first);
r++;
}
return ans;
}
vector<int>v;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b,w;cin>>a>>b>>w;
edge.push_back({w,{a,b}});
order.push_back(w);
}
sort(edge.begin(),edge.end());
int q;cin>>q;
for(int i=0;i<q;i++){
int x;cin>>x;
qr.push_back(x);
}
for(auto x:qr){
for(int i=1;i<=n;i++)p[i]=i;
cout<<get_res(x)<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
460 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 |
460 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 |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
460 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 |
460 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 |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
26 ms |
6112 KB |
Output is correct |
21 |
Correct |
31 ms |
6152 KB |
Output is correct |
22 |
Correct |
27 ms |
6156 KB |
Output is correct |
23 |
Correct |
35 ms |
6320 KB |
Output is correct |
24 |
Correct |
27 ms |
6148 KB |
Output is correct |
25 |
Correct |
26 ms |
6156 KB |
Output is correct |
26 |
Correct |
27 ms |
6288 KB |
Output is correct |
27 |
Correct |
27 ms |
6152 KB |
Output is correct |
28 |
Correct |
30 ms |
6324 KB |
Output is correct |
29 |
Correct |
37 ms |
6156 KB |
Output is correct |
30 |
Correct |
28 ms |
6108 KB |
Output is correct |
31 |
Correct |
27 ms |
6148 KB |
Output is correct |
32 |
Correct |
26 ms |
6228 KB |
Output is correct |
33 |
Correct |
27 ms |
6148 KB |
Output is correct |
34 |
Correct |
30 ms |
6664 KB |
Output is correct |
35 |
Correct |
26 ms |
6156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5069 ms |
25020 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
460 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 |
460 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 |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
5080 ms |
26280 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
460 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 |
460 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 |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
26 ms |
6112 KB |
Output is correct |
21 |
Correct |
31 ms |
6152 KB |
Output is correct |
22 |
Correct |
27 ms |
6156 KB |
Output is correct |
23 |
Correct |
35 ms |
6320 KB |
Output is correct |
24 |
Correct |
27 ms |
6148 KB |
Output is correct |
25 |
Correct |
26 ms |
6156 KB |
Output is correct |
26 |
Correct |
27 ms |
6288 KB |
Output is correct |
27 |
Correct |
27 ms |
6152 KB |
Output is correct |
28 |
Correct |
30 ms |
6324 KB |
Output is correct |
29 |
Correct |
37 ms |
6156 KB |
Output is correct |
30 |
Correct |
28 ms |
6108 KB |
Output is correct |
31 |
Correct |
27 ms |
6148 KB |
Output is correct |
32 |
Correct |
26 ms |
6228 KB |
Output is correct |
33 |
Correct |
27 ms |
6148 KB |
Output is correct |
34 |
Correct |
30 ms |
6664 KB |
Output is correct |
35 |
Correct |
26 ms |
6156 KB |
Output is correct |
36 |
Correct |
4273 ms |
6788 KB |
Output is correct |
37 |
Execution timed out |
5038 ms |
6516 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
460 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 |
460 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 |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
26 ms |
6112 KB |
Output is correct |
21 |
Correct |
31 ms |
6152 KB |
Output is correct |
22 |
Correct |
27 ms |
6156 KB |
Output is correct |
23 |
Correct |
35 ms |
6320 KB |
Output is correct |
24 |
Correct |
27 ms |
6148 KB |
Output is correct |
25 |
Correct |
26 ms |
6156 KB |
Output is correct |
26 |
Correct |
27 ms |
6288 KB |
Output is correct |
27 |
Correct |
27 ms |
6152 KB |
Output is correct |
28 |
Correct |
30 ms |
6324 KB |
Output is correct |
29 |
Correct |
37 ms |
6156 KB |
Output is correct |
30 |
Correct |
28 ms |
6108 KB |
Output is correct |
31 |
Correct |
27 ms |
6148 KB |
Output is correct |
32 |
Correct |
26 ms |
6228 KB |
Output is correct |
33 |
Correct |
27 ms |
6148 KB |
Output is correct |
34 |
Correct |
30 ms |
6664 KB |
Output is correct |
35 |
Correct |
26 ms |
6156 KB |
Output is correct |
36 |
Correct |
0 ms |
352 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Execution timed out |
5069 ms |
25020 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |