【HDU5544 2015CCPC 南阳国赛E】【树上dfs找本质不同环 高斯消元 时间戳优化】Ba Gua Zhen 连通图上最大异或环

原创 2015年11月19日 15:31:55


Ba Gua Zhen

Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 213    Accepted Submission(s): 76


Problem 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 XORcircuit path in this puzzle to help him hack the puzzle?
 

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

Each test case begins with two integers N(2N5×104) and M(1M105) in one line. Then M lines follow. Each line contains three integers ui, viand wi(1ui,viN,0wi2601) 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
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.
 

Source
 


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=5e4+10,M=2e5+10,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int n,m,x,y;LL z;
int first[N];int id;
int w[M],nxt[M];LL c[M];
LL f[N],cir[M>>1];int g;
int dfn[N];int tim;
void ins(int x,int y,LL z)
{
	++id;
	w[id]=y;
	c[id]=z;
	nxt[id]=first[x];
	first[x]=id;
}
void dfs(int x)
{
	dfn[x]=++tim;
	for(int z=first[x];z;z=nxt[z])
	{
		int y=w[z];
		if(dfn[y]==0)
		{
			f[y]=f[x]^c[z];
			dfs(y);
		}
		else if(dfn[y]>dfn[x])
		{
			LL tmp=f[x]^c[z]^f[y];
			if(tmp)cir[++g]=tmp;
		}
	}
}
int main()
{
	scanf("%d",&casenum);
	for(casei=1;casei<=casenum;++casei)
	{
		scanf("%d%d",&n,&m);
		memset(first,0,(n+2)*4);id=0;
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%lld",&x,&y,&z);
			ins(x,y,z);
			ins(y,x,z);
		}
		memset(dfn,0,(n+2)*4);tim=0;
		g=0;f[1]=0;dfs(1);
		LL ans=0;
		for(int i=59;i>=0;i--)
		{
			LL bit=1ll<<i;
			LL v=-1;
			for(int j=1;j<=g;j++)if(cir[j]&bit)
			{
				v=cir[j];
				break;
			}
			if(~v)
			{
				gmax(ans,ans^v);
				for(int j=1;j<=g;j++)if(cir[j]&bit)cir[j]^=v;
			}
		}
		printf("Case #%d: %lld\n",casei,ans);
		if(g>2*m-(n-1))while(1);
	}
	return 0;
}
/*
【trick&&吐槽】
1,把图上的问题转化为树上,真是好办法啊
2,题目中提到2^60-1,而且还是对于异或值而言,于是我们应该想到按位从高到低贪心或高斯消元
3,因为反向边的干扰,这道题可能的环数不只是m-(n-1),而可能高达2m-(n-1),我们可以通过时间戳的方法简化

【题意】
T(1<=T<=30)组数据
对于每组数据,给你一个联通图,图上有n(2<=n<=5e4)个点,有m(1<=m<=1e5)条双向带权边。
然后问你这个图上异或和最大的环。

【类型】
高斯消元

【分析】
首先,我们从一点出发,再回到这个点,如果一条链走了2次(或者说偶数次),那么异或值变成了0
如果我们想产生不为0的异或值,需要走一个环走奇数次,也就是从某条路过去再从另外一条路回来。
我们发现,可以把任意一个环的异或和引入为我们的答案。
于是,我们如果可以得到所有环的异或和,把这些数来做高斯消元求最大异或和,答案也就出来了。

然而,想求出所有环,是会TLE的。
于是我们转化为求本质不同的环。
因为这个图是联通的,所以事实上从一个点就可达所有环。
于是我们干脆从点1开始做dfs,记录从1到每个点的异或值f[],
一旦从一个点沿边出发能够到达另外一个已经记录了f[]的点,那么我们就可以得到一个环的权值,
权值为f[x]^c[z]^f[y],我们记录下这所有本质不同的环。
这可以看做是由联通图的生成树加边所构成的环。所以环的数量g不会超过m-(n-1)

之后问题就变成了——
"给你g个数,让你在其中取出任意数量的数,使得选择的数的异或值尽可能大"这个问题。
这个问题要如何解决呢?类似于高斯消元的做法——
我们对于一个较高的位,肯定是"能选1的话一定贪心为1"

如果当前位为0,那么我们可以选择任意一个此位为1的数v。
然后把ans异或上这个数v,并把所有此位为1的数都异或消掉v。来使得后面低位的选数不会再对高位产生干扰。

如果当前位为1,那么我们肯定不会选择此位为1的数。
然而为了使得后面低位的选数不会再对这位产生干扰,我们还是要选择任意一个此位为1的数v。并把所有此位为1的数都异或消掉v。
这样这道题就可以AC啦。
这题是2015CCPC难度第9易的题目,做完这题就能拿到金牌了。

【时间复杂度&&优化】
O(n+m+60(m-n))

*/


版权声明:题解中哪里写错请一定要指出来QwQ 转载还请注明下出处哦,谢谢^_^

相关文章推荐

hdu5544 Ba Gua Zhen(高斯消元)

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

hdu 5544 Ba Gua Zhen

http://acm.hdu.edu.cn/showproblem.php?pid=5544题意: 有一个n(<=50000)个顶点m(<=100000)条边的无向图,每条边有一个边权(0<=边权<...

hdu5544 Pick The Sticks(dp)

还是得正视这场比赛了。。不说了,就放代码。 http://acm.hdu.edu.cn/showproblem.php?pid=5543 #include #include #include ...

HDU 5544 (独立回路 高斯消元)

Ba Gua Zhen Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Tota...

HDU 5544 Ba Gua Zhen (dfs&独立回路&异或消元)

这道题和BZOJ 2115 基本一样,容易一点,直接存个代码:这个代码要好看一点(好像并没有什么区别。。。)#include #define LL long long #define FOR(i,...

hdu 5544 Ba Gua Zhen(线性基+dfs)

题意: 一个无向图,每条边有值,找出无向图中所有的环的异或和,问这些值能异或出来的最大值。 解题思路: 去年的做的比赛里的题,今年才补上。 找环用dfs跑一遍图就可以找出来了,然后求最...

【HDU5547 2015 CCPC 南阳国赛H】【DFS】Sudoku 4x4棋盘的填充

#include #include #define MS(x,y) memset(x,y,sizeof(x)) int casenum,casei; int n; char s[10][10]; bo...

HDU-5833-Zhu and 772002【2016CCPC网络赛】【高斯消元】

1002-Zhu and 772002 题目大意:给出n个数字,问有多少种方式取(1~n)个数字使得成为一个完美平方数 题目思路: 求完全平方数,质因子的范围也知道,所以我们可以对每个质因子列出一个模...

2016ccpc 1002(hdu5833)题解 (高斯消元求异或方程组自由变元)

比赛结束才知道是个高斯消元的题目,吓得我赶紧学了一发,然后惊讶的发现白皮书上原题QAQ. 由于刚学会,虽然是手敲但有些细节还是比对了模板,所以并不能解释,先放一发代码,等熟练了再补. 代码: #i...

HDU 3364 - Lanterns (高斯消元 + 异或方程组)

RTR
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)