Time:2016.08.26
Author:xiaoyimi
转载注明出处谢谢
传送门
思路:
权值就是一条单路径(1->n)+所有环
dfs把它们求出来就可以了
复杂度
O(n+m)
求出线性基,里面存储的就是各个环的xor值
后对每一位贪心就可以了
复杂度
O(64log(n+m))
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#define LL long long
using namespace std;
int n,m,cnt,tot;
int first[50005];
bool vis[50005];
LL data[1000005],lb[65],dis[50005],ans;
struct edge{
int u,v,next;
LL w;
}e[200005];
LL in()
{
char ch=getchar();LL t=0;
while (ch<'0'||ch>'9') ch=getchar();
while (ch>='0'&&ch<='9') t=(t<<3)+(t<<1)+ch-48,ch=getchar();
return t;
}
void add(LL z,int y,int x)
{
e[++tot]=(edge){x,y,first[x],z};first[x]=tot;
e[++tot]=(edge){y,x,first[y],z};first[y]=tot;
}
void dfs(int x)
{
vis[x]=1;
for (int i=first[x];i;i=e[i].next)
if (!vis[e[i].v])
dis[e[i].v]=dis[x]^e[i].w,
dfs(e[i].v);
else
data[++cnt]=dis[x]^dis[e[i].v]^e[i].w,
cnt-=(!data[cnt]);
}
main()
{
n=in();m=in();
for (int i=1;i<=m;++i)
add(in(),in(),in());
dfs(1);
for (int i=1;i<=cnt;++i)
for (int j=63;j>=0;--j)
if (data[i]>>j&1)
if (!lb[j])
{lb[j]=data[i];break;}
else
data[i]^=lb[j];
ans=dis[n];
for (int i=63;i>=0;--i)
if (!(ans>>i&1)) ans^=lb[i];
printf("%lld\n",ans);
}