HDOJ 5627 Clarke and MST (位运算最大生成树kruskul)
来源:互联网 发布:爱家cms房产网 编辑:程序博客网 时间:2024/04/30 11:12
Clarke and MST
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 266 Accepted Submission(s): 152
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 byn−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 ofn−1 edges.
Now he wants to figure out the maximum spanning tree.
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
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 integersn,m(2≤n≤300000,1≤m≤300000) , denoting the number of points and the number of edge respectively.
Thenm 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 withn,m>100000 will not exceed 1.
For each test case, the first line contains two integers
Then
The number of test case with
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
14 51 2 51 3 31 4 22 3 13 4 7
Sample Output
1
题意:给你一个图,有边权,构建一棵生成树,使得边权经过and(&)运算后结果最大,求这棵生成树最后的结果,不存在输出0
思路:用kruskul方法,排序时按照边权含有1的数量排序,优先添加1多的边,然后过程中计算就好了
ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<set>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 1010000#define LL long long#define ll __int64#define INF 0xfffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)using namespace std;int gcd(int a,int b){return b?gcd(b,a%b):a;}LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}//headstruct s{ int a,b; int num; int v;}p[MAXN];ll ans;int pri[MAXN];bool cmp(s aa,s bb){ return aa.num>bb.num;}int find(int x){ int r=x; while(r!=pri[r]) r=pri[r]; int i=x,j; while(i!=r) { j=pri[i]; pri[i]=r; i=j; } return r;}void connect(int xx,int yy,int k){ int nx=find(xx); int ny=find(yy); if(nx!=ny) { pri[nx]=pri[ny]; ans=ans&k; //printf("a=%d b=%d\n",xx,yy); //printf("kkk:%I64d\n",ans); }}int main(){ int t,i; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) pri[i]=i; for(i=0;i<m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); int cnt=0; int zz=z; while(z) { z&=(z-1); cnt++; } p[i].a=x;p[i].b=y;p[i].num=cnt;p[i].v=zz; } sort(p,p+m,cmp); ans=p[0].num; pri[p[0].a]=p[0].b; for(i=1;i<m;i++) { //printf("...%d\n",p[i].num); connect(p[i].a,p[i].b,p[i].v); } int ccnt=0; for(i=1;i<=n;i++) if(pri[i]==i) ccnt++; if(ccnt>1) printf("0\n"); else printf("%I64d\n",ans); } return 0;}
0 0
- HDOJ 5627 Clarke and MST (位运算最大生成树kruskul)
- hdoj Clarke and MST 5627(求位运算and后得到的最大生成树)(并查集&位运算)好题
- hdoj 5627 Clarke and MST 【最大and生成树】
- Clarke and MST(最大生成树)
- hdu 5627 Clarke and MST【最大生成树】
- bestcoder #72 DIV2 Clarke and MST 最大按位与的生成树
- HDU 5627 Clarke and MST 求&的最大生成树 贪心
- [伪位运算生成树] HDU 5629 Clarke and tree
- HDU 5627 Clarke and MST(贪心)
- HDU 5627 Clarke and MST
- HDU 5627 Clarke and MST
- HDOJ 2988 Dark roads(最小生成树--kruskul)
- HDOJ 4786 Fibonacci Tree(最小生成树--kruskul)
- HDOJ 5253 连接的管道(最小生成树--kruskul)
- POJ 2377 Bad Cowtractors(最大生成树--kruskul)
- HDU 5627 Clarke and MST(贪心、连通性)
- Clarke and MST HDU
- POJ Network(最小生成树--kruskul)
- 五子棋AI算法第四篇-启发式搜索函数
- sdk和ndk
- Ubuntu 文件备份问题
- linux下so动态库一些不为人知的秘密
- web页面实现拨打电话,发短信等功能
- HDOJ 5627 Clarke and MST (位运算最大生成树kruskul)
- [学习笔记] KEIL 如何快速更改工程名字
- SQL四种语言:DDL,DML,DCL,TCL
- Android4.4电池管理
- SAP接口编程-RFC系列09 : 使用BAPI
- stm32之中断优先级NVIC
- spring实现定时任务的两种方式之Spring 3整合Quartz 2实现定时任务
- 使用maven Debug断点出现无法找到源问题
- oracle SGA区基础知识与查看命令