您的位置:首页 > 其它

HDU 5544 独立回路,高斯消元,线性基

2017-03-05 15:47 441 查看
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 XOR circuit 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.

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.

题意:有一个n(<=50000)个顶点m(<=100000)条边的无向图,每条边有一个边权(0<=边权<2^60),求所有回路中边权xor和的最大值。

解法:任意选定一个起点,从起点出发开始dfs,同时记录路径xor和,每访问到一个访问过的点表明得到了一个环,将当前异或值与起点到当前点的xor和xor一下可以得到环上的xor和,由于xor的性质,任意一个回路都可以用前面得到的环来线性表示,于是对之前得到的xor和做一次xor高斯消元,得到一组向量基,令ans=0,枚举每个向量基,ans=max(ans,ans^base)更新答案即可,复杂度O(60*(n+m))。

这题其实重要的就是异或高斯消元,我们最终想把每个权值都用二进制表示,每一个基代表相应的向量的最高位是那个且是1,这样最后我们就可以贪心的把每一位变成1,而不用考虑其余低位的情况(因为二进制的性质)!

这时我们需要了解一些性质:假设集合A,其中我们将ai替换成ai xor a[j] i<>j,那么得到的新集合,这个集合中元素能xor出的值于集合A相同,而此时我们恰恰需要从高到低,把其余对应位是1的异或为0,此时无影响!

推荐一个高斯消元解异或方程的论文:见这里

//UESTC 1219

#include <bits/stdc++.h>
using namespace std;
const int maxn = 50005;
vector <pair<int , long long> > G[maxn];
vector <long long> ans;
long long Xor[maxn];
int vis[maxn];
void dfs(int x, int fa, long long Ans)
{
if(vis[x]){
long long p = Ans ^ Xor[x];
ans.push_back(p);
return ;
}
vis[x] = 1;
for(int i = 0; i < G[x].size(); i++){
int v = G[x][i].first;
if(v == fa) continue;
if(!vis[v]) Xor[v] = Ans ^ G[x][i].second;
dfs(v, x, Ans ^ G[x][i].second);
}
}
int main()
{
int T, n, m, ks = 0; scanf("%d", &T);
while(T--){
ans.clear();
memset(vis, 0, sizeof(vis));
memset(Xor, 0, sizeof(Xor));
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) G[i].clear();
for(int i = 1; i <= m; i++){
int x, y; long long z;
scanf("%d%d%lld", &x, &y, &z);
G[x].push_back(make_pair(y, z));
G[y].push_back(make_pair(x, z));
}
dfs(1, -1, 0);
int k = 0;
for(int i = 60; i >= 0; i--){
int j;
for(j = k; j < ans.size(); j++){
if((ans[j]&(1LL<<i)) != 0) break;
}
if(j == ans.size()) continue;
if(j != k) swap(ans[k], ans[j]);
for(j = k + 1; j < ans.size(); j++){
if((ans[j]&(1LL<<i)) != 0){
ans[j] ^= ans[k];
}
}
k++;
}
long long Ans = 0;
for(int i = 0; i < k; i++) Ans = max(Ans, Ans ^ ans[i]);
printf("Case #%d: %lld\n", ++ks, Ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: