1328人阅读 评论(0)

# 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）组数据

【类型】

【分析】

"给你g个数，让你在其中取出任意数量的数，使得选择的数的异或值尽可能大"这个问题。

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

*/

1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：266053次
• 积分：8170
• 等级：
• 排名：第2451名
• 原创：549篇
• 转载：0篇
• 译文：0篇
• 评论：133条
文章分类
最新评论