hdu 4750
来源:互联网 发布:英雄之刃 网页游戏mac 编辑:程序博客网 时间:2024/05/22 04:36
#include <cstdio>#include <algorithm>#define _int __int64using 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;}