hdu5627 Clarke and MST (并查集)
来源:互联网 发布:如何发布求购信息淘宝 编辑:程序博客网 时间:2024/06/05 10:49
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 311 Accepted Submission(s): 173
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 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 of n−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
题意:给你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 300050struct 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;}
0 0
- hdu5627 Clarke and MST (并查集)
- 并查集-HDU-5627-Clarke and MST
- HDU 5627 Clarke and MST &高位枚举+并查集
- HDOJ5627 Clarke and MST (并查集)
- hdoj Clarke and MST 5627(求位运算and后得到的最大生成树)(并查集&位运算)好题
- Clarke and MST HDU
- MST&&并查集
- HDU 5627 Clarke and MST
- HDU 5627 Clarke and MST
- 并查集&MST题集
- 并查集&MST题集
- 并查集&MST题集
- 并查集&MST题集
- 并查集&MST爬坑中。。。。
- 并查集&MST题集
- 并查集&MST题集
- HDU 5627 Clarke and MST(贪心)
- Clarke and MST(最大生成树)
- android findviewbyId 简化写法
- 自学习Django中遇到的一些问题
- Hadoop上路_14-Hadoop2.3.0的分布式集群搭建
- windows杀掉某端口进程
- shell awk 检查程序是否执行
- hdu5627 Clarke and MST (并查集)
- 和启动Activity有关的Intent标志
- Hadoop上路_12-MapReduce流程演示
- Java string 类型与其他类型的转化(string.valueof())
- Android中最常使用的Context上下文相关总结
- 配置SQL Server ODBC数据源
- Android后台任务队列类库
- 蓝牙手柄按键码
- html 编码规范