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
 
Source
 

首先贴上自己的写法,虽然不是很正宗的做法

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<math.h>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<set>
  10. #include<bitset>
  11. #include<map>
  12. #include<vector>
  13. #include<stdlib.h>
  14. #include <stack>
  15. using namespace std;
  16. #define PI acos(-1.0)
  17. #define max(a,b) (a) > (b) ? (a) : (b)
  18. #define min(a,b) (a) < (b) ? (a) : (b)
  19. #define ll long long
  20. #define eps 1e-10
  21. #define MOD 1000000007
  22. #define N 300006
  23. #define M 300006
  24. #define inf 1e12
  25. struct Node{
  26. int x,y;
  27. int cost;
  28. }edge[M];
  29. int n,m;
  30. int fa[N];
  31. void init(){
  32. for(int i=;i<N;i++){
  33. fa[i]=i;
  34. }
  35. }
  36. int find(int x){
  37. return fa[x]==x?x:fa[x]=find(fa[x]);
  38. }
  39. bool cmp(Node a,Node b){
  40. return a.cost>b.cost;
  41. }
  42. int main()
  43. {
  44. int t;
  45. scanf("%d",&t);
  46. while(t--){
  47. scanf("%d%d",&n,&m);
  48. init();
  49. for(int i=;i<m;i++){
  50. int a,b,c;
  51. scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].cost);
  52. }
  53. sort(edge,edge+m,cmp);
  54. int flag=;
  55. int ans;
  56. int num=n-;
  57. for(int i=;i<m;i++){
  58. int root1=find(edge[i].x);
  59. int root2=find(edge[i].y);
  60. if(root1!=root2){
  61. if(flag){
  62. ans=edge[i].cost;
  63. flag=;
  64. }else{
  65. ans&=edge[i].cost;
  66. }
  67. fa[root1]=root2;
  68. num--;
  69. }
  70. }
  71. if(num!=){
  72. printf("0\n");
  73. }
  74. else{
  75. printf("%d\n",ans);
  76. }
  77. }
  78.  
  79. return ;
  80. }

官方题解:

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <algorithm>
  5. using namespace std;
  6. const int N = + ;
  7.  
  8. struct Edge{
  9. int from,to,dis;
  10. }a[N],b[N];
  11. int fa[N];
  12. int find(int x){
  13. if(x==fa[x]) return x;
  14. return fa[x] = find(fa[x]);
  15. }
  16. int tmp;
  17. bool solve(int pos, Edge *a, int n, int m){
  18. for(int i=;i<=n;++i)
  19. fa[i] = i;
  20. int cnt = ;
  21. tmp = ;
  22. for(int i=;i<=m;++i){
  23. if(((a[i].dis>>pos)&)==)
  24. continue;
  25.  
  26. int fu = find(a[i].from);
  27. int fv = find(a[i].to);
  28. if(fu!=fv){
  29. if(cnt==)
  30. tmp = a[i].dis;
  31. else
  32. tmp &= a[i].dis;
  33.  
  34. fa[fu] = fv;
  35. cnt++;
  36. if(cnt==n-)
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. int main() {
  43.  
  44. int t,n,m;
  45. scanf("%d",&t);
  46. while(t--){
  47. scanf("%d%d",&n,&m);
  48.  
  49. for(int i=;i<=m;++i){
  50. scanf("%d%d%d",&a[i].from,&a[i].to,&a[i].dis);
  51. }
  52. int ans = ;
  53. for(int i=;i>=;--i){
  54. if(solve(i,a,n,m)){
  55. ans = tmp;
  56. int mm = ;
  57. for(int i=;i<=m;++i){
  58. if((a[i].dis>>i)&)
  59. b[++mm] = a[i];
  60. }
  61. m = mm;
  62. for(int i=;i<=m;++i)
  63. a[i] = b[i];
  64. }
  65. }
  66. cout<<ans<<endl;
  67. }
  68. return ;
  69. }

hdu 5627 Clarke and MST(最大 生成树)的更多相关文章

  1. HDU 5627 Clarke and MST &意义下最大生成树 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5627 题意:Bestcoder的一道题,让你求&意义下的最大生成树. 解法: 贪心,我们从高位 ...

  2. HDU 5628 Clarke and math——卷积,dp,组合

    HDU 5628 Clarke and math 本文属于一个总结了一堆做法的玩意...... 题目 简单的一个式子:给定$n,k,f(i)$,求 然后数据范围不重要,重要的是如何优化这个做法. 这个 ...

  3. hdu 3367(Pseudoforest ) (最大生成树)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  4. HDU 5629 Clarke and tree dp+prufer序列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=562 题意: 求给每个节点的度数允许的最大值,让你求k个节点能组成的不同的生成树个数. 题解: 对于n ...

  5. hdu 5565 Clarke and baton 二分

    Clarke and baton Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  6. hdu 5563 Clarke and five-pointed star 水题

    Clarke and five-pointed star Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/show ...

  7. hdu 5465 Clarke and puzzle 二维线段树

    Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  8. hdu 5464 Clarke and problem dp

    Clarke and problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...

  9. HDU 5628 Clarke and math dp+数学

    Clarke and math 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5628 Description Clarke is a patient ...

随机推荐

  1. Linux内存点滴 用户进程内存空间

    Linux内存点滴 用户进程内存空间 经常使用top命令了解进程信息,其中包括内存方面的信息.命令top帮助文档是这么解释各个字段的. VIRT, Virtual Image (kb) RES, Re ...

  2. <% %> 、 <%= %> 、<%# %> 的区别

    1,<% %>用来绑定后台代码    中间一般放函数或者方法,典型的asp程序写法. 在前台页面可以写后台代码                 相当于开辟了C#空间,可以写C#代码 2,& ...

  3. textChanged(*)重点

    # -*- coding: cp936 -*- import sys from PyQt4 import QtCore, QtGui class MyDialog(QtGui.QDialog): de ...

  4. 判断一个int 型整数 是否为回文数

    leetcode 上的题目 Determine whether an integer is a palindrome. Do this without extra space. 由于不能使用额外空间, ...

  5. SPRING源码分析:IOC容器

    在Spring中,最基本的IOC容器接口是BeanFactory - 这个接口为具体的IOC容器的实现作了最基本的功能规定 - 不管怎么着,作为IOC容器,这些接口你必须要满足应用程序的最基本要求: ...

  6. SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.

    SpringMVC的@ResponseBody,返回json,如果有中文显示乱码的解决办法. 在SpringMVC的配置文件中 <bean class="org.springframe ...

  7. [跟我学spring学习笔记][更多DI知识]

    延迟初始化Bean 定义: 延迟初始化也叫做惰性初始化,指不提前初始化Bean,在真正使用时才创建并初始化Bean 如何延迟: 配置方式很简单只需在标签上指定 “lazy-init” 属性值为“tru ...

  8. 虚拟化之docker安装篇

    1,docker pull centos     下载centos镜像 docker search centos  搜索镜像 2,docker images           查看本地镜像 3,do ...

  9. 关于document.write()重写页面

    今天碰到了一个以前没注意的问题即:document.write(),在此拿来分享! document.write是最基本的JavaScript命令之一,这个命令简单地打印指定的文本内容到页面上(注意是 ...

  10. 0112.1——iOS开发之理解iOS中的MVC设计模式

    模型-视图-控制器(Model-View-Controller,MVC)是Xerox PARC在20世纪80年代为编程语言Smalltalk-80发明的一种软件设计模式,至今已广泛应用于用户交互应用程 ...