hdu5627 Clarke and MST (并查集)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 311    Accepted Submission(s): 173


Problem Description
Clarke is a patient with multiple personality disorder. One day he turned into a learner of graph theory. 
He learned some algorithms of minimum spanning tree. Then he had a good idea, he wanted to find the maximum spanning tree with bit operation AND. 
A spanning tree is composed by  n1 edges. Each two points of  n points can reach each other. The size of a spanning tree is generated by bit operation AND with values of  n1 edges. 
Now he wants to figure out the maximum spanning tree.
 

Input
The first line contains an integer  T(1T5), the number of test cases. 
For each test case, the first line contains two integers  n,m(2n300000,1m300000), denoting the number of points and the number of edge respectively. 
Then  m lines followed, each line contains three integers  x,y,w(1x,yn,0w109), denoting an edge between  x,y with value  w
The number of test case with  n,m>100000 will not exceed 1. 
 

Output
For each test case, print a line contained an integer represented the answer. If there is no any spanning tree, print 0.
 

Sample Input
 
  
1 4 5 1 2 5 1 3 3 1 4 2 2 3 1 3 4 7
 

Sample Output
 
  
1
 
题意:给你n个点m条边以及m条边的权值,让你构造一棵生成树,使得生成树的权值and和最大。
思路:我们可以贪心地枚举二进制中的每一位,从30到0,然后判断有该位的值为1的所有权值中能不能形成一棵生成树,如果有,那么这些权值的边为可选边,看能不能组成下一位。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 300050
struct node{
    int x,y,w;
}e[maxn];
int ok[maxn],pre[maxn],ran[maxn];
void makeset(int x){
    pre[x]=x;
    ran[x]=0;
}
int findset(int x){
    int i,j=x,r=x;
    while(r!=pre[r])r=pre[r];
    while(j!=pre[j]){
        i=pre[j];
        pre[j]=r;
        j=i;
    }
    return r;

}

int main()
{
    int n,m,i,j,T,t;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(i=1;i<=m;i++){
            scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);
        }
        for(i=1;i<=m;i++)ok[i]=1;
        int sum=0;
        for(t=30;t>=0;t--){
            for(i=1;i<=n;i++){
                makeset(i);
            }
            int ans=n;
            for(i=1;i<=m;i++){
                if(ok[i] && (e[i].w&(1<<t) ) ){
                    int x=findset(e[i].x);
                    int y=findset(e[i].y);
                    if(x==y)continue;
                    ans--;
                    if(ran[x]>ran[y]){
                        pre[y]=x;
                    }
                    else{
                        pre[x]=y;
                        if(ran[x]==ran[y])ran[y]++;
                    }


                }


            }
            if(ans==1){
                sum|=(1<<t);
                for(i=1;i<=m;i++){
                    if(ok[i] && (e[i].w&(1<<t) )){
                        ok[i]=1;
                    }
                    else ok[i]=0;
                }


            }



        }
        printf("%d\n",sum);




    }
    return 0;




}


转载于:https://www.cnblogs.com/herumw/p/9464554.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值