P4151 [WC2011]最大XOR和路径
求无向图中1到n的路径中,边权异或最大的一条路径异或和
求异或和,异或的操作就要想到线性基
性基是一个数的集合,并且每个序列都拥有至少一个线性基,取线性基中若干个数异或起来可以得到原序列中的任何一个数。
线性基三大性质
原序列里面的任意一个数都可以由线性基里面的一些数异或得到
线性基里面的任意一些数异或起来都不能得到 0 00
线性基里面的数的个数唯一,并且在保持性质一的前提下,数的个数是最少的
https://www.luogu.com.cn/problem/P4151
//对于某条1到n的路径,如果存在某条路径更优,那么首先这条路径与另一条路径构成环
//那么这条路径与环的异或就是另一条路径,所以找出所有环的异或和,构造线性基
//找所有环可以直接dfs,也可以用并查集
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
const int maxn = 1e6 + 10;
//const ll inf=0x3f3f3f3f3f3f3f3f;
const int INF=0x3f3f3f3f;
inline int rd() {
    int x = 0; char ch = getchar(); bool f = 1;
    while(ch > '9' || ch < '0') {
        if(ch == '-')   f ^= 1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + ch - 48;
        ch = getchar();
    }
    return f ? x : -x;
}
#define dbg(x...) \
    do { \
        cout << #x << " -> "; \
        err(x); \
    } while(0)
void err() {
    cout << endl;
}
template<class T, class ...Ts>
void err(const T&arg, const Ts& ...args) {
    cout << arg << " ";
    err(args...);
}
int head[maxn], to[maxn], nxt[maxn], val[maxn], ecnt;
inline void add(int u, int v, int w) {
    to[++ecnt] = v; nxt[ecnt] = head[u]; head[u] = ecnt; val[ecnt] = w;
    to[++ecnt] = u; nxt[ecnt] = head[v]; head[v] = ecnt; val[ecnt] = w;
}
int dis[maxn], vis[maxn];
int n, m, len = 63;
struct UCset {
	int f[maxn];
	ll w[maxn];
	void build(){for(int i = 1; i <= n; ++ i)   f[i] = i, w[i] = 0;}
	int get(int x) {
		if(f[x] == x)   return x;
		get(f[x]);
		w[x] ^= w[f[x]];
		f[x] = f[f[x]];
		return f[x];
	}
	ll un(int x, int y, ll v) {
		get(x); get(y);
		if(f[x] == f[y])    return w[x] ^ w[y] ^ v;
		w[f[x]] = w[x] ^ w[y] ^ v;
		f[f[x]] = f[y];
		return -1;
	}
}T;
struct LBase{
    int bit[64], cnt;
    int add(int x) {
        for(int i = len; i >= 0; -- i)
            if(x & (1ll << i)) {
                if(!bit[i]) {
                    bit[i] = x;
                    cnt ++;
                    return 1;
                }
                x ^= bit[i];
            }
        return 0;
    }
    void dfs(int x, int fa, int res) {//图的异或路径和,即求出所有环的异或 的线性基,再和任意一条x to y的路径异或和最大即可
        dis[x] = res; vis[x] = 1;
        for(int i = head[x]; i; i = nxt[i]) {
            if(to[i] == fa) continue;
            if(!vis[to[i]]) dfs(to[i], x, res ^ val[i]);
            else add(dis[x] ^ dis[to[i]] ^ val[i]);
        }
    }
    int getmax(int res = 0) {
        for(int i = len; i >= 0; -- i)  if((res ^ bit[i]) > res)    res ^= bit[i];
        return res;
    }
    int getbit() {
        return cnt;
    }
}lb;
signed main() {
    n = rd(), m = rd(); len = 63;
    T.build();
    for(int i = 0; i < m; ++ i) {
        int u = rd(), v = rd(), w = rd();
        add(u, v, w);
        ll res = T.un(u, v, w);
        if(res != -1)   lb.add(res);
    }
    //lb.dfs(1, 1, 0);
    //printf("%lld\n", lb.getmax(dis[n]));
    printf("%lld\n", lb.getmax(T.un(1, n, 0)));
    //system("pause");
    return 0;
}
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号