hdu 4750

#include <cstdio>
#include <algorithm>
#define _int __int64
using namespace std;
struct edge{
    _int u,v,len;
}E[501000];
_int fa[11000],set[11000];
_int tot,res[501000],n;
int m,w[501000];
bool hash[11000];
bool operator < (const edge& a,const edge& b) {
    return a.len<b.len;
}
void init(){
    for(_int i=0;i<=10100;i++){
        fa[i]=i;
        set[i]=1;
        hash[i]=0;
    }
    tot=0;
    for(_int i=0;i<501000;i++){
        w[i]=0;
        res[i]=res[i-1];
    }
}
_int find(_int x){
    return fa[x] !=x ? fa[x] = find(fa[x]) : x;
}
void so(_int &fx,_int &fy){
    _int t;
    if(fx<fy){
        t=fx;
        fx=fy;
        fy=t;
    }
}
void MST(){
    _int fx,fy;
    for(_int i=0;i<m;i++){
        fx=find(E[i].u),fy=find(E[i].v);
        so(fx,fy);
        //printf("x:%I64d y:%I64d\n",fx,fy);
        if(fx!=fy){
            res[i]+=2*set[fx]*set[fy];
            fa[fy]=fx;
            set[fx]+=set[fy];
            w[i]=E[i].len;
        }
        else {
            if(i)
                w[i]=w[i-1];
        }
        if(i)
            res[i]+=res[i-1];
    }
}
void sol(){
    MST();
    for(_int i=0;i<n;i++){
        _int t=find(i);
        if(!hash[t])
           tot+=set[t]*(set[t]-1);
        hash[t]=1;
    }
    _int p,tt;
    int t;
    scanf("%I64d",&p);
    int x;
    while(p--){
        scanf("%d",&t);
        x=lower_bound(w,w+m,t)-w;
        if(w[x]<t)
            tt=tot;
        else if(x==0)
            tt=0;
        else
            tt=res[x-1];
        printf("%I64d\n",tot-tt);
    }
}
int main(){
    while(~scanf("%I64d%d",&n,&m)){
        init();
        for(_int i=0;i<m;i++){
            scanf("%I64d%I64d%I64d",&E[i].u,&E[i].v,&E[i].len);
            res[i]=0;
            w[i]=0;
        }
        sort(E,E+m);
        sol();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值