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>
using namespace std;
const int maxn=505,maxm=100000+10;
struct yal{
int u,v,w,ind;
bool operator <(const yal a)const{
if(a.w!=w){
return w<a.w;
}
if(a.u!=u){
return u<a.u;
}
return v<a.v;
}
};
struct updy{
int l,val,ted;
updy(int a=0,int b=0,int c=0){
l=a;
val=b;
ted=c;
}
bool operator <(const updy a)const{
if(a.l!=l){
return l<a.l;
}
if(a.val!=val){
return val<a.val;
}
return ted<a.ted;
}
}fupdy;
struct dsu{
int par[maxn],sz[maxn];
void clear(){
memset(par,0,sizeof(par));
memset(sz,0,sizeof(sz));
}
int root(int u){
if(par[u]==0){
return u;
}
return par[u]=root(par[u]);
}
int con(int u,int v){
u=root(u),v=root(v);
if(u!=v){
if(sz[u]<sz[v]){
swap(u,v);
}
par[v]=u;
sz[u]+=sz[v]+1;
return 1;
}
return 0;
}
}ds;
vector<updy>allq;
vector<yal>alle,v;
int n,m;
int upd(){
ds.clear();
for(int i=(int)v.size()-1;i>=0;i--){
// cout<<"ha: "<<v[i].u<<" "<<v[i].v<<" "<<v[i].ind<<"\n";
if(ds.con(v[i].u,v[i].v)==0){
// cout<<"nababa"<<endl;
int ret=v[i].ind;
v.erase(v.begin()+i);
return ret;
}
}
return -1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
alle.resize(m);
for(int i=0;i<m;i++){
cin>>alle[i].u>>alle[i].v>>alle[i].w;
allq.push_back(updy(alle[i].w,-2*alle[i].w,2));
}
sort(alle.begin(),alle.end());
for(int i=0;i<m;i++){
alle[i].ind=i;
}
for(int i=0;i<m;i++){
v.push_back(alle[i]);
int ind=upd();
if(ind==-1){
allq.push_back(updy(-1,alle[i].w,-1));
}
else{
int rl=(alle[i].w+alle[ind].w+1)/2;
// cout<<i<<" "<<ind<<" "<<alle[i].w<<" "<<alle[ind].w<<" wtf"<<endl;
allq.push_back(updy(rl,alle[i].w,-1));
allq.push_back(updy(rl,alle[ind].w,-1));
}
}
sort(allq.rbegin(),allq.rend());
int q;
cin>>q;
long long res=0,ted=0;
for(int i=0;i<q;i++){
int d;
cin>>d;
while((int)allq.size()>0&&allq.back().l<=d){
auto x=allq.back();
allq.pop_back();
res+=x.val;
ted+=x.ted;
//cout<<"outy: "<<x.val<<" "<<x.ted<<" "<<x.l<<" "<<d<<"\n";
}
long long mainres=res+d*ted;
cout<<mainres<<"\n";
}
}
# | 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... |