【CF edu 27 G. Shortest Path Problem?】

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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
这就你,完美的生命,悲哀而真实。——————汪峰《哭泣的拳头》
posted @ 2017-10-19 11:27  小米狐  阅读(213)  评论(0编辑  收藏  举报
TOP