题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5544

学习链接:https://www.cnblogs.com/qscqesze/p/4902518.html

https://blog.csdn.net/snowy_smile/article/details/49928445

Ba Gua Zhen

Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1045    Accepted Submission(s): 325

Problem Description
During the Three-Kingdom period, there was a general named Xun Lu who belonged to Kingdom Wu. Once his troop were chasing Bei Liu, he was stuck in the Ba Gua Zhen from Liang Zhuge. The puzzle could be considered as an undirected graph with N vertexes and M edges. Each edge in the puzzle connected two vertexes which were ui and vi with a length of wi. Liang Zhuge had great interests in the beauty of his puzzle, so there were no self-loops and between each pair of vertexes, there would be at most one edge in the puzzle. And it was also guaranteed that there was at least one path to go between each pair of vertexes.

Fortunately, there was an old man named Chengyan Huang who was willing to help Xun Lu to hack the puzzle. Chengyan told Xun Lu that he had to choose a vertex as the start point, then walk through some of the edges and return to the start point at last. During his walk, he could go through some edges any times. Since Liang Zhuge had some mysterious magic, Xun Lu could hack the puzzle if and only if he could find such a path with the maximum XOR sum of all the edges length he has passed. If the he passed some edge multiple times, the length would also be calculated by multiple times. Now, could you tell Xun Lu which is the maximum XORcircuit path in this puzzle to help him hack the puzzle?

 
Input
The first line of the input gives the number of test cases, T(1≤T≤30). T test cases follow.

Each test case begins with two integers N(2≤N≤5×104) and M(1≤M≤105) in one line. Then M lines follow. Each line contains three integers ui, vi and wi(1≤ui,vi≤N,0≤wi≤260−1) to describe all the edges in the puzzle.

 
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum XOR sum of one circuit path in the puzzle.
 
Sample Input
2
3 3
1 2 1
1 3 2
2 3 0
6 7
1 2 1
1 3 1
2 3 1
3 4 4
4 5 2
4 6 2
5 6 2
 
Sample Output
Case #1: 3
Case #2: 3

Hint

A XOR takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits.
The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1.
In this we perform the comparison of two bits, being 1 if the two bits are different, and 0 if they are the same.

 
题目大意:输入T  T组样例。  输入N,M  代表有N个结点 M条边   接下来给出 x,y,z  代表x,y结点之间有一条权值为z的边  
求所有回路中异或值为最大是多少?
思路:首先我们可以容易想到只有环才有可能对答案有贡献,因为题意说了所有回路中的异或值最大是多少,如果不是环  那么这条边一定要走偶数次的 这样异或起来就是0了
所以我们首先得求出所有环的异或值  并存入一个数组中。  那么我们怎么存起来所有的环呢?  dfs跑一遍  当跑到一个访问过的点的时候代表跑到了环。  这时候存起来就好了
跑出了所有的环,之后我们的任务就是在这些环的答案中选出若干个使得答案最大,这怎么选呢?  不可能暴力吧  显然不可能。  在这里我们使用的方法是贪心:
怎么贪心呢?   我们从高位开始求  题目中的范围是2^59 那么答案最大也就是2^60. 。看到这句话有思路吗?
有的话就很不错了  我们从最高位开始贪心  如果某个数在这一位有数的值的话  并且我们当前的数在这一位没有数 那么是不是一定要把这个数异或呢 ? 显然是的  当我们得到了这个数
我们当前的这一位也就有值了,剩下的数不能在这一位有值 不然的话 岂不是异或之后这一位又变为了0  。  所以所有在这一位有值的数 都需要异或一下这一个数  使得后面位数不会影响到前面
的结果。  就是这样了 
看代码:
#include<iostream>
#include<vector>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=5e4+;
vector<pair<LL,LL> >v[maxn];
vector<LL>ans;
LL Xor[maxn];
bool vis[maxn];
void dfs(LL n,LL pre,LL sum)//当前节点 上一个节点 异或和
{
if(vis[n])//走到了环
{
ans.push_back(sum^Xor[n]);//环的异或值存入数组中
return ;
}
vis[n]=true;
int len=v[n].size();
for(int i=;i<len;i++)
{
LL w=v[n][i].first;
if(w!=pre)//防止回到上一个节点
{
if(!vis[w])
{
Xor[w]=Xor[n]^v[n][i].second;
}
dfs(w,n,sum^v[n][i].second);
}
}
}
int main()
{
int T;
scanf("%d",&T);
int ca=;
while(T--)
{
ans.clear();
LL N,M;
scanf("%lld%lld",&N,&M);
for(int i=;i<=N;i++)//初始化
{
v[i].clear();
vis[i]=false;
Xor[i]=;
} for(int i=;i<=M;i++)
{
LL x,y,w;
scanf("%lld%lld%lld",&x,&y,&w);
v[x].push_back(make_pair(y,w));//无向边
v[y].push_back(make_pair(x,w));
}
dfs(,,);//选1为起点 跑出所有的环 /**
贪心
答案最大在2^60
*/
LL Ans=;
LL len=ans.size();
LL k=;
for(int i=;i>=;i--)//
{
LL tmp=;
int j;
for(j=k;j<len;j++)
{
if(ans[j]&(1LL<<i))
{
tmp=ans[j];
break;
}
} if(tmp)
{
if(j!=k) swap(ans[j],ans[k]);//保证使用了这个数不会再用 优化
Ans=max(Ans,Ans^tmp);
for(j=k+;j<len;j++)
{
if(ans[j]&(1LL<<i)) ans[j]^=tmp;
}
k++;
} }
printf("Case #%d: %lld\n",ca++,Ans);
}
return ;
}

Ba Gua Zhen的更多相关文章

  1. The 2015 China Collegiate Programming Contest E. Ba Gua Zhen hdu 5544

    Ba Gua Zhen Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  2. 2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大

    Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom perio ...

  3. HDU 5544 Ba Gua Zhen dfs+高斯消元

    Ba Gua Zhen Problem Description During the Three-Kingdom period, there was a general named Xun Lu wh ...

  4. ACM学习历程—UESTC 1219 Ba Gua Zhen(dfs && 独立回路 && xor高斯消元)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1219 题目大意是给了一张图,然后要求一个点通过路径回到这个点,使得xor和最大. 这是CCPC南阳站的一道题 ...

  5. HDU 5544 Ba Gua Zhen ( 2015 CCPC 南阳 C、DFS+时间戳搜独立回路、线性基 )

    题目链接 题意 : 给出一副简单图.要你找出一个回路.使得其路径上边权的异或和最大 分析 : 类似的题有 BZOJ 2115 对于这种异或最长路的题目(走过的边可以重复走) 答案必定是由一条简单路径( ...

  6. 【C#公共帮助类】 Utils 10年代码,最全的系统帮助类

    为大家分享一下个人的一个Utils系统帮助类,可能有些现在有新的技术替代,自行修改哈~ 这个帮助类主要包含:对象转换处理 .分割字符串.截取字符串.删除最后结尾的一个逗号. 删除最后结尾的指定字符后的 ...

  7. 【C#公共帮助类】 Utils最全的系统帮助类

    最近闲的没事做,自己想着做一些东西,不知不觉居然在博客园找到了这么多公共类,感觉还是挺有用的,平时自己还是用到了好多,就是缺少整理,现在为大家分享一下一个Utils系统帮助类,可能有些现在有新的技术替 ...

  8. C#字符操作

    //字符串转ASCII码 // str1:字符串 str2:ASCII码 ] })[] == )//判断输入是否为字母 { str2= Encoding.GetEncoding(].ToString( ...

  9. 完整的系统帮助类Utils

    //来源:http://www.cnblogs.com/yuangang/p/5477324.html using System; using System.Collections.Generic; ...

随机推荐

  1. datetime 2017-10-21 10:09:02.560 转年月日的时间类型

    sql语句时间转年月日格式: 适用于多种时间格式 select  REPLACE(STUFF(CONVERT(char(10), REPLACE(CONVERT(varchar(10),'2017-1 ...

  2. (转)Asp.Net生命周期系列三

    原文地址:http://www.cnblogs.com/skm-blog/p/3178862.html 上文讲到了HttpRunTime主要做了三个事情,我们先回忆一下. 第一:雇佣了项目经理(Htt ...

  3. wgs84坐标系与gcj02坐标系转换误差分布图 | Mapping the Error in Transformation between WGS84 and GCJ02 Coordinations

    国际上通用的是wgs84坐标系,而我国对于境内的坐标进行了加密,采用了gcj02坐标系,或者称为火星坐标系.亢孟军老师带的一门课<多媒体电子地图设计>要求我们从wgs84坐标系转换为gcj ...

  4. memcpy用法

    函数原型 void *memcpy(void*dest, const void *src, size_t n); 功能 由src指向地址为起始地址的连续n个字节的数据复制到以destin指向地址为起始 ...

  5. easyui datagrid deleteRow(删除行)的BUG

    有时候想临时保存一些数据,等确定好后在批量一次提交,但EasyUI  datagrid 用的时候添加可以正常,如果从中间删除那行号就全乱了.导致删除的时候有可能删除上一行数据. function ad ...

  6. vue生态圈

    本文来自网易云社区 作者:刘凌阳 前言 公司社区上关于Vue的文章挺少的(少的可怜),不禁为Vue愤愤不平,此文应运而生. 但笔者水平有限,也写不了什么特别高深的东西,只能简单介绍下Vue生态圈,如有 ...

  7. 5种banner

      <!DOCTYPE html><html> <head>  <meta charset="UTF-8">  <tit ...

  8. 我的java问题排查工具单

    前言 平时的工作中经常碰到很多疑难问题的处理,在解决问题的同时,有一些工具起到了相当大的作用,在此书写下来,一是作为笔记,可以让自己后续忘记了可快速翻阅,二是分享,希望看到此文的同学们可以拿出自己日常 ...

  9. Xamarin如何使用终端设备的NFC功能传递卡号等信息给Web页面(Android)

    一.前提条件,App必须具有NFC权限. 二.项目中加入监控类NFCCatchActivity.cs [Activity(Label = "NFCCatch",Theme = &q ...

  10. 八大排序算法的python实现(一)直接插入排序

    刚参考网上的以及书上的资料把这八大算法又复习了一遍,感悟就是:有些算法原理真的很简单,一看就懂,但实现起来总是有这样那样的问题. 闲言少叙,先贴代码,之后再以自己的理解,以及自己在理解与实现的过程中遇 ...