BZOJ2115 [Wc2011] Xor 【线性基】

来源:互联网 发布:淘宝上图片多大尺寸 编辑:程序博客网 时间:2024/06/05 06:52

2115: [Wc2011] Xor

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 3915  Solved: 1633
[Submit][Status][Discuss]

Description

Input

第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目。 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边。 图中可能有重边或自环。

Output

仅包含一个整数,表示最大的XOR和(十进制结果),注意输出后加换行回车。

Sample Input

5 7
1 2 2
1 3 2
2 4 1
2 5 1
4 5 3
5 3 4
4 3 2

Sample Output

6

HINT


弄了那么久还是讲不清楚线性基是什么

大概就是在异或时去除掉一些重复的元素,使得剩下的元素异或不出0且值域覆盖原来的异或值域?

学完回来补坑


除此之外,就是找到1到N的一条路径,对于路径上所有的环记录下来进行一次高斯消元,贪心异或即可

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define LL long long int#define REP(i,n) for (int i = 1; i <= (n); i++)#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)using namespace std;const int maxn = 50005,maxm = 200005,INF = 1000000000;inline LL RD(){LL out = 0,flag = 1; char c = getchar();while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}while (c >= 48 && c <= 57) {out = (out << 1) + (out << 3) + c - '0'; c = getchar();}return out * flag;}LL N,M,d[maxn],V[maxm],A[65],cir = 0,tot = 0,bin[65];bool vis[maxn];int head[maxn],nedge = 0;struct EDGE{LL to,w,next;}edge[maxm];inline void build(int u,int v,LL w){edge[nedge] = (EDGE){v,w,head[u]}; head[u] = nedge++;edge[nedge] = (EDGE){u,w,head[v]}; head[v] = nedge++;}void dfs(int u){vis[u] = true; int to;Redge(u) if (!vis[to = edge[k].to]){d[to] = d[u] ^ edge[k].w;dfs(to);}else V[++cir] = d[to] ^ d[u] ^ edge[k].w;}void gaosi(){for (LL j = bin[60]; j; j >>= 1){int i = tot + 1;while (i <= cir && !(V[i] & j)) i++;if (i == cir + 1) continue;swap(V[++tot],V[i]);for (int k = 1; k <= cir; k++)if (k != tot && (V[k] & j))V[k] ^= V[tot];}}int main(){bin[0] = 1;REP(i,60) bin[i] = bin[i - 1] << 1;memset(head,-1,sizeof(head));N = RD(); M = RD(); LL a,b,w;while (M--){a = RD(); b = RD(); w = RD();build(a,b,w);}dfs(1); gaosi();LL ans = d[N];for (int i = 1; i <= cir; i++)ans = max(ans,ans ^ V[i]);cout<<ans<<endl;return 0;}