[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\n" , B.query ( dis[n] ) ) ;
    system ("pause") ; return 0 ;
}
posted @ 2019-10-25 11:30  Phecda  阅读(112)  评论(0编辑  收藏  举报

Contact with me