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; ...
随机推荐
- 2张图简单分析count(0)与count(*)
以前一直以为count(0)查询效率比count(*)比较高,原因大概是这么认为count(0)只是第一列进行统计,而count(*)所有列放在一起统计(亲,不要误会,这里不是所有列累加哦) 结果真的 ...
- 以太坊系列之六: p2p模块--以太坊源码学习
p2p模块 p2p模块对外暴露了Server关键结构,帮助上层管理复杂的p2p网路,使其集中于Protocol的实现,只关注于数据的传输. Server使用discover模块,在指定的UDP端口管理 ...
- 【大数据之数据仓库】GreenPlum PK DeepGreen(TPCH)
1.背景 一张UML类图可以简单的说明GreenPlum和DeepGreen之间的关系: GreenPlum: 主页:http://greenplum.org/ 源码:开源,https://githu ...
- ubuntu 安装 删除 卸载 Deb 包文件
图形界面: 安装deb 直接双击图标,输入密码后就可自动安装. 卸载deb 1. 菜单-系统->系统管理->新立得软件包管理器 或 Alt+F2(运行窗口)输入 sudo synaptic ...
- SpringMVC 入门程序
SpringMVC是什么 SpringMVC 是一种基于 Java 的实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架,属于 Spring FrameWork 的后续产品,已经融合在 Spr ...
- 题解 P1255 【数楼梯】
题目链接 好吧,承认python 轻松水过 代码奉上: n = int(input()) #定义,输入 a=1 #初始的变量赋值 b=1 n-=1 #我的毒瘤的循环不得不加上这句话 if n > ...
- 洛谷P3357 最长k可重线段集问题(费用流)
传送门 其实和最长k可重区间集问题差不多诶…… 把这条开线段给压成x轴上的一条线段,然后按上面说的那种方法做即可 然而有一个坑点是线段可以垂直于x轴,然后一压变成一个点,连上正权环,求最长路……然后s ...
- MySQL索引的索引长度问题
转自:http://samyubw.blog.51cto.com/978243/223773 MySQL的每个单表中所创建的索引长度是有限制的,且对不同存储引擎下的表有不同的限制. 在MyISAM表中 ...
- Centos查看端口占用令
Centos查看端口占用情况命令,比如查看80端口占用情况使用如下命令: lsof -i tcp:80 列出所有端口 netstat -ntlp 1.开启端口(以80端口为例) 方法一: /sbin/ ...
- 局域网内搭建一个服务器,可以使用 https 吗
https://www.v2ex.com/t/472394 这是一个创建于 126 天前的主题,其中的信息可能已经有所发展或是发生改变. 局域网内通过嵌入式设备搭建一个轻量级 web 服务,可以仍然使 ...