Ba Gua Zhen
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?
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, viand wi(1≤ui,vi≤N,0≤wi≤260−1) to describe all the edges in the puzzle.
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
Case #2: 3
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.
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 5e5+, M = 1e3+, mod = 1e9+, inf = 2e9; int head[N],t,cas = ;
struct ss{
int to,next;
LL value;
}e[N*];
int vis[N],cnt,T,n,m;
LL dep[N],ins[N],a[N];
void add(int u,int v,LL w) {
e[t].next = head[u];
e[t].to = v;
e[t].value = w;
head[u] = t++;
}
void dfs(int u,int f) {
vis[u] = vis[f] + ;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(to == f || (vis[to] && vis[to] < vis[u])) continue;
if(vis[to]) {
a[++cnt] = e[i].value ^ dep[to] ^ dep[u];
continue;
}
dep[to] = dep[u] ^ e[i].value;
dfs(to,u);
}
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
memset(dep,,sizeof(dep));
memset(ins,,sizeof(ins));
t = ;
cnt = ;
for(int i = ; i <= m; ++i) {
int u,v;LL w;
scanf("%d%d%I64d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
dfs(,);
for(int i = ; i <= cnt; ++i) {
for(int j = ; j >= ; --j) {
if(a[i]&(1LL<<j)) {
if(!ins[j]) {
ins[j] = a[i];
break;
}
a[i] ^= ins[j];
}
}
}
LL ans = ;
for(int i = ; i >= ; --i) {
if((ins[i]^ans) > ans) ans^=ins[i];
}
printf("Case #%d: %I64d\n",cas++,ans);
}
return ;
}