HDU 5627 Clarke and MST

问题描述
克拉克是一名人格分裂患者。某一天克拉克变成了一名图论研究者。  
他学习了最小生成树的几个算法,于是突发奇想,想做一个位运算and的最大生成树。  
一棵生成树是由n-1n1条边组成的,且nn个点两两可达。一棵生成树的大小等于所有在生成树上的边的权值经过位运算and后得到的数。  
现在他想找出最大的生成树。
输入描述
第一行是一个整数T(1 \le T \le 5)T(1T5),表示数据组数。  
每组数据第一行是两个整数n, m(1 \le n, m \le 300000)n,m(1n,m300000),分别表示点个数和边个数。其中n, m > 100000n,m>100000的数据最多一组。  
接下来mm行,每行33个整数x, y, w(1 \le x, y \le n, 0 \le w \le 10^9)x,y,w(1x,yn,0w109),表示x, yx,y之间有一条大小为ww的边。
输出描述
每组数据输出一行一个数,表示答案。若不存在生成树,输出00
输入样例
1
4 5
1 2 5
1 3 3
1 4 2
2 3 1
3 4 7
输出样例
1
根据2进制一位一位往下做,找到符合的生成树
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<functional>
using namespace std;
typedef unsigned long long ull;
typedef long long LL;
const int maxn = 3e5 + 10;
const int mod = 1e9 + 7;
int T, n, m, fa[maxn], ans;

struct point
{
    int x, y, w;
    void read(){ scanf("%d%d%d", &x, &y, &w); }
    bool operator <(const point&a)const
    {
        return w > a.w;
    }
}a[maxn];

int get(int x)
{
    return fa[x] == x ? x : fa[x] = get(fa[x]);
}

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        cin >> n >> m;
        for (int i = 0; i < m; i++) a[i].read();
        sort(a, a + m);
        ans = 0;
        for (int i = 29; i >= 0; i--)
        {
            int cnt = n - 1;
            for (int j = 1; j <= n; j++) fa[j] = j;
            for (int j = 0; j < m&&cnt; j++)
            {
                if (a[j].w < (1 << i)) break;
                if (a[j].w & (1 << i))
                {
                    int fx = get(a[j].x), fy = get(a[j].y);
                    if (fx == fy) continue;
                    fa[fx] = fy;    cnt--;
                }
            }
            if (!cnt)
            {
                ans |= (1 << i);
                for (int j = 0; j < m; j++)
                {
                    if (a[j].w < (1 << i)) break;
                    if (a[j].w & (1 << i)) a[cnt++] = a[j];
                }
                m = cnt;
            }
        }
        printf("%d\n", ans);
    }    
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值