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 n−1 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 n−1 edges. 

Now he wants to figure out the maximum spanning tree.
 

Input
The first line contains an integer T(1≤T≤5),
the number of test cases. 

For each test case, the first line contains two integers n,m(2≤n≤300000,1≤m≤300000),
denoting the number of points and the number of edge respectively. 

Then m lines
followed, each line contains three integers x,y,w(1≤x,y≤n,0≤w≤109),
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; }

hdu5627 Clarke and MST (并查集)的更多相关文章

  1. Codeforces 891C Envy(MST + 并查集的撤销)

    题目链接  Envy 题意  给出一个连通的无向图和若干询问.每个询问为一个边集.求是否存在某一棵原图的最小生成树包含了这个边集. 考虑$kruskal$的整个过程, 当前面$k$条边已经完成操作的时 ...

  2. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  3. 做运动(Dijkstra+并查集+MST)

    上面的题解是这样,这道题我真的脑残,其实打代码的时候就意识到了许多,可以用Dfs+Dij+二分,这样还可以卡一卡 但是我打了spfa+spfa+二分,这个显然很慢,类似的题目我好像还做过一道的,就是在 ...

  4. [BZOJ2238]Mst 最小生成树+树链剖分/并查集

    链接 题解 先构建出最小生成树,如果删的是非树边,直接输出答案 否则问题转化为,把该边删掉后剩下两个联通块,两个端点分别在两个块内的最小边权,LCT可以维护 不妨换一种思考方向:考虑一条非树边可以代替 ...

  5. 由最小生成树(MST)到并查集(UF)

    背景 最小生成树(Minimum Spanning Tree)的算法中,克鲁斯卡尔算法(Kruskal's algorithm)是一种常用算法. 在克鲁斯卡尔算法中的一个关键问题是如何判断图中的两个点 ...

  6. 【春训团队赛第四场】补题 | MST上倍增 | LCA | DAG上最长路 | 思维 | 素数筛 | 找规律 | 计几 | 背包 | 并查集

    春训团队赛第四场 ID A B C D E F G H I J K L M AC O O O O O O O O O 补题 ? ? O O 传送门 题目链接(CF Gym102021) 题解链接(pd ...

  7. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  8. 【BZOJ】1016: [JSOI2008]最小生成树计数 深搜+并查集

    最小生成树计数 Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小 ...

  9. Luogu 2245 星际导航(最小生成树,最近公共祖先LCA,并查集)

    Luogu 2245 星际导航(最小生成树,最近公共祖先LCA,并查集) Description sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为 ...

随机推荐

  1. Linux 入门教程:00 Background

    Linux 为何物? 就是一个操作系统. Linux 历史: 操作系统始于二十世纪五十年代,当时的操作系统能运行批处理程序.批处理程序不需要用户的交互,它从文件或者穿孔卡片读取数据,然后输出到另外一个 ...

  2. HAProxy-1.8.20 根据后缀名转发到后端服务器

    global maxconn 100000 chroot /data/soft/haproxy stats socket /var/lib/haproxy/haproxy.sock mode 600 ...

  3. 关于maven多module的依赖问题

    之前的项目因为历史的原因,都是一个project里只包含了一个module,今年进入了新的项目组,出现了多个module,最近刚好也是在学<maven实战>因此想要将这个东西记录下来 工程 ...

  4. Mysql数据类型以及特性,,,防止SQL注入

    MyISAM.InnoDB.HEAP.BOB,ARCHIVE,CSV等 MyISAM:成熟.稳定.易于管理,快速读取.一些功能不支持(事务等),表级锁. InnoDB:支持事务.外键等特性.数据行锁定 ...

  5. VSCode运行时弹出powershell

    问题 安装好了vscode并且装上code runner插件后,运行代码时总是弹出powershell,而不是在vscode底部终端 显示运行结果. 解决方法 打开系统cmd ,在窗口顶部条右击打开属 ...

  6. IP2188中文资料书

    IP2188 是一款集成 12 种.用于 USB 输出端口的快充协议 IC,支持 USB 端口充电协议.支持 11种快充协议,包括 USB TypeC PD2.0/PD3.0/PPS DFP,HVDC ...

  7. 如何在K8s,Docker-Compose注入镜像Tag

    最近在做基于容器的CI/CD, 一个朴素的自动部署的思路是: 从Git Repo打出git tag,作为镜像Tag ssh远程登录到部署机器 向部署环境注入镜像Tag,拉取镜像,重新部署 下面分享我是 ...

  8. uni-app 获取地址位置

    uni.getLocation 获取当前的地理位置.速度. 在微信小程序中,当用户离开应用后,此接口无法调用:当用户点击"显示在聊天顶部"时,此接口可继续调用 uni.getLoc ...

  9. Ubuntu 能ping通DNS 地址 无法解析域名

    ping通qq百度都行,唯独谷歌不行, 主机能够ping通google的dns服务器地址 8.8.8.8,却无法解析域名   $ ping www.google.co.uk ping: unknown ...

  10. Linux网卡没有eth0显示ens33原因以及解决办法

    原因 首先说明下eth0与ens33的关系: 目前的主流网卡为使用以太网络协定所开发出来的以太网卡 (Ethernet),因此我们 Linux 就称呼这种网络接口为 ethN (N 为数字). 举例来 ...