【题目分析】

显然,一个路径走过两边是不需要计算的,所以我么找到一条1-n的路径,然后向该异或值不断异或简单环即可。

但是找出所有简单环是相当复杂的,我们只需要dfs一遍,找出所有的环路即可,因为所有的简单环都可以经过各种各样的异或得到。

然后线性基,在从高位向低位贪心即可,至于证明,需要拟阵的相关知识。

【代码】

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;

#define maxn 100005
#define ll long long

int read()
{
    int x=0,f=1; char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

int h[maxn],to[maxn<<1],ne[maxn<<1];
ll w[maxn<<1];
int en=0,n,m,vis[maxn],tot=0;
ll a[maxn<<1];
ll dis[maxn];

void add(int a,int b,ll c)
{
    to[en]=b;
    w[en]=c;
    ne[en]=h[a];
    h[a]=en++;
}

void dfs(int k)
{
//  printf("dfs on %d\n",k);
    vis[k]=1;
    for (int i=h[k];i>=0;i=ne[i])
    {
        if (!vis[to[i]])
        {
            dis[to[i]]=dis[k]^w[i];
            dfs(to[i]);
        }
        else a[++tot]=dis[k]^dis[to[i]]^w[i];
    }
}

ll lb[64],ans;

int main()
{
    memset(h,-1,sizeof h);
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;++i)
    {
        int a,b; ll c;
        scanf("%d%d%lld",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
    }
    dfs(1);
    ans=dis[n];
//  for (int i=1;i<=n;++i) cout<<dis[i]<<" "; cout<<endl;
//  for (int i=1;i<=tot;++i) cout<<a[i]<<" ";cout<<endl;
    for (int i=1;i<=tot;++i)
    {
        for (int j=63;j>=0;j--)
        {
            if ((a[i]>>j)&1){
                if (!lb[j]) {lb[j]=a[i];break;}
                else a[i]^=lb[j];
            }
        }
    }
    for (int i=63;i>=0;i--)
        if (lb[i]&&((ans>>i)&1)==0) ans^=lb[i];
    cout<<ans<<endl;
    return 0;
}

  

BZOJ 2115 [Wc2011] Xor ——线性基的更多相关文章

  1. BZOJ.2115.[WC2011]Xor(线性基)

    题目链接 \(Description\) 给定一张无向带边权图(存在自环和重边).求一条1->n的路径,使得路径经过边的权值的Xor和最大.可重复经过点/边,且边权和计算多次. \(Soluti ...

  2. BZOJ 2115: [Wc2011] Xor 线性基 dfs

    https://www.lydsy.com/JudgeOnline/problem.php?id=2115 每一条从1到n的道路都可以表示为一条从1到n的道路异或若干个环的异或值. 那么把全部的环丢到 ...

  3. BZOJ 2115: [Wc2011] Xor DFS + 线性基

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MB Description Input 第一行包含两个整数N和 M, 表示该无向图中 ...

  4. BZOJ 2115: [Wc2011] Xor

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 2794  Solved: 1184 [Submit][Stat ...

  5. bzoj 2115: [Wc2011] Xor xor高斯消元

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 797  Solved: 375[Submit][Status] ...

  6. bzoj 2115: [Wc2011] Xor【线性基+dfs】

    -老是想到最长路上 其实可以这样:把每个环的xor和都存起来,然后任选一条1到n的路径的xor和ans,答案就是这个ans在环的线性基上跑贪心. 为什么是对的--因为可以重边而且是无相连通的,并且对于 ...

  7. bzoj 2115 [Wc2011] Xor 路径最大异或和 线性基

    题目链接 题意 给定一个 \(n(n\le 50000)\) 个点 \(m(m\le 100000)\) 条边的无向图,每条边上有一个权值.请你求一条从 \(1\)到\(n\)的路径,使得路径上的边的 ...

  8. BZOJ2115:[WC2011] Xor(线性基)

    Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...

  9. BZOJ - 2115 独立回路 线性基

    题意:给定一个图集\((V,E)\),求路径\(1...n\)的最大异或和,其中重复经过的部分也会重复异或 所求既任意一条\(1...n\)的路径的异或和,再异或上任意独立回路的组合的异或和(仔细想想 ...

随机推荐

  1. js 中关于this用变量存起来的原因

    近来,不忙,总是在网上看大牛的博客,发现关于this指向的情况,有的说是4种,也对,有的就是总结一句话,也对,但是我发现一种特殊情况,例子如下 var show={ btn:$('.div1'), i ...

  2. 我的SqlHelper类!

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  3. jQuery验证控件jquery.validate.js使用说明

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...

  4. tyvj1097 mm不哭

    背景 Bless all rp++.. 描述 在一个数轴上,有n个MM(绝非恐龙!)在哭泣(5555~一直哭). tcboy也在这个数轴上,并恰好看到了这一幕,由于每个MM哭都会让tcboy损失一定的 ...

  5. thinkphp3.2跨控制器调用其他模块的方法

    thinphp中前台后台都有互相调用方法,这样可以省去重复内容. 1 2 $hello = new \Admin\Common\Fun\hello(); $hello->hehe(); 调用其他 ...

  6. H5播放器

    <!DOCTYPE html>  <html>  <body>  <div style="text-align:center">  ...

  7. synchronized在jvm底层是如何实现的

    目前在Java中存在两种锁机制:synchronized和Lock,Lock接口及其实现类是JDK5增加的内容,其作者是大名鼎鼎的并发专家Doug Lea.本文并不比较synchronized与Loc ...

  8. cookie中文乱码

    在学习当中碰到cookie中文乱码问题,问题原因:cookie对中文不太支持,将中文放入cookie中会报错误. 解决办法: 1.编码 将中文进行编码再放入cookie中: String userna ...

  9. Git 常用命令合集

    $ git init          建立git仓库(一般都是在github上新建好,直接克隆到本地) $ git clone **.git       克隆git仓库 $ git add -A   ...

  10. ecshop中foreach的详细用法归纳

    ec模版中foreach的常见用法. foreach 语法: 假如后台:$smarty->assign('test',$test); {foreach from=$test item=list ...