Ba Gua Zhen HDU - 5544

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

题意:找一个回路,使得回路上的所有的边的异或和最大;
做法:把图拆成最小环,这题就是找一些最小环,然后求他们的最大异或和,这个可以用高斯消元去做,首先找出所有环,对1进行dfs,xo[i]是每个点储存从1到i的所有边的异或和,如果一个点的儿子被走过,那么这里就有一个最小环,可以通过xo[i]^x[x]&d(d是两点之间边的长度)得到,
对于每一个最小环,要么它是直接由i与x的祖先和他们之间的边构成要么就是通过这个圆和前面的圆得到。

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+7;
vector<pair<int,long long> > G[N];
long long xo[N];
bool vis[N];
vector<long long> vp;
long long a[70];
void dfs(int x,int f){
    vis[x] = false;
    for(int i = 0;i < G[x].size();i ++){
        pair<int,long long> now = G[x][i];
        if(vis[now.first]){
            xo[now.first] = xo[x]^now.second;
            dfs(now.first,x);
        }
        else if(now.first != f){
            //cout <<x << ' '<<now.first << ' '<< xo[now.first] << ' '<<xo[x] << ' '<< now.second << ' '<< (xo[now.first]^xo[x]^now.second) << endl;
            vp.push_back(xo[now.first]^xo[x]^now.second);
        }
    }
}

long long solve(){
    memset(a,0,sizeof(a));
    for(int i = 0;i < vp.size();i ++){
        for(int j = 59;j >=0;j --){
            if((vp[i]>>j)&1){
                if(a[j]){
                    vp[i] ^= a[j];
                }
                else{
                    a[j] = vp[i];
                    break;
                }
            }
        }
    }
    for(int i = 0;i < 60;i ++){
        for(int j = i+1;j < 60;j ++){
            if((a[j]>>i)&1) a[j] ^= a[i];
        }
    }
    long long ans = 0;
    for(int i = 0;i < 60;i ++) ans ^= a[i];
    return ans;
}

int main(){
    int T;
    cin >> T;
    for(int kase = 1;kase <= T;kase ++){
        int n,m;
        scanf("%d %d",&n,&m);
        for(int i = 1;i <= n;i ++) G[i].clear();
        for(int i = 1;i <= m;i ++){
            int u,v;
            long long w;
            scanf("%d %d %lld",&u,&v,&w);
            G[u].push_back({v,w});
            G[v].push_back({u,w});
        }
        vp.clear();
        memset(vis,true,sizeof(vis));
        dfs(1,-1);
        long long now = solve();
        printf("Case #%d: %lld\n",kase,now);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/zstu_zy/article/details/81060157