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
Source
要求的其实是起点到终点的路径和一堆环的异或最大值
其实是【求选出一些数与给定数异或的最大值】。找出环来然后丢矩阵里高斯消元一下就行了
一定要处处开longlong!!
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int SZ = 1000010;
const int INF = 1000000010;
int head[SZ],nxt[SZ],tot = 1;
struct edge{
int t;
LL d;
}l[SZ];
void build(int f,int t,LL d)
{
l[++ tot].t = t;
l[tot].d = d;
nxt[tot] = head[f];
head[f] = tot;
}
int n,m,r = 0;
LL dist[SZ],g[SZ];
bool vis[SZ];
void dfs(int u)
{
vis[u] = 1;
for(int i = head[u];i;i = nxt[i])
{
int v = l[i].t;
if(!vis[v])
{
dist[v] = dist[u] ^ l[i].d;
dfs(v);
}
else
g[++ r] = dist[u] ^ dist[v] ^ l[i].d;
}
}
LL gauss()
{
int now = 0;
for(int i = 63;i >= 0;i --)
{
int j = now + 1;
while(j <= r && !(g[j] >> i & 1)) j ++;
if(j > r) continue;
now ++;
swap(g[now],g[j]);
for(int j = 1;j <= r;j ++)
if((g[j] >> i & 1) && j != now)
g[j] ^= g[now];
}
LL ans = dist[n];
for(int i = 1;i <= now;i ++)
ans = max(ans,ans ^ g[i]);
return ans;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = 1;i <= m;i ++)
{
int a,b;
LL c;
scanf("%d%d%lld",&a,&b,&c);
build(a,b,c); build(b,a,c);
}
dfs(1);
printf("%lld",gauss());
return 0;
}