题意:
代码:
#include <cstdio>
#include <iostream>
#include <queue>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cstring>
#define sf scanf
#define pf printf
using namespace std;
const int maxm = 500000 + 50,maxn = 10000 + 50,INF = 0x3f3f3f3f;
int n,m;
int fa[maxn];
int value[maxn];
int Find(int cur){
return fa[cur] == -1 ? cur : fa[cur] = Find(fa[cur]);
}
int edge[maxm][3];
int lable[maxm];
bool cmp(const int& a,const int& b){
return edge[a][2] < edge[b][2];
}
int ans[maxn],edge_value[maxn],ans_cnt;
int Kruskal(){
memset(fa,-1,sizeof(fa));
fill(value,value + n,1);
sort(lable,lable + m,cmp);
ans[0] = 0;
edge_value[0] = 0;
ans_cnt = 1;
int cnt = 0,i;
for(int j = 0;j < m;++j){
i = lable[j];
int& u = edge[i][0],&v = edge[i][1],&w = edge[i][2];
int f_u = Find(u),f_v = Find(v);
if(f_u != f_v){
if(ans_cnt == 0){
ans[ans_cnt] = value[f_u] * value[f_v] * 2;
}
else ans[ans_cnt] = ans[ans_cnt - 1] + value[f_u] * value[f_v] * 2;
edge_value[ans_cnt++] = w;
fa[f_u] = f_v;
value[f_v] += value[f_u];
cnt++;
if(cnt == n - 1){
break;
}
}
}
edge_value[ans_cnt] = INF;
ans[ans_cnt++] = n * (n - 1);
return 0;
}
int main(){
// freopen("read.txt","r",stdin);
int temp;
while( ~sf("%d %d",&n,&m) ){
for(int i = 0;i < m;++i){
sf("%d %d %d",&edge[i][0],&edge[i][1],&edge[i][2]);
lable[i] = i;
}
Kruskal();
sf("%d",&m);
for(int i = 0;i < m;++i){
sf("%d",&temp);
int p = lower_bound(edge_value,edge_value + ans_cnt,temp) - edge_value;
if(edge_value[p] >= temp) p--;
pf("%d\n",n * (n - 1) - ans[p]);
}
}
return 0;
}