time limit per test 3 seconds

memory limit per test 512 megabytes

input standard input

output standard output

You are given an undirected graph with weighted edges. The length of some path between two vertices is the bitwise xor of weights of all edges belonging to this path (if some edge is traversed more than once, then it is included in bitwise xor the same number of times). You have to find the minimum length of path between vertex 1 and vertex n.

Note that graph can contain multiple edges and loops. It is guaranteed that the graph is connected.

Input

The first line contains two numbers n and m (1 ≤ n ≤ 100000, n - 1 ≤ m ≤ 100000) — the number of vertices and the number of edges, respectively.

Then m lines follow, each line containing three integer numbers x, y and w (1 ≤ x, y ≤ n, 0 ≤ w ≤ 108). These numbers denote an edge that connects vertices x and y and has weight w.

Output

Print one number — the minimum length of path between vertices 1 and n.

Examples

Input

3 3
1 2 3
1 3 2
3 2 0

Output

2

Input

2 2
1 1 3
1 2 3

Output

0
 

【翻译】求1 to n的异或最短路。

 
题解:
①线基入门题。啊,终于入门了。
      ②最短路转树维护环这个思路很好~
#include<stdio.h>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define ro(i,a,b) for(int i=a;i>=b;i--)
#define fo(i,a,x) for(int i=a[x],v=e[i].v;i;i=e[i].next,v=e[i].v)
const int N=100003;
struct E{int v,next,w;}e[N<<1];
int n,m,U,V,W,head[N],k=1,d[N];bool vis[N];
void ADD(int u,int v,int w){e[k]=(E){v,head[u],w};head[u]=k++;} struct Linear_Base
{
int a[40];
void Insert(int x){if(x)ro(i,30,0)if((1<<i)&x)!a[i]?a[i]=x:i=0,x^=a[i];}
int Get_Max(int x){ro(i,30,0)if(x>(x^a[i]))x^=a[i];return x;}
}Base; void dfs(int u)
{
fo(i,head,u)!vis[v]?d[v]=d[u]^e[i].w,vis[v]=1,
dfs(v):Base.Insert(d[u]^d[v]^e[i].w);
} int main()
{
freopen("in.in","r",stdin);
scanf("%d%d",&n,&m);d[1]=0;
go(i,1,m)scanf("%d%d%d",&U,&V,&W),ADD(U,V,W),ADD(V,U,W);
dfs(1);printf("%d\n",Base.Get_Max(d[n]));return 0;
}//Paul_Guderian
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
这就你,完美的生命,悲哀而真实。——————汪峰《哭泣的拳头》

【CF edu 27 G. Shortest Path Problem?】的更多相关文章

  1. Codefroces Educational Round 27 845G Shortest Path Problem?

    Shortest Path Problem? You are given an undirected graph with weighted edges. The length of some pat ...

  2. 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码

    00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...

  3. 线性基【CF845G】Shortest Path Problem?

    Description 给定一张 \(n\) 个点 \(m\) 条边的无向图,一开始你在点 \(1\),且价值为 \(0\) 每次你可以选择一个相邻的点,然后走过去,并将价值异或上该边权 如果在点 \ ...

  4. Codeforces 845G Shortest Path Problem?

    http://codeforces.com/problemset/problem/845/G 从顶点1dfs全图,遇到环则增加一种备选方案,环上的环不需要走到前一个环上作为条件,因为走完第二个环可以从 ...

  5. 【cf contest 1119 G】Get Ready for the Battle

    题目 你有\(n\)个士兵,需要将他们分成\(m\)组,每组可以为0: 现在这些士兵要去攻打\(m\)个敌人,每个敌人的生命值为\(hp_i\) : 一轮游戏中一组士兵选定一个攻打的敌人,敌人生命值- ...

  6. 【Cf Edu #47 G】Allowed Letters

    这个题大概就是每一个位置都有一个能填字符的限制(一个点集),给出已有的$n$个字符,问能填出的最小字典序的字符串. 总体思路是贪心,每一位尽量选最小的字符. 关键在于判断在某位选了一个字符后,接下来的 ...

  7. [CF845G]Shortest Path Problem?

    题目大意:同这道题,只是把最大值变成了最小值 题解:略 卡点:无 C++ Code: #include <cstdio> #define maxn 100010 #define maxm ...

  8. cf 843 D Dynamic Shortest Path [最短路+bfs]

    题面: 传送门 思路: 真·动态最短路 但是因为每次只加1 所以可以每一次修改操作的时候使用距离分层的bfs,在O(n)的时间内解决修改 这里要用到一个小技巧: 把每条边(u,v)的边权表示为dis[ ...

  9. 题解 CF938G 【Shortest Path Queries】

    题目让我们维护一个连通无向图,边有边权,支持加边删边和询问从\(x\)到\(y\)的异或最短路. 考虑到有删边这样的撤销操作,那么用线段树分治来实现,用线段树来维护询问的时间轴. 将每一条边的出现时间 ...

随机推荐

  1. 【赛时总结】 ◇赛时·II◇ AtCoder ABC-100

    ◆赛时·II◆ ABC-100 ■唠叨■ ABC终于超过百场比赛啦(毫不犹豫地参加).然后莫名其妙的好像是人很多,评测慢得不可理喻.然后我就--交了一大发--错误程序--然后B题就没了.最后的D题居然 ...

  2. Hibernate基础学习2

    Hibernate基础学习2 测试hibernate的一级缓存,事务以及查询语句 1)Hibernate的一些相关概念 hibernate的一级缓存 1)缓存是为了提高该框架对数据库的查询速度 2)一 ...

  3. spring-IOC底层机制

    JDK与CGLIB的动态代理 JDK动态代理 创建代理的方法 将需要代理的类传入代理类中(通过构造方法) 在代理方法中创建代理实例(需要一个接口和一个实现接口的类): Proxy.newProxyIn ...

  4. Spring MVC 接收前端参数的方式

    方式一: 普通方式接收 1 @RequestMapping("/index") 2 public String getUserName(String username) { 3 S ...

  5. Ajax上传文件/照片时报错TypeError :Illegal invocation

    问题 Ajax上传文件/照片时报错TypeError :Illegal invocation 解决 网上搜索问题,错误原因可能有以下几个,依次检查: 请求类型有误,如post请求,但在后台设置的是ge ...

  6. ethereum(以太坊)(二)--合约中属性和行为的访问权限

    pragma solidity ^0.4.0; contract Test{ /* 属性的访问权限 priveta public internal defualt internal interlnal ...

  7. python 函数 闭包 (节省内存空间 html 获取网页的源码)

    #闭包:嵌套函数,内部函数调用外部函数的变量 # def outer(): # a = 1 # def inner(): # print(a) # inner() # outer() def oute ...

  8. C语言指针篇(二)多级指针

        多级指针         多级指针常常使用于数组.这里仅仅介绍一下它长什么样,后文会再次详细对比使用.         多级指针呢,常见的有二级指针.见图.             二级指针的 ...

  9. Dialog BLE 学习之 修改分散加载文件 (2)

    最近搞Dialog的BLE SDK,发现空间不够了,询问原厂,得知可以通过调整分散加载文件而增加空间,一方面是有42KB+8KB的硬件基础,另一方面是原有的程序限制为38KB+8KB.故顺便学习了下把 ...

  10. 怎么将oracle的sql文件转换成mysql的sql文件-- 费元星

    http://jingyan.baidu.com/article/ca41422fe01f251eaf99ed6e.html