zoukankan      html  css  js  c++  java
  • [WC2011]最大XOR和路径

    传送门

    做了前几天的模拟看这个现在很有感觉……

    首先我们能想出,走任何一条路径,都是可以把所有环的贡献全部异或到的(走过去的路和回来的路是一条路,贡献为0)。 因为要求最大异或和容易想到线性基。于是我们先搜索出所有环,并且把它们的异或值加入线性基中。

    之后我们就只要找一条路,用它到终点的异或和和线性基里面的东西异或异或就行了。不过这个结论我没有想到,就是随便选取一条路即可。因为其实所有的1~n的路径 都能互相成环。而环已经被我们加入线性基,所以在异或之后必然是最大值。

    看一下代码。

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #define rep(i,a,n) for(register int i = a;i <= n;i++)
    #define per(i,n,a) for(register int i = n;i >= a;i--)
    #define enter putchar('
    ')
    #define pr pair<int,int>
    #define mp make_pair
    #define fi first
    #define sc second
    using namespace std;
    typedef long long ll;
    const int M = 100005;
    const int N = 10000005;
     
    ll read()
    {
       ll ans = 0,op = 1;char ch = getchar();
       while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
       while(ch >='0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
       return ans * op;
    }
    
    struct edge
    {
       ll next,to,from,v; 
    }e[M<<2];
    
    ll n,m,head[M],ecnt,ans,p[M],dis[M],x,y,z;
    bool vis[M];
    
    void add(ll x,ll y,ll z)
    {
       e[++ecnt] = (edge){head[x],y,x,z};
       head[x] = ecnt;
    }
    
    void insert(ll x)
    {
       per(i,63,0)
       {
          if(!(x >> i)) continue;
          if(!p[i]) {p[i] = x;break;}
          x ^= p[i];
       }
    }
    
    void dfs(int x,ll cur)
    {
       dis[x] = cur,vis[x] = 1;
       for(int i = head[x];i;i = e[i].next)
       {
          if(!vis[e[i].to]) dfs(e[i].to,cur ^ e[i].v);
          else insert(cur ^ e[i].v ^ dis[e[i].to]);
       }
    }
    
    ll query(ll x)
    {
       per(i,63,0) if((x ^ p[i]) > x) x ^= p[i];
       return x;
    }
    
    int main()
    {
       n = read(),m = read();
       rep(i,1,m) x = read(),y = read(),z = read(),add(x,y,z),add(y,x,z);
       dfs(1,0);
       printf("%lld
    ",query(dis[n]));
       return 0;
    }
    
    
  • 相关阅读:
    2018——测试与信仰
    面试必备----测试用例笔试题分享
    软件测试人员必备网络知识(一):什么是cookie?
    Postman和Selenium IDE开局自带红蓝BUFF属性,就问你要还是不要
    【Loadrunner】LR参数化:利用mysql数据库里面的数据进行参数化
    因果图法设计测试用例
    场景法设计测试用例
    Linux Centos7下安装Python
    Vmware安装与VMware下Linux系统安装
    Python运算符与表达式
  • 原文地址:https://www.cnblogs.com/captain1/p/10204763.html
Copyright © 2011-2022 走看看