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. bzoj3196 二逼平衡树 树套树(线段树套Treap)

    Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4697  Solved: 1798[Submit][Status][D ...

  2. 通过new ClasspathApplicationContext("applicationContext.xml")找不到文件时

    可以把applicationContext.xml放到/WEB-INF/classes目录下使用先说:ClassPathXmlApplicationContext 这个类,默认获取的是WEB-INF/ ...

  3. PHP文件上传设置和处理(单文件)

    <!--upload.php内容--><?php /* 修改php.ini的设置 file_uploads必须是On upload_max_filesize 设置上传文件的大小,此值 ...

  4. 使用putty上传下载文件(pscp)

    putty作为ssh工具开源免费,简单易用.可是如何使用它来上传和下载文件呢?答案在于pscp. pscp下载地址:http://www.chiark.greenend.org.uk/~sgtatha ...

  5. 【ZOJ4053】Couleur(主席树,set,启发式)

    题意: 有n个位置,每个位置上的数字是a[i],现在有强制在线的若干个单点删除操作,每次删除的位置都不同,要求每次删除之后求出最大的连续区间逆序对个数 n<=1e5,1<=a[i]< ...

  6. 「CodePlus 2018 3 月赛」白金元首与莫斯科

    $n \leq 17,m \leq 17$,$n*m$的01矩形,对每一个0问:当他单独变成1之后,在其他0处放多米诺牌(不一定放满,可以不放)的方案数.膜$1e9+7$. 直接$dp$是$n^42^ ...

  7. 2018 ICPC 沈阳网络预赛 Fantastic Graph (优先队列)

    [传送门]https://nanti.jisuanke.com/t/31447 [题目大意]:有一个二分图,问能不能找到它的一个子图,使得这个子图中所有点的度数在区间[L,R]之内. [题解]首先我们 ...

  8. sqlplus登陆scott用户,以及退出连接

    进入sqlplus界面 即登陆成功,PLsql也一样 退出连接:

  9. tcp ip协议讲解

    http://blog.csdn.net/zhangskd/article/details/7174682

  10. ArcCatalog中通过ArcSDE向Oracle数据库中导入数据

    将数据导入到Oracle指定的表空间的具体内容如下: 首先,在ArcCatalog中建立指定表空间的数据库连接(要以指定表空间的用户登录): 然后,在ArcCatlog中定位到数据源,选中并拷贝图层; ...