[WC2011]最大XOR和路径

传送门

做了前几天的模拟看这个现在很有感觉……

首先我们能想出,走任何一条路径,都是可以把所有环的贡献全部异或到的(走过去的路和回来的路是一条路,贡献为0)。 因为要求最大异或和容易想到线性基。于是我们先搜索出所有环,并且把它们的异或值加入线性基中。

之后我们就只要找一条路,用它到终点的异或和和线性基里面的东西异或异或就行了。不过这个结论我没有想到,就是随便选取一条路即可。因为其实所有的1~n的路径 都能互相成环。而环已经被我们加入线性基,所以在异或之后必然是最大值。

看一下代码。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define rep(i,a,n) for(register int i = a;i <= n;i++)
#define per(i,n,a) for(register int i = n;i >= a;i--)
#define enter putchar('\n')
#define pr pair<int,int>
#define mp make_pair
#define fi first
#define sc second
using namespace std;
typedef long long ll;
const int M = 100005;
const int N = 10000005;
 
ll read()
{
   ll ans = 0,op = 1;char ch = getchar();
   while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
   while(ch >='0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
   return ans * op;
}

struct edge
{
   ll next,to,from,v; 
}e[M<<2];

ll n,m,head[M],ecnt,ans,p[M],dis[M],x,y,z;
bool vis[M];

void add(ll x,ll y,ll z)
{
   e[++ecnt] = (edge){head[x],y,x,z};
   head[x] = ecnt;
}

void insert(ll x)
{
   per(i,63,0)
   {
      if(!(x >> i)) continue;
      if(!p[i]) {p[i] = x;break;}
      x ^= p[i];
   }
}

void dfs(int x,ll cur)
{
   dis[x] = cur,vis[x] = 1;
   for(int i = head[x];i;i = e[i].next)
   {
      if(!vis[e[i].to]) dfs(e[i].to,cur ^ e[i].v);
      else insert(cur ^ e[i].v ^ dis[e[i].to]);
   }
}

ll query(ll x)
{
   per(i,63,0) if((x ^ p[i]) > x) x ^= p[i];
   return x;
}

int main()
{
   n = read(),m = read();
   rep(i,1,m) x = read(),y = read(),z = read(),add(x,y,z),add(y,x,z);
   dfs(1,0);
   printf("%lld\n",query(dis[n]));
   return 0;
}

转载于:https://www.cnblogs.com/captain1/p/10204763.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值