异或值 xor
题目描述
给出一个 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
#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的更多相关文章
- [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 ...
- 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 ...
- 51Nod XOR key —— 区间最大异或值 可持久化字典树
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1295 1295 XOR key 题目来源: HackerRa ...
- [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 ...
- 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 ...
- 异或链表(XOR linked list)
异或链表(Xor Linked List)也是一种链式存储结构,它可以降低空间复杂度达到和双向链表一样目的,任何一个节点可以方便的访问它的前驱节点和后继结点.可以参阅wiki 普通的双向链表 clas ...
- 【机器学习】神经网络实现异或(XOR)
注:在吴恩达老师讲的[机器学习]课程中,最开始介绍神经网络的应用时就介绍了含有一个隐藏层的神经网络可以解决异或问题,而这是单层神经网络(也叫感知机)做不到了,当时就觉得非常神奇,之后就一直打算自己实现 ...
- Leetcode 421.数组中两数的最大异或值
数组中两数的最大异或值 给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ...
- 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, ...
随机推荐
- avalon ms-repeat avalon1
工作原因要用到avalon二次开发, 但是看了下以前的avalon版本是1,现在大多数都是2版本了吧,,所以很多文档不好找,但是大多数还是好用的 ms-repeat 循环当前赋值的, ms-repea ...
- zip压缩工具,unzip解压缩工具
zip压缩工具,unzip解压缩工具=================== [root@aminglinux tmp]# yum install -y zip[root@aminglinux tmp] ...
- 【yii】【php】自定义故障代码
实际状态码: 200 操作成功 406 账号密码错误 208 请勿重复操作 401 需登陆验证 405 不容许此方法 409 验证错误
- Linux基本的指令操作
绝对路径: 路径的写法,由根目录/写起,例如:/usr/share/doc这个目录. 相对路径: 路径的写法,不由/写起,例如由/usr/share/doc要到/usr/share/man底下时,可以 ...
- RabbitMQ实现中AMQP与MQTT消息收发异同
实现了AMQP与MQTT(至多一次)后,用多个队列以topic exchange的方式用相同交换机监听同一个主题(topic),发现情况存在不同,觉得有点意思,所以记录了下来. 用2个MQTT(分别记 ...
- 20145202马超 《Java程序设计》第五周学习总结
异常:程序在运行的时候出现不正正常的情况 由来:问题也是可以通过java对不正常情况进行描述后的对象的体现. 问题的划分:(1).严重的问题,java通过error类进行描述,对于error一般不编写 ...
- 3 Mongodb数据查询1
1.基本查询 方法find():查询 db.集合名称.find({条件文档}) 方法findOne():查询,只返回第一个 db.集合名称.findOne({条件文档}) 方法pretty():将结果 ...
- CentOS 单用户模式:修改Root密码和grub加密[转]
原文出处: http://zhengdl126.iteye.com/blog/430268 Linux 系统处于正常状态时,服务器主机开机(或重新启动)后,能够由系统引导器程序自动引导 Linux 系 ...
- 剑指Offer - 九度1391 - 顺时针打印矩阵
剑指Offer - 九度1391 - 顺时针打印矩阵2013-11-24 04:55 题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目7
2014-03-21 22:05 题目:给你N个盒子堆成一座塔,要求下面盒子的长和宽都要严格大于上面的.问最多能堆多少个盒子? 解法1:O(n^2)的动态规划解决.其实是最长递增子序列问题,所以也可以 ...