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; ...
随机推荐
- HttpUploader7-授权码配置
1.1. 七牛云存储 配置方式: 1.配置授权码 2.配置云存储 3.配置空间名称 4.配置上传地址 1.2. 阿里云存储 配置方式: 1.填写授权码 2.配置云存储为阿里云 ...
- easyUI Methods
doc对象转jQuery 对象 $(doc Object); jQuery Object.控件名('方法'[,参数]); options 为该控件的属性 方式一: var opts = $('.eas ...
- ASP.NET多页面传递数据,附框架源码
很多时候我们需要把数据传递到多个页面,比如表单提交可以指定提交数据到某个页面,那么关闭某个页面怎么把数据传递到上一个页面或者它的父页面. 在这里我附一段源码用于当前页面关闭指定某个页面刷新. 子页面方 ...
- 编写高质量代码改善C#程序的157个建议——建议53:必要时应将不再使用的对象引用赋值为null
建议53:必要时应将不再使用的对象引用赋值为null 在CLR托管的应用程序中,存在一个“根”的概念,类型的静态字段.方法参数.以及局部变量都可以作为“根”的存在(值类型不能作为“根”,只有引用类型的 ...
- C#操作excel打印
using System; using System.Data; using System.IO; using System.Runtime.InteropServices; using System ...
- C# 密封(2)
上一章节说到 sealed 作用于类,那么sealed 作用到方法和成员上面该如何呢. 在C# 中 Sealed作用于方法必须是重写之后的方法.也就是override+sealed.在之后别的类在继 ...
- npm更换为镜像
背景:npm在安装的过程中需用从国外的网站获取代码,速度非常慢,因此更换为淘宝镜像,提升速度. 1.永久起作用: npm config set registry https://registry.ta ...
- 解决org.hibernate.NonUniqueObjectException的问题
不知道是不是之前处理懒加载的问题对session工厂进行了处理,导致了之前没有问题的地方出现了错误. 当修改班级操作时出现了错误 前端错误信息 后台处理以及报错信息 16:37:36,034 ERRO ...
- 【转】c#中@的3种作用
源地址:https://www.cnblogs.com/linkbiz/p/6380814.html
- c++多线程基础2(命名空间 this_thread)
整理自:zh.cppreference.com/w/cpp/thread std::this_thread::yield: 定义于头文件 <thread> 函数原型:void yield( ...