Ba Gua Zhen

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

Description

During the Three-Kingdom period, there was a general named Xun Lu who belonged to Kingdom Wu. Once his troop were chasing Bei Liu, he was stuck in the Ba Gua Zhen from Liang Zhuge. The puzzle could be considered as an undirected graph with N vertexes and M edges. Each edge in the puzzle connected two vertexes which were ui and vi with a length of wi. Liang Zhuge had great interests in the beauty of his puzzle, so there were no self-loops and between each pair of vertexes, there would be at most one edge in the puzzle. And it was also guaranteed that there was at least one path to go between each pair of vertexes.

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

The first line of the input gives the number of test cases, T(1≤T≤30). T test cases follow.

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

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum XOR sum of one circuit path in the puzzle.

Sample Input

23 31 2 11 3 22 3 06 71 2 11 3 12 3 13 4 44 5 24 6 25 6 2

Sample Output

Case #1: 3Case #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]=1;for(int i=0;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=1;cas<=t;cas++){ans.clear();memset(vis,0,sizeof(vis));memset(Xor,0,sizeof(Xor));int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)G[i].clear();for(int i=1;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(1,-1,0);int k=0;for(int i=60;i>=0;i--){int j;for(j=k;j<ans.size();j++)if((ans[j]&(1LL<<i))!=0){break;}if (j==ans.size()) continue;if (j!=k)swap(ans[k],ans[j]);//cout<<d[j]<<endl;for (j=k+1;j<ans.size();j++)if ((ans[j]&(1LL<<i))!=0)ans[j]^=ans[k];k++;}long long Ans = 0;for(int i=0;i<k;i++)Ans = max(Ans,Ans ^ ans[i]);printf("Case #%d: %lld\n",cas,Ans);}
}

2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大相关推荐

  1. hdu 5544 Ba Gua Zhen

    link 因为在沈阳站的比赛中我们没有做出来H题,后来才知道是高斯消元.于是我先来补一下高斯消元的题目.先做了hdu XOR对线性基有了一定的了解,然后来做一下南阳ccpc的Ba Gua Zhen 分 ...

  2. hdu5544 Ba Gua Zhen(高斯消元)

    思路:首先DFS把图上的所有环找出来,然后就是K个数里面任意选数使得异或和最大,高斯消元就好了 异或方程的高斯消元见 点击打开链接 #include<bits/stdc++.h> usin ...

  3. Ba Gua Zhen

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5544 学习链接:https://www.cnblogs.com/qscqesze/p/4902518. ...

  4. UESTC OJ1219 Ba Gua Zhen

    链接:http://mozhu.today/#/problem/show/1219 题意:给定n个点m条边有边权的无向图,求一个异或值最大的回路. 分析:其实就是将所有环的异或值找出来然后任选一些组成 ...

  5. [HDU] 5544 Ba Gua Zhen

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5544 题目大意:T组数据, 给一个n个点, m条边的无向连通图, 无自环, 无重边, 求最大XOR回 ...

  6. UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)...

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  7. YbtOJ#893-带权的图【高斯消元,结论】

    正题 题目链接:https://www.ybtoj.com.cn/problem/893 题目大意 给出一张nnn个点mmm条边的无向联通图,每条边正反向各有A,B,CA,B,CA,B,C三种边权. ...

  8. HDU 4870 Rating(高斯消元 )

    HDU 4870   Rating 这是前几天多校的题目,高了好久突然听旁边的大神推出来说是可以用高斯消元,一直喊着赶快敲模板,对于从来没有接触过高斯消元的我来说根本就是一头雾水,无赖之下这几天做DP ...

  9. 解线性方程组——高斯消元の板子

    ATP记得它在很久以前看过一点点高斯消元的东西然后做过一点点题目..但是当时实在是太zz了所以本来就没有很懂这个东西现在更是忘得差不多了.. 所以现在就当重新学一遍了QwQ 一点口胡的解释 高斯消元. ...

最新文章

  1. Kprobe在Linux kernel debug中的应用
  2. Python--判断一个字符串是否包含某子串的几种方法
  3. San介绍以及在百度APP的实践
  4. 2020CCPC绵阳
  5. 牛刀小试、用SharePoint 实现请假管理功能
  6. 代码质量度量标准_追求代码质量(2): 监视圈复杂度
  7. Lucene.Net的中文分词组件AdvancedChineseAnalyzer
  8. 【图像分割】基于matlab蚁群优化模糊聚类图像分割【含Matlab源码 130期】
  9. [中文翻译] ASP.NET 5 简介(Introducing ASP.NET 5,原作ScottGu 2015/2/23)
  10. VAssistX失效后重新安装双击没反应
  11. jQuery图片LightBox插件 点击图片放大 支持移动手机
  12. c语言图像的简单叠加,第10章C语言图形编程.ppt
  13. animals中文谐音_魔力红歌曲Aanimals中文谐音,就是音译歌词
  14. JDK8下载安装与Win10下Java环境变量配置
  15. Linksys e3200初试tomato系统
  16. 数字金额转化为大写金额(js)
  17. Docker Swarm 练习:投票 App
  18. 加密勒索病毒:诞生、忽视以及爆炸式增长
  19. linux向日葵无法启动, 提示错误 connect is error
  20. ios appstore 审核 Guideline 5.2.2 - Legal ios新闻资讯类APP5.2.2被拒

热门文章

  1. 电视剧中一看就崩溃的镜头
  2. 为何要从用户角度出发来思考问题
  3. python画猫和老鼠代码_猫捉老鼠游戏(Python)
  4. 大学生电子设计竞赛总结
  5. 夜来风雨声,Python协程知多少
  6. 电话号码对应英文单词 (python)
  7. 知三维空间中任意旋转抛物面的顶点和焦点坐标,建立该旋转抛物面方程
  8. 刚子:走马观花奋达创“芯”发布会
  9. 解决提交到github报错Please tell me who you are.和为不同的项目设置不同的名称
  10. div+css使用padding样式和 important标记实现Firefox和IE6处理带float样式的margi