HDU 4750 Count The Pairs (最小生成树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750

 

题意:给定一个边权都不一样的图,然后给出10W个询问,每个询问有个T,求图中任意两点所有路径中满足最长边中的最小值(瓶颈边)大于等于T的(s,t)对数

 

思路:按kruskal的顺序合并边,每次合并边,可以发现每次合并的那条边,就是连接两个集合的瓶颈路(最小生成树中边权最大那条边),这样就可以求出以这条边为瓶颈边的顶点对数

 

 

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 10010;
const int M = 500010;

int sum[N];
int fa[N];
int tot[N];
int cnt;
vector <int> v;

struct edge
{
    int a, b, w;
    edge() {}
    edge(int a, int b, int w) : a(a), b(b), w(w) {}
    bool operator < (const edge &rhs) const
    {
        return w < rhs.w;
        if(a != rhs.a)
            return a < rhs.a;
        if(b != rhs.b)
            return b < rhs.b;
    }
} e[M];

void init()
{
    cnt = 1;
    v.clear();
    for(int i = 0; i <= N; i++)
    {
        fa[i] = i;
        sum[i] = 1;
    }
    memset(tot, 0, sizeof(tot));
}

int Find(int x)
{
    if(x == fa[x])
        return x;
    return fa[x] = Find(fa[x]);
}

int main()
{
    int n, m, q;
    while(~scanf("%d%d", &n, &m))
    {
        init();
        for(int i = 0; i < m; i++)
        {
            int a, b, w;
            scanf("%d%d%d", &a, &b, &w);
            e[i] = edge(a, b, w);
        }
        sort(e, e + m);
        for(int i = 0; i < m; i++)
        {
            int fx = Find(e[i].a);
            int fy = Find(e[i].b);
            if(fx != fy)
            {
                v.push_back(e[i].w);
                tot[cnt++] = sum[fx] * sum[fy] * 2;
                sum[fy] += sum[fx];
                fa[fx] = fy;
            }
        }
        for(int i = 1; i < cnt; i++)
            tot[i] += tot[i - 1];
        scanf("%d", &q);
        for(int i = 0; i < q; i++)
        {
            int w;
            scanf("%d", &w);
            int idx = lower_bound(v.begin(), v.end(), w) - v.begin();
            printf("%d\n", tot[cnt - 1] - tot[idx]);
        }
    }
    return 0;
}

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值