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
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.
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.
4 5
1 2 5
1 3 3
1 4 2
2 3 1
3 4 7
#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 (并查集)的更多相关文章
- Codeforces 891C Envy(MST + 并查集的撤销)
题目链接 Envy 题意 给出一个连通的无向图和若干询问.每个询问为一个边集.求是否存在某一棵原图的最小生成树包含了这个边集. 考虑$kruskal$的整个过程, 当前面$k$条边已经完成操作的时 ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 做运动(Dijkstra+并查集+MST)
上面的题解是这样,这道题我真的脑残,其实打代码的时候就意识到了许多,可以用Dfs+Dij+二分,这样还可以卡一卡 但是我打了spfa+spfa+二分,这个显然很慢,类似的题目我好像还做过一道的,就是在 ...
- [BZOJ2238]Mst 最小生成树+树链剖分/并查集
链接 题解 先构建出最小生成树,如果删的是非树边,直接输出答案 否则问题转化为,把该边删掉后剩下两个联通块,两个端点分别在两个块内的最小边权,LCT可以维护 不妨换一种思考方向:考虑一条非树边可以代替 ...
- 由最小生成树(MST)到并查集(UF)
背景 最小生成树(Minimum Spanning Tree)的算法中,克鲁斯卡尔算法(Kruskal's algorithm)是一种常用算法. 在克鲁斯卡尔算法中的一个关键问题是如何判断图中的两个点 ...
- 【春训团队赛第四场】补题 | 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 ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- 【BZOJ】1016: [JSOI2008]最小生成树计数 深搜+并查集
最小生成树计数 Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小 ...
- Luogu 2245 星际导航(最小生成树,最近公共祖先LCA,并查集)
Luogu 2245 星际导航(最小生成树,最近公共祖先LCA,并查集) Description sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为 ...
随机推荐
- 如何构建一个多人(.io) Web 游戏,第 1 部分
原文:How to Build a Multiplayer (.io) Web Game, Part 1 GitHub: https://github.com/vzhou842/example-.io ...
- MySQL where 条件字句查询
where 条件字句 搜索条件可由一个或多个逻辑表达式组成 , 结果一般为布尔值 逻辑运算符 运算符 语法 描述 and && a and b a && b 逻辑与 两 ...
- 【ORACLE错误】SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled
执行set autotrace traceonly的时候,报错 SQL> set autotrace traceonly SP2-0618: Cannot find the Session Id ...
- RocketMQ—消息队列入门
消息队列功能介绍 字面上说的消息队列是数据结构中"先进先出"的一种数据结构,但是如果要求消除单点故障,保证消息传输可靠性,应对大流量的冲击,对消息队列的要求就很高了.现在互联网的& ...
- 解决MyBatis-Plus 3.3.1中自动生成代码tinyint(1)无法自动转换为Boolean 的办法
解决方法 1.在测试类中新建一个类MySqlTypeConvertCustom,继承MySqlTypeConvert并实现ITypeConvert后覆盖processTypeConvert方法. 2. ...
- Java 8中字符串拼接新姿势:StringJoiner
介绍 StringJoiner是java.util包中的一个类,用于构造一个由分隔符分隔的字符序列(可选),并且可以从提供的前缀开始并以提供的后缀结尾.虽然这也可以在StringBuilder类的帮助 ...
- OpenStack使用OVN
1. Controller节点 1.1 安装 OVS和OVN 安装 Python3.7: yum -y groupinstall "Development tools" yum - ...
- Java语法糖详解
语法糖 语法糖(Syntactic Sugar),也称糖衣语法,是由英国计算机学家 Peter.J.Landin 发明的一个术语,指在计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更 ...
- MySQL新特性MTS
一.MTS:多线程复制 MTS简介 在MySQL 5.6版本之前,Slave服务器上有两个线程I/O线程和SQL Thread线程.I/O线程负责接收二进制日志(Binary Log,更准确的说是二进 ...
- 览器全面禁用三方 Cookie 全栈前端精选 4月16日
览器全面禁用三方 Cookie 全栈前端精选 4月16日