Clarke and MST

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 315    Accepted Submission(s): 176

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
从大到小按位枚举。因为是&  所以把边集中这个位为1的都拿出来,看这些边能不能构成生成树。具体 看代码。(这里用的并查集判断是不是树,也可以用bfs或dfs判断)
/* ***********************************************
Author :guanjun
Created Time :2016/2/16 19:02:06
File Name :bc72c.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 300000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
int fa[maxn],w[maxn],p[maxn],q[maxn],n,m;
int findfa(int x){
if(x==fa[x])return x;
return fa[x]=findfa(fa[x]);
}
void Union(int a,int b){
int x=findfa(a);
int y=findfa(b);
if(x>y)fa[x]=y;
else if(y>x)fa[y]=x;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t,x,y,z;
cin>>t;
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){
scanf("%d%d%d",&x,&y,&z);
w[i]=z;
p[i]=x;
q[i]=y;
}
int ans=;
for(int i=;i>=;i--){
x=(<<i);
for(int ii=;ii<=n;ii++)fa[ii]=ii;
for(int j=;j<m;j++){
if((w[j]&x)&&((w[j]&ans)==ans)){
//cout<<p[j]<<" "<<q[j]<<endl;
Union(p[j],q[j]);
}
}
int f=fa[];
int mark=;
for(int ii=;ii<=n;ii++){
if(fa[ii]!=f){
mark=;break;
}
}
if(mark)ans+=x;
}
printf("%d\n",ans);
}
return ;
}

HDU 5627Clarke and MST的更多相关文章

  1. LA 5713 - Qin Shi Huang's National Road System(HDU 4081) MST

    LA:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  2. MST(最小生成树)——Prim算法——HDU 1879-继续畅通工程

    Prim算法很好理解,特别是学完了迪杰斯特拉算法之后,更加能理解Prim的算法思想 和迪杰斯特拉算法差不多,由于最后要形成连通图,故任意指定一个点,作为初始点,遍历所有点,以当前最小权值的点(和迪杰斯 ...

  3. hdu 4756 MST+树形dp ****

    题意:给你n(n = 1000)个二维点,第一个点是power plant,还有n - 1个点是dormitories.然后现在知道有一条寝室到寝室的边是不能连的,但是我们不知道是哪条边,问这种情况下 ...

  4. HDU 1598 find the most comfortable road (MST)

    find the most comfortable road Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  5. HDU 4081 MST

    这道题在LRJ的书上看到,今天回过头来继续看这题,发现很多东西都已经明白了. 题意:有N个城市,每个城市有一个坐标和人口. 现在要建一些边使得他们都联通,花费就是这些边的长度,然后有一条边可以免费.问 ...

  6. HDU 4126 Genghis Khan the Conqueror MST+树形dp

    题意: 给定n个点m条边的无向图. 以下m行给出边和边权 以下Q个询问. Q行每行给出一条边(一定是m条边中的一条) 表示改动边权. (数据保证改动后的边权比原先的边权大) 问:改动后的最小生成树的权 ...

  7. HDU 4126 Genghis Khan the Conqueror (树形DP+MST)

    题意:给一图,n个点,m条边,每条边有个花费,给出q条可疑的边,每条边有新的花费,每条可疑的边出现的概率相同,求不能经过原来可疑边 (可以经过可疑边新的花费构建的边),注意每次只出现一条可疑的边,n个 ...

  8. HDU 4756 Install Air Conditioning (MST+树形DP)

    题意:n-1个宿舍,1个供电站,n个位置每两个位置都有边相连,其中有一条边不能连,求n个位置连通的最小花费的最大值. 析:因为要连通,还要权值最小,所以就是MST了,然后就是改变一条边,然后去找出改变 ...

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

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

随机推荐

  1. bzoj1975: [Sdoi2010]魔法猪学院【k短路&A*算法】

    1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2446  Solved: 770[Submit][Statu ...

  2. 征途(bzoj 4518)

    Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜 ...

  3. STL学习笔记(四) 迭代器

    条款26:iterator 优先于 const_iterator, reverse_iterator, const_reverse_iterator iterator, reverse_iterato ...

  4. ElasticSearch聚合入门(续)

    主要理解聚合中的terms. 参考:http://www.cnblogs.com/xing901022/p/4947436.html Terms聚合 记录有多少F,多少M { "size&q ...

  5. ReSharper7.1.25.234 注册机

    经常用vs做开发的人都知道,ReSharper是vistual studio必备插件之一.他的智能提示,智能感知,.net底层方法查看,测试等都非常方便,给程序员带来了巨大的效率. 但众所周知ReSh ...

  6. indexedDB 增删改查

    /** * 打开数据库 */ export function openDB() { return new Promise((resolve, reject) => { let indexedDB ...

  7. 关于代码调试de那些事

    原文出处:http://www.wklken.me/posts/2014/11/23/how-to-debug.html 关于代码调试de那些事 1.你得明白你在做什么, 保持清醒 2.想清楚了再写代 ...

  8. ctrl+c,ctrl+d,ctrl+z在linux程序中意义和区别

    原文: http://blog.csdn.net/sxhlovehmm/article/details/41318111 [侵删] ctrl+c和ctrl+z都是中断命令,但是他们的作用却不一样.   ...

  9. Scrapy学习-4-Items类&Pipelines类

    items类使用 作用 能使得我们非常方便的操作字段名 在items.py中定制我们的类 class ArticleItem(scrapy.Item): title = scrapy.Field() ...

  10. IntelliJ IDEA配置Tomcat/Jetty运行Web项目

    一.使用Maven的POM引入插件的形式: 这种方式只需在POM中引入Tomcat/Jetty的插件即可运行.参考:http://www.cnblogs.com/EasonJim/p/6687272. ...