2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大
Ba Gua Zhen
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
无
Description
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 XOR circuit path in this puzzle to help him hack the puzzle?
Input
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
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.
题意
有一个n(<=50000)个顶点m(<=100000)条边的无向图,每条边有一个边权(0<=边权<2^60),求所有回路中边权xor和的最大值。
题解:
首先dfs,跑出所有环的xor值,比如里面有k个环
然后我们就可以转化为,给你k个数,让你选择任意多的数,使得异或值最大
这个要用高斯消元做:http://wenku.baidu.com/link?url=BFic5zoh7tkGkLTgrRta5OtFliMsghlACqlx-XyjMFPgLh14ujAo33SDtLbFhHYN6JoGt2b1d9XsxMP97Degfpb8QzUs_eZJNEwgbhPVScO
代码:
#include<iostream>
#include<math.h>
#include<vector>
#include<stdio.h>
#include<cstring>
using namespace std;
#define maxn 50005
vector<pair<int,long long> >G[maxn];
vector<long long> ans;
long long Xor[maxn];
int vis[maxn];
void dfs(int x,int pre,long long Ans)
{
if(vis[x])
{
long long p = Ans ^ Xor[x];
ans.push_back(p);
return;
}
vis[x]=;
for(int i=;i<G[x].size();i++)
{
int v = G[x][i].first;
if(pre == v)continue;
if(!vis[v])Xor[v]=Ans^G[x][i].second;
dfs(v,x,Ans ^ G[x][i].second);
}
}
int main()
{
int t;scanf("%d",&t);
for(int cas=;cas<=t;cas++)
{
ans.clear();
memset(vis,,sizeof(vis));
memset(Xor,,sizeof(Xor));
int n,m;scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
G[i].clear();
for(int i=;i<=m;i++)
{
int x,y;long long z;
scanf("%d%d%lld",&x,&y,&z);
G[x].push_back(make_pair(y,z));
G[y].push_back(make_pair(x,z));
}
dfs(,-,);
int k=;
for(int i=;i>=;i--)
{
int j;
for(j=k;j<ans.size();j++)
if((ans[j]&(1LL<<i))!=)
{
break;
}
if (j==ans.size()) continue;
if (j!=k)
swap(ans[k],ans[j]);
//cout<<d[j]<<endl;
for (j=k+;j<ans.size();j++)
if ((ans[j]&(1LL<<i))!=)
ans[j]^=ans[k];
k++;
}
long long Ans = ;
for(int i=;i<k;i++)
Ans = max(Ans,Ans ^ ans[i]);
printf("Case #%d: %lld\n",cas,Ans);
}
}
2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大的更多相关文章
- 【BZOJ-1923】外星千足虫 高斯消元 + xor方程组
1923: [Sdoi2010]外星千足虫 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 766 Solved: 485[Submit][Status ...
- BZOJ 2844: albus就是要第一个出场 [高斯消元XOR 线性基]
2844: albus就是要第一个出场 题意:给定一个n个数的集合S和一个数x,求x在S的$2^n$个子集从小到大的异或和序列中最早出现的位置 一开始看错题了...人家要求的是x第一次出现位置不是第x ...
- HDU 3949 XOR [高斯消元XOR 线性基]
3949冰上走 题意: 给你 N个数,从中取出若干个进行异或运算 , 求最后所有可以得到的异或结果中的第k小值 N个数高斯消元求出线性基后,设秩为$r$,那么总共可以组成$2^r$中数字(本题不能不选 ...
- BZOJ 3105: [cqoi2013]新Nim游戏 [高斯消元XOR 线性基]
以后我也要用传送门! 题意:一些数,选择一个权值最大的异或和不为0的集合 终于有点明白线性基是什么了...等会再整理 求一个权值最大的线性无关子集 线性无关子集满足拟阵的性质,贪心选择权值最大的,用高 ...
- BZOJ 1923: [Sdoi2010]外星千足虫 [高斯消元XOR]
1923: [Sdoi2010]外星千足虫 对于 100%的数据,满足 N≤1,000,M≤2,000. 裸高斯消元解异或方程组 给定方程顺序要求用从上到下最少的方程,那么找主元时记录一下最远找到哪个 ...
- POJ1222 EXTENDED LIGHTS OUT 高斯消元 XOR方程组
http://poj.org/problem?id=1222 在学校oj用搜索写了一次,这次写高斯消元,haoi现场裸xor方程消元没写出来,真实zz. #include<iostream> ...
- BZOJ 1770: [Usaco2009 Nov]lights 燈 [高斯消元XOR 搜索]
题意: 经典灯问题,求最少次数 本题数据不水,必须要暴搜自由元的取值啦 想了好久 然而我看到网上的程序都没有用记录now的做法,那样做遇到自由元应该可能会丢解吧...? 我的做法是把自由元保存下来,枚 ...
- BZOJ 3569: DZY Loves Chinese II [高斯消元XOR 神题]
http://www.lydsy.com/JudgeOnline/problem.php?id=3569 题意:多次询问一个无向连通图当图中某k条边消失时这个图是否联通 强制在线 太神啦啦啦啦啦啦啦啦 ...
- BZOJ 2115: [Wc2011] Xor [高斯消元XOR 线性基 图]
啦啦啦 题意: N 个点M条边的边带权的无向图,求1到n一条XOR和最大的路径 感觉把学的东西都用上了.... 1到n的所有路径可以由一条1到n的简单路径异或上任意个简单环得到 证明: 如果环与路径有 ...
随机推荐
- mysql大内存高性能优化方案
mysql优化是一个相对来说比较重要的事情了,特别像对mysql读写比较多的网站就显得非常重要了,下面我们来介绍mysql大内存高性能优化方案 8G内存下MySQL的优化 按照下面的设置试试看:key ...
- hdu 2457(ac自动机+dp)
题意:容易理解... 分析:这是一道比较简单的ac自动机+dp的题了,直接上代码. 代码实现: #include<stdio.h> #include<string.h> #in ...
- [Everyday Mathematics]20150128
求极限 $$\bex \lim_{x\to 0}\sex{\frac{e^x+e^{2x}+\cdots+e^{nx}}{n}}^\frac{1}{x}. \eex$$
- ylbtech-SubwayNav(地铁线路导航)-数据库设计
ylbtech-DatabaseDesgin:ylbtech-SubwayNav(地铁线路导航)-数据库设计 DatabaseName:SubwayNav(地铁线路导航) Type:线路导航 1.A, ...
- 《Python基础教程(第二版)》学习笔记 -> 第十章 充电时刻 之 标准库
SYS sys这个模块让你能够访问与Python解释器联系紧密的变量和函数,下面是一些sys模块中重要的函数和变量: 函数和变量 描述 argv 命令行参数,包括脚本和名称 exit([arg]) ...
- C#实现对文件目录的实时监控
本文主要描述如何通过C#实现实时监控文件目录下的变化,包括文件和目录的添加,删除,修改和重命名等操作. 首先,我们需要对.net提供的FileSystemWatcher类有所了解.我有些懒,找了MSD ...
- VMtools安装以及设置
一.安装VMtools 点击VMware菜单的——虚拟机——安装VMware Tools,在弹出的对话框中选择“安装”.这时,在Ubuntu下会自动加载Linux版的VMware Tools的安装光盘 ...
- new trip
离开YY已经快一周了,特别感谢以前的老大姚冬和朱云峰,从他俩身上学到了很多.这个决定也经过了很长的纠结,不想再做个犹豫不决的人,所以最后还是坚定了最初的信念,也算是对半年前自己的一个完好交代,以防将来 ...
- 【和我一起学python吧】初学Python,版本如何选择?
早在四年多以前,在我进入英才网之前,去面试过一家海归创业的公司.他们需要的是有unix开发经验的技术人员,但是因为他们当时所处的阶段对很多成熟 技术人员不是很吸引,所以条件放宽为熟悉面向对象的程序开发 ...
- 机器学习真的可以起作用吗?(2)(以二维PLA算法为例)
一个问题:大多数情况下,M(hypothesis set的大小)是无穷大的,例如PLA算法.那么是不是我们的原则1就不能使用了? 我们试着做一些努力: Step1:寻找hypothesis set的e ...