Ba Gua Zhen
题目链接: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
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?
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.
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
Case #2: 3
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.
#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的更多相关文章
- 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 ...
- 2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大
Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom perio ...
- HDU 5544 Ba Gua Zhen dfs+高斯消元
Ba Gua Zhen Problem Description During the Three-Kingdom period, there was a general named Xun Lu wh ...
- ACM学习历程—UESTC 1219 Ba Gua Zhen(dfs && 独立回路 && xor高斯消元)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1219 题目大意是给了一张图,然后要求一个点通过路径回到这个点,使得xor和最大. 这是CCPC南阳站的一道题 ...
- HDU 5544 Ba Gua Zhen ( 2015 CCPC 南阳 C、DFS+时间戳搜独立回路、线性基 )
题目链接 题意 : 给出一副简单图.要你找出一个回路.使得其路径上边权的异或和最大 分析 : 类似的题有 BZOJ 2115 对于这种异或最长路的题目(走过的边可以重复走) 答案必定是由一条简单路径( ...
- 【C#公共帮助类】 Utils 10年代码,最全的系统帮助类
为大家分享一下个人的一个Utils系统帮助类,可能有些现在有新的技术替代,自行修改哈~ 这个帮助类主要包含:对象转换处理 .分割字符串.截取字符串.删除最后结尾的一个逗号. 删除最后结尾的指定字符后的 ...
- 【C#公共帮助类】 Utils最全的系统帮助类
最近闲的没事做,自己想着做一些东西,不知不觉居然在博客园找到了这么多公共类,感觉还是挺有用的,平时自己还是用到了好多,就是缺少整理,现在为大家分享一下一个Utils系统帮助类,可能有些现在有新的技术替 ...
- C#字符操作
//字符串转ASCII码 // str1:字符串 str2:ASCII码 ] })[] == )//判断输入是否为字母 { str2= Encoding.GetEncoding(].ToString( ...
- 完整的系统帮助类Utils
//来源:http://www.cnblogs.com/yuangang/p/5477324.html using System; using System.Collections.Generic; ...
随机推荐
- /dev/null 位桶
把/dev/null看作"黑洞". 它非常等价于一个只写文件. 所有写入它的内容都会永远丢失. 而尝试从它那儿读取内容则什么也读不到. 然而, /dev/null对命令行和脚本都非 ...
- CSS3的2D与3D转换
2D和3D转换涉及到数学中的知识,作为一个数学专业的毕业生,不研究一下岂不是对不起自己的专业? 首先来看几个参数: 1.transform-origin:origin(起源,起点),也即变形的起点,在 ...
- Java集合类总结 (五)
集合框架 为了避免进行不必要的随机访问操作,Java引入了一种标签接口RandomAccess, 这个接口没有任何方法,只是一个标签,用来标记一个集合是否应该进行随机访问操作: if (c insta ...
- LSI SAS3IRCU配置SAS3系列RAID卡
LSI SAS3IRCU配置SAS3系列RAID卡 一.适用的controller LSISAS3008 LSISAS3004 二.名词解释 Controller: IR: Volume: 卷,基于物 ...
- 客户端 post ,get 访问服务器
private void sendReuestExpansion() { HttpRequest<T> req = this; HttpWebRequest request; try { ...
- CentOS目录与文件操作
pwd:查看当前目录 touch:创建文件 touch a.c ls:查看当前目录下文件,也可以ls /tmp查看tmp下的文件 rm:删除文件 rm a.c,也可以rm a.c -rf 强制删除 c ...
- 汇编Shellcode的技巧
汇编Shellcode的技巧 来源 https://www.4hou.com/technology/3893.html 本文参考来源于pentest 我们在上一篇提到要要自定义shellcode,不 ...
- NPOI.XWPF生成WORD,设置Table单元格的背景色
tr.GetCell().SetColor("#fbd4b4");
- php tp5常用小知识
1. tp5 获取当前访问的模块名,控制器名,方法名 $request= \think\Request::instance(); $module = $request->module(); // ...
- linux下蓝牙开发(bluez应用)
编译blueZ-5.25 需要先编译安装以下包: bluez-libs-3.36.tar.gz expat-2.1.0.tar.gz dbus-1.10.0.tar.gz glib-2.26.1.ta ...