题干:

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−1n−1 edges. Each two points of nn points can reach each other. The size of a spanning tree is generated by bit operation AND with values of n−1n−1 edges. 
Now he wants to figure out the maximum spanning tree.

Input

The first line contains an integer T(1≤T≤5)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)n,m(2≤n≤300000,1≤m≤300000), denoting the number of points and the number of edge respectively. 
Then mm lines followed, each line contains three integers x,y,w(1≤x,y≤n,0≤w≤109)x,y,w(1≤x,y≤n,0≤w≤109), denoting an edge between x,yx,y with value ww. 
The number of test case with n,m>100000n,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条边的无向图,问你最大与运算生成树多少权值是多少,如果不连通输出0。

解题报告:

贪心不难发现其实就是求最大生成树。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 6e5 + 5;
struct Edge {int u,v;ll w;
} e[MAX];
int n,m;
bool cmp(Edge a,Edge b) {return a.w > b.w;
}
int f[MAX];
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {int t1 = getf(u),t2 = getf(v);f[t2] = t1;
}
int main()
{int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);for(int i = 1; i<=n; i++) f[i] = i;for(int a,b,i = 1; i<=m; i++) {ll w;scanf("%d%d%lld",&a,&b,&w);e[i].u = a;e[i].v = b;e[i].w = w;}sort(e+1,e+m+1,cmp);ll ans = 1LL<<62;ans--;ll no = ans;
//      cout <<ans <<endl;
//      while(ans) {
//          printf("%lld",ans%2);
//          ans>>=1;
//      }int cnt = 0;for(int i = 1; i<=m; i++) {if(getf(e[i].u) != getf(e[i].v)) {ans = ans & e[i].w;merge(e[i].u,e[i].v);cnt++;}}if(cnt != n-1) printf("0\n");else printf("%lld\n",ans);} return 0 ;
}

【HDU - 5627】Clarke and MST(最大生成树,与运算性质,最小生成树MST变形)相关推荐

  1. The Unique MST 判断生成树是否唯一

    The Unique MST 题目抽象:给你一个连通无向网,判断生成树是否唯一. 分析 :判定最小生成树是否唯一的一个正确思路为: 1) 对图中每条边,扫描其他边,如果存在相同权值的边,则对该边作标记 ...

  2. 图论复习——最小生成树MST

    知识点 MST的构造 Boruvka算法常用于解决这类问题:给你n个点,每个点有点权,任意两个点之间有边权,边权为两个点权用过某种计算方式得出,求最小生成树.动图 MST上的确定性和存在性问题 最小生 ...

  3. C语言Prims求最小生成树MST的算法(附完整源码)

    C语言Prims求最小生成树MST的算法 C语言Prims求最小生成树MST的算法完整源码(定义,实现,main函数测试) C语言Prims求最小生成树MST的算法完整源码(定义,实现,main函数测 ...

  4. HDU 2276 Kiki Little Kiki 2 (位运算+矩阵快速幂)

    HDU 2276 Kiki & Little Kiki 2 (位运算+矩阵快速幂) ACM 题目地址:HDU 2276 Kiki & Little Kiki 2 题意:  一排灯,开关 ...

  5. `Computer-Algorithm` 最小生成树MST,Prim,Kruskal,次小生成树

    Contents 最小生成树 Algorithm Prim Code Kruskal Prim&KruskalPrim \& KruskalPrim&Kruskal算法的性质 ...

  6. hdu 4263(有限制的生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4263 思路:将红边和蓝边单独求一次生成树,求的红边最多可以加入的边数cntr,蓝边最多可以加入的边数c ...

  7. 【POJ - 3723】Conscription (最大生成树,最小生成树MST变形)

    题干: Windy has a country, and he wants to build an army to protect his country. He has picked up N gi ...

  8. HDU 5465 Clarke and puzzle (二维树状数组维护区间异或)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5465 解题思路: 因为要对大量的区间进行异或,所以考虑用二维树状数组就行维护. #include< ...

  9. hdu 5563 Clarke and five-pointed star (枚举)

    题意:给出5个点的坐标,问这5个点是否刚好是一个五角星的顶点. 思路: dfs枚举5个点的顺序. 判断5条邻边相等,5条对角线相等.若均满足则是,否则不是. 附:为什么要判断5条对角线也相等才行呢? ...

最新文章

  1. 机器学习模型部署都有哪些坑?
  2. WebSocket 学习
  3. 给你一份长长长的 Spring Boot 知识清单(上)
  4. 1094 The Largest Generation (25 分)【难度: 一般 / 树的遍历】
  5. 什么是用户智能,它与数据有什么关系?
  6. 关于字节序(大端法、小端法)的定义
  7. php批量评价,彻底杜绝 WordPress 批量垃圾评论留言的三步曲
  8. windowsphone7高级编程中提到的地址
  9. 一次失败的项目经理招聘经验
  10. 【教育】斯坦福大学——人工智能本科4年课程清单
  11. redis数据库无法写入导致的bug
  12. Bailian4150 上机【DP】
  13. 【Windows编程】系列第六篇:创建Toolbar与Statusbar
  14. js上传本地文件到oss
  15. 固态硬盘接口类型介绍
  16. 输入直角三角形的两个直角边的长度 a、b,求斜边 c 的长度。
  17. 使用linux集体升级系统,1.3. 利用mtd工具升级Linux系统
  18. 直播带货“老三”,抖音背上「KPI」了
  19. ArcGIS Pro教程 | 1#数据准备
  20. quill光标位置插入html,quill编辑器+word文档上传,插入指定位置

热门文章

  1. code1928: 日期差值 技巧模拟
  2. java 8 io_Java IO8:IO简单总结
  3. 堆排序python代码实现_python实现堆排序
  4. 大工19春计算机文化基础在线测试1,大工19春《计算机文化基础》在线测试1(含答案)...
  5. Linux表空间扩容,linux下oracle表空间导致磁盘空间不足
  6. python gui 自动化_python GUI测试自动化
  7. python水平_如何在python中水平透视表
  8. C# 派生类的构造函数
  9. javapanel根据内部组件_[译]避免在unmounted组件上调用setState
  10. pppd 源码修改1