题目描述

给出一个 N 个点的带权无向图,要求从 1 号点到 N 号点的一条路径,使得路径上的边 权异或值最大.

输入格式

第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目。

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

输出格式

仅包含一个整数,表示最大的XOR和(十进制结果) 。

样例

样例输入

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

样例输出

6

数据范围与提示

【题目来源】

WC2011


solution

如果没有环,那么方案是确定的。
对于每一个环我们都可以选择走或不走。
那么这就是一个线性基的问题了。
 
稍稍回忆一下线性基。
问题:给一个集合,求异或和最大的一个子集。
我们考虑一个60位的数组。
现在一个个往里加数。
枚举这个数从高位到低位的所有1,如果线性基这一位为0,那么就加入,然后break;
否则异或上这位上的数继续取。
这样子原集合中能异或出的所有数新集合中也能异或出来。
然后从高位到低位贪心即可。
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 100005
#define ll long long
using namespace std;
int n,m,head[maxn],tot=,top;
ll t3,c[maxn*],d[maxn],b[];
bool vis[maxn];
struct node{
int v,nex;
ll w;
}e[maxn*];
void lj(int t1,int t2,ll t3){
e[++tot].v=t2;e[tot].w=t3;e[tot].nex=head[t1];head[t1]=tot;
}
void dfs(int k,ll v){
vis[k]=;d[k]=v; for(int i=head[k];i;i=e[i].nex){
if(!vis[e[i].v]){
dfs(e[i].v,v^e[i].w);
}
else c[++top]=v^d[e[i].v]^e[i].w;
}
}
int main()
{
cin>>n>>m;
for(int i=,t1,t2;i<=m;i++){
scanf("%d%d%lld",&t1,&t2,&t3);
lj(t1,t2,t3);lj(t2,t1,t3);
}
dfs(,); for(int i=;i<=top;i++){
for(int j=;j>=;j--){
if(c[i]&(1LL<<j)){
if(!b[j]){b[j]=c[i];break;}
else c[i]^=b[j];
}
}
}
ll ans=d[n];
for(int i=;i>=;i--){
ans=max(ans,ans^b[i]);
}
cout<<ans<<endl;
return ;
}

异或值 xor的更多相关文章

  1. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  2. Codeforces Round #482 (Div. 2) : Kuro and GCD and XOR and SUM (寻找最大异或值)

    题目链接:http://codeforces.com/contest/979/problem/D 参考大神博客:https://www.cnblogs.com/kickit/p/9046953.htm ...

  3. 51Nod XOR key —— 区间最大异或值 可持久化字典树

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1295 1295 XOR key  题目来源: HackerRa ...

  4. [LeetCode] 421. Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  5. LeetCode 421. 数组中两个数的最大异或值(Maximum XOR of Two Numbers in an Array) 71

    421. 数组中两个数的最大异或值 421. Maximum XOR of Two Numbers in an Array 题目描述 给定一个非空数组,数组中元素为 a0, a1, a2, - , a ...

  6. 异或链表(XOR linked list)

    异或链表(Xor Linked List)也是一种链式存储结构,它可以降低空间复杂度达到和双向链表一样目的,任何一个节点可以方便的访问它的前驱节点和后继结点.可以参阅wiki 普通的双向链表 clas ...

  7. 【机器学习】神经网络实现异或(XOR)

    注:在吴恩达老师讲的[机器学习]课程中,最开始介绍神经网络的应用时就介绍了含有一个隐藏层的神经网络可以解决异或问题,而这是单层神经网络(也叫感知机)做不到了,当时就觉得非常神奇,之后就一直打算自己实现 ...

  8. Leetcode 421.数组中两数的最大异或值

    数组中两数的最大异或值 给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ...

  9. Educational Codeforces Round 12 E. Beautiful Subarrays trie求两异或值大于等于k对数

    E. Beautiful Subarrays   One day, ZS the Coder wrote down an array of integers a with elements a1,   ...

随机推荐

  1. MAC中向阿里云服务器上传文件

    打开mac中的终端 使用命令:$scp /local/file user@remote:/file /local/file 是本地文件 后面部分[用户名]@[ip地址:][服务器中的文件目录] not ...

  2. FullCalendar日历插件(中文API)

    FullCalendar提供了丰富的属性设置和方法调用,开发者可以根据FullCalendar提供的API快速完成一个日历日程的开发,本文将FullCalendar的常用属性和方法.回调函数等整理成中 ...

  3. redis数据库的安装配置

    redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了memcached的不足,它支持存储的value类型相对更多,包括strin ...

  4. laravel EncryptCookies中间件导致无法获取自定义cookie

    解决办法: \app\Http\Middleware\EncryptCookies.php 添加过滤cookie key protected $except = [ 'token' ];

  5. Python学习笔记:Matplotlib(数据可视化)

    Matplotlib是一个可以将数据绘制为图形表示的Python三方库,包括线性图(折线图,函数图).柱形图.饼图等基础而直观的图形,在平常的开发当中需要绘图时就非常有用了. 安装:pip insta ...

  6. django-simple-captcha

    在注册页面生成验证码的时候,出现错误如下: build_attrs() takes from 1 to 2 positional arguments but 3 were given, 不知道为什么报 ...

  7. 搭建一个简单的dns缓存服务器

    环境:linux 软件:bind97,bind97-utils, bind97-libs ip:192.168.192.130:192.168.192.131 -------------------- ...

  8. python-7面向对象高级编程

    1-给类动态增加方法 class Student(object): pass def set_score(self, score): self.score = score Student.set_sc ...

  9. CSS3---混合模式

    概念 CSS3混合模式(  CSS Blend Modes  )是CSS3新增的一个魔法特性,可以允许多个背景或多个元素进行混合,类似于Photoshop的图层混合模式 CSS3混合模式属性一览 ba ...

  10. android gridview 停止滚动

    http://blog.csdn.net/yaphetzhao/article/details/50544105 参考上面的博客,关键代码我就贴出来吧: public void stopGridVie ...