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 xy 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到地点N经过路的异或和最短路的题;对于与一个环我们都可以使其异或和为零(一个环经过两次即异或和为0),一个环要么走完一遍,要么不走;我们可以任意找一条从1到N的路,然后异或每一个环,找最小值即可;

AC代码为:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
const int INF=0x3f3f3f3f;
typedef long long LL;
int n,m,u,v,w,tot,temp,first[maxn],vis[maxn],a[maxn],b[maxn],dis[maxn];
struct Edge{
    int to,w,net;
} edge[maxn<<1];

inline void Init()
{
    memset(first,-1,sizeof first);
    memset(vis,0,sizeof vis);
    memset(b,0,sizeof b);
    tot=1;temp=0;
}

inline void addedge(int u,int v,int w)
{
    edge[tot].to=v;
    edge[tot].w =w;
    edge[tot].net=first[u];
    first[u]=tot++;
}

inline void dfs(int id,int len)
{
    vis[id]=1; dis[id]=len;
    for(int i=first[id];~i;i=edge[i].net)
    {
        if(vis[edge[i].to]) a[++temp]=dis[edge[i].to]^edge[i].w^len;
        else dfs(edge[i].to,len^edge[i].w);
    }
}

inline void Guass()
{
    for(int i=1;i<=temp;i++)
    {
        for(int j=31;j>=0;j--)
        {
            if((a[i] >> j) & 1)
            {
                if(!b[j])
                {
                    b[j]=a[i];
                    break;
                }
                else a[i]^=b[j];
            }
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    Init();
    for(int i=1;i<=m;i++)
    {
        cin>>u>>v>>w;
        addedge(u,v,w);
        addedge(v,u,w);
    }
    dfs(1,0);
    int ans=dis[n];
    for(int i=31;i>=0;i--) ans=min(ans,ans^b[i]);
    cout<<ans<<endl;
    return 0;
}

CodeForces845G-Shortest PathProblem?的更多相关文章

  1. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  2. [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  3. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  4. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  5. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  6. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Leetcode: Encode String with Shortest Length && G面经

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  8. LeetCode 214 Shortest Palindrome

    214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...

  9. POJ2001 Shortest Prefixes

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

  10. Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

随机推荐

  1. 【Swift】UNNotificationServiceExtension

    一.简介 An object that modifies the content of a remote notification before it's delivered to the user. ...

  2. Project Euler 60: Prime pair sets

    素数3, 7, 109, 673很有意思,从中任取两个素数以任意顺序拼接起来形成的仍然是素数.例如,取出7和109,7109和1097都是素数.这四个素数的和是792,是具有这样性质的四个素数的最小的 ...

  3. windows系统cmd命令行窗口查看端口占用情况

    # 查看所有在用端口 netstat -ano # 查看指定端口 netstat -ano | findstr 8899 # 结束该进程:taskkill /f /t /im javaw.exe:或者 ...

  4. 心里有点B树

    在说B树之前最好先看看2-3树, 2-3树是B树的一种特例, 什么B树, B树就是2-3树, 2-3-4 树 , 2-3-4-5... 树的统称, 而B+树又是B树的一种变形 性质: 什么是二节点, ...

  5. pug参考文档

    1. API express框架的渲染模板有多种选择,官方的例子是Pug,网上找了一下,Pug没有可以参考的中文文档,于是自己动手丰衣足食.翻译水平一般,各位凑合着看吧. 1.1 开始 安装 通过np ...

  6. vue 学习 渲染、v-指令

    vue渲染 在组件中data是一个方法里面的值要是一个对象return出去 export default { name: "HelloWorld", data() { return ...

  7. 2019-11-5:docker拉去配置oracle数据库

    转载自:https://www.cnblogs.com/OliverQin/p/9765808.html 1.拉取Oracle11g镜像 docker pull registry.cn-hangzho ...

  8. java学生管理系统

    student类 package cn.itheima.Manag; /** * *标准类 * **/public class Student { //学号 private String id; // ...

  9. 【Luogu P1967】货车运输

    Luogu P1967 题目大意:给定一张图和q个询问,询问x节点和y节点的路径之间最小边权最大可以是多少. 可以发现对于一条边\(E(x,y)\),如果x到y有另一条路径且最小边权大于\(E(x,y ...

  10. 插槽在父组件和子组件间的使用(vue3.0推荐)

    子组件: 父组件: 插槽在父组件和子组件间的使用(vue3.0推荐):在外面加一个template模板