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; ...
随机推荐
- 问渠那得清如许?为有源头活水来。——java面向对象的思想
20169205 2016-2017-2 <移动平台应用开发实践>第2周学习总结 教材学习内容总结 本次作业要求的部分主要是Java高级语言实现面向对象编程的基本方法,其中所介绍的面向对象 ...
- 安装Xamarin.Android几个经典介面
昨晚Microsoft MVP的身份来申请Xamarin.Android,想不到今早就有邮件回复.花上些少时间订阅与注册: 望有时间能学习到一些新技术. 下面是Insus.NET下载并安装,留下几个经 ...
- .net core .NET Core与.NET Framework、Mono之间的关系
.NET Core与.NET Framework.Mono之间的关系 首先想要知道.NET Core与.NET Framework.Mono之间的关系,就必须他们分别是什么,有什么用途? 一. .ne ...
- Java以邮件附件的方式发送excel文件
String to = "xxx@qq.com"; // 收件人的QQ邮箱 String from = "xxx@qq.com"; // 发件人的QQ邮箱 St ...
- hive的安装与配置 mysql安装 启动
三种模式 内嵌模式:元数据保持在内嵌的derby模式,只允许一个会话连接 本地独立模式:在本地安装Mysql,吧元数据放到mySql内 远程模式:元数据放置在远程的Mysql数据库 1.下载Hive安 ...
- 多线程《五》GIL全局解释器锁
一 引子 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native t ...
- javascript按键盘上/右/下/左箭头加速运动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【bzoj4103】[Thu Summer Camp 2015]异或运算 可持久化trie树
Description 给定长度为n的数列X={x1,x2,...,xn}和长度为m的数列Y={y1,y2,...,ym},令矩阵A中第i行第j列的值Aij=xi xor yj,每次询问给定矩形区域i ...
- 'javac' 不是内部或外部命令,也不是可运行的程序
win10 系统下'javac' 不是内部或外部命令,也不是可运行的程序 1.在系统变量下面配置 JAVA_HOME:你自己的jdk的路径 CLASSPATH= .;%JAVA_HOME%libdt. ...
- 数学【洛谷P4071】 [SDOI2016]排列计数
P4071 [SDOI2016]排列计数 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列 ...