This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define f first
#define s second
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define sz(x) (int)(x).size()
#define m_p make_pair
#define fast_prep ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const int N=501;
const ll inf=1e9+10;
int p[N],sz[N];
void make(int v){
p[v]=v;sz[v]=1;
}
int get(int v){
return p[v]=(p[v]==v?v:get(p[v]));
}
bool mg(int a,int b){
a=get(a),b=get(b);
if(a==b) return 0;
if(sz[a]<sz[b]) swap(a,b);
p[b]=a;sz[a]+=sz[b];
return 1;
}
signed main(){
fast_prep;
int n,m;
cin>>n>>m;
vec<array<int,3>> arr;
for(int i=0;i<m;i++){
int v,u,w;
cin>>v>>u>>w;--v;--u;
arr.pb({w,v,u});
}
sort(all(arr));
vec<array<ll,3>> change;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++) make(j);
int j=i+1;
while(j<m){
mg(arr[j][1],arr[j][2]);
if(get(arr[i][1])==get(arr[i][2])) break;
++j;
}
ll x=(j==m?inf+2:(arr[j][0]+arr[i][0]+1)/2);
///
change.pb({x,-1,arr[i][0]});
for(int j=0;j<n;j++) make(j);
j=i-1;
while(j>=0){
mg(arr[j][1],arr[j][2]);
if(get(arr[i][1])==get(arr[i][2])) break;
--j;
}
x=(j==-1?-inf:(arr[j][0]+arr[i][0]+1)/2);
change.pb({x,-1,arr[i][0]});
change.pb({arr[i][0],2,-2ll*arr[i][0]});
}
sort(all(change));
int q;
cin>>q;
ll a=0,b=0;
int j=0;
for(int i=0;i<q;i++){
int x;
cin>>x;
while(j<sz(change) && x>=change[j][0]){
a+=change[j][1];b+=change[j][2];
++j;
}
cout<<1ll*a*x+b<<'\n';
}
return 0;
}
/*
5 10
1 2 8
1 3 13
1 4 5
1 5 11
1 5 3
2 3 7
2 4 15
3 4 6
3 5 6
4 5 2
1
10
17
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |