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


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 ,   vi and   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))

*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值