Ba Gua Zhen
Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 330 Accepted Submission(s): 102
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?
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(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.
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
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: 3HintA 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.
题意:求一个环路使得路径的xor和最大,环路上的边可以走多次。
BZOJ2115和这个题类似:点击打开链接
参考:http://www.cnblogs.com/andyqsmart/p/4968589.html
非常神奇优美的性质。把路径分解成若干个独立回路,然后最终的回路一定可以看成某些
独立回路的组合,然后从高位开始每一位都高斯消元寻找解。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define maxn 51111
#define maxm 211111
struct node {
int u, v, next;
long long w;
} edge[maxm];
int n, m;
int head[maxn], cnt;
long long s[maxm], tot;//独立回路
int vis[maxn];//时间戳
long long cur[maxn];//到当前节点为止的xor和
long long ans;
void add_edge (int u, int v, long long w) {
edge[cnt].u = u ,edge[cnt].v = v, edge[cnt].w = w, edge[cnt].next = head[u];
head[u] = cnt++;
}
void dfs (int u, int fa, int pre) {
vis[u] = pre;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
if (v == fa)
continue;
if (vis[v] != -1) {
if (vis[v] <= pre) //后继节点已经访问过
s[tot++] = cur[u]^cur[v]^edge[i].w;
}
else {
cur[v] = cur[u]^edge[i].w;
dfs (v, u, pre+1);
}
}
}
void solve () {
int row = 0;
for (int i = 61; i >= 0; i--) {
int j;
for (j = row; j < tot; j++) {
if (s[j]&((long long)1 << i))
break;
}
if (j < tot) {
swap (s[row], s[j]);
for (j = 0; j < tot; j++) if (j != row) {
if (s[j]&((long long)1 << i))
s[j] ^= s[row];
}
row++;
}
}
ans = 0;
for (int i = 0; i < row; i++) {
ans = max (ans, ans^s[i]);
}
printf ("%lld\n", ans);
}
int main () {
int t, kase = 0;
scanf ("%d", &t);
while (t--) {
printf ("Case #%d: ", ++kase);
scanf ("%d%d", &n, &m);
memset (head, -1, sizeof head);
cnt = tot = 0;
for (int i = 0; i < m; i++) {
int u, v;
long long w;
scanf ("%d%d%lld", &u, &v, &w);
add_edge (u, v, w);
add_edge (v, u, w);
}
memset (vis, -1, sizeof vis);
cur[1] = 0;
dfs (1, 0, 0);
solve ();
}
return 0;
}