HDU3938

传送门

题解:

MST+并查集。最长路的最小边一定是在最小生成树中。我们按照边权从小到大排序。小集合的答案一定包含在大集合里面。当我们形成两个联通块的时候再加一条边进来能够联通并且满足条件的那么这条边一定是最长的最小边。方案数也就是两个联通块的节点数相乘。

#include <bits/stdc++.h>
#define LL long long
#define N 10050
using namespace std;
struct node{
	int a, b, c, i;
	bool operator < (const node & A) const{
		return c < A.c;
	}
}d[N * 5], q[N];
int f[N], p[N], sum, ans[N], cnt[N];
int find(int x){
	return x == f[x]? x: f[x] = find(f[x]);
}
int main(){
	int i, j, n, m, t, a, b, c;
	while(scanf("%d%d%d", &n, &m, &t) != EOF){
		for(i = 1; i <= n; i++) f[i] = i;
		for(i = 1; i <= m; i++) scanf("%d%d%d", &d[i].a, &d[i].b, &d[i].c);
		sort(d + 1, d + i);
		for(i = 1; i <= t; i++) scanf("%d", &q[i].c), q[i].i = i;
		sort(q + 1, q + i);
		for(i = 1; i <= t; i++) p[q[i].i] = i;
		for(i = 1; i <= n; i++) cnt[i] = 1;
		j = 1; sum = 0;
		for(i = 1; i <= m; i++){
			c = d[i].c;
			while(c > q[j].c && j <= t){
				ans[j++] = sum;
			}
			a = find(d[i].a); b = find(d[i].b);
			if(a != b){
				f[b] = a;
				sum += cnt[a] * cnt[b];
				cnt[a] += cnt[b];
			}
		}
		while(j <= t) ans[j++] = sum;
		for(i = 1; i <= t; i++) printf("%d\n", ans[p[i]]);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值