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

    [WC2011]最大XOR和路径

    和刚才那个问题的区别就是搬到了无向连通图上.

    但做法却大相径庭.

    最特殊的地方就是与上一个问题相比多了环.

    于是我们着重考虑环,我们发现,一个环走两遍是没有意义的,于是每个环只会被走一遍.

    而如果一条合法路径外接一个环,那么这条路径就可能可以通过这个环增广.

    所以我们可以预处理出所有环的异或和,然后每次考虑增广.

    那么怎么去找一条合适的路径增广呢?

    实际上任意一条路径都可以.

    因为如果存在别的合法路径,那么它会和当前路径成环,也就是会被更新.

    所以这题就愉快地做完啦!

    (Code:)

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <queue>
    #include <cmath>
    #include <ctime>
    #include <map>
    #include <set>
    #define MEM(x,y) memset ( x , y , sizeof ( x ) )
    #define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
    #define per(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
    #define pii pair < int , int >
    #define one first
    #define two second
    #define rint read<int>
    #define int long long
    #define pb push_back
    #define db double
    #define ull unsigned long long
    #define lowbit(x) ( x & ( - x ) )
    
    using std::queue ;
    using std::set ;
    using std::pair ;
    using std::max ;
    using std::min ;
    using std::priority_queue ;
    using std::vector ;
    using std::swap ;
    using std::sort ;
    using std::unique ;
    using std::greater ;
    
    template < class T >
        inline T read () {
            T 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 << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
                ch = getchar () ;
           }
           return f * x ;
        }
    
    const int N = 5e4 + 100 ;
    const int M = 1e5 + 100 ;
    
    struct edge { int to , next , data ; } e[M<<1] ;
    
    int n , m , tot , head[N] , dis[N] ;
    bool vis[N] ;
    
    class LinearBasis {
        private : int base[65] ;
        public :
            inline void clear () { MEM ( base , 0 ) ; return ; }
        public :
            inline bool insert (int x) {
                if ( ! x ) return false ;
                for (int i = 62 ; ~ i ; -- i)
                    if ( x & ( 1ll << i ) ) {
                        if ( base[i] ) x ^= base[i] ;
                        else { base[i] = x ; return true ; }
                    }
                return false ;
            }
        public :
            inline int query (int pri) {
                int res = pri ;
                for (int i = 62 ; ~ i ; -- i)
                    res = max ( res , res ^ base[i] ) ;
                return res ;
            }
    } B ;
    
    inline void build (int u , int v , int w) {
        e[++tot].next = head[u] ; e[tot].to = v ;
        e[tot].data = w ; head[u] = tot ; return;
    }
    
    inline void dfs (int cur , int len) {
        dis[cur] = len ; vis[cur] = true ;
        for (int i = head[cur] ; i ; i = e[i].next) {
            int k = e[i].to ;
            if ( ! vis[k] ) dfs ( k , len ^ e[i].data ) ;
            else B.insert ( len ^ e[i].data ^ dis[k] ) ;
        }
        return ;
    }
    
    signed main (int argc , char * argv[]) {
        n = rint () ; m = rint () ;
        rep ( i , 1 , m ) {
            int u = rint () , v = rint () , w = rint () ;
            build ( u , v , w ) ; build ( v , u , w ) ;
        }
        B.clear () ; dfs ( 1 , 0 ) ;
        printf ("%lld
    " , B.query ( dis[n] ) ) ;
        system ("pause") ; return 0 ;
    }
    
  • 相关阅读:
    DS博客作业06--图
    DS博客作业05--树
    DS博客作业03--栈和队列
    DS博客作业02--线性表
    DS博客作业01--日期抽象数据类型设计与实现
    C语言博客作业06--结构体&文件
    c语言博客作业05--指针
    C语言博客作业03--函数
    DS博客作业--课程总结
    DS博客作业07--查找
  • 原文地址:https://www.cnblogs.com/Equinox-Flower/p/11737087.html
Copyright © 2011-2022 走看看