• P4551 最长异或路径


    P4551 最长异或路径

    挺裸的01trie吧,dfs的时候算一下这个点到根路径异或和,加进trie,查一下和trie里面的异或和最大的。

    就当用来存一下基础的01trie的板子吧

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    #define MAXN 100006
    
    int read(  ) {
        char ch = ' '; int x = 0;
        while( ch < '0' || ch > '9' ) ch = getchar();
        while( ch >= '0' && ch <= '9' ) x *= 10 , x += ch - '0' , ch = getchar();
        return x;
    }
    
    int n;
    int head[MAXN] , nxt[MAXN<<1] , to[MAXN<<1] , ww[MAXN << 1] , ecnt = 0;
    void ade( int u , int v , int w ) {
        nxt[++ecnt] = head[u] , to[ecnt] = v , ww[ecnt] = w , head[u] = ecnt ;
    }
    
    int ch[MAXN * 32][2] , cnt = 0;
    int getmx( int x ) {
        int cur = 0 , res = 0;
        for( int dep = 30 ; dep >= 0 ; -- dep )
            if( ( x >> dep ) & 1 ) {
                if( ch[cur][0] ) cur = ch[cur][0] , res |= ( 1 << dep );
                else cur = ch[cur][1];
            } else {
                if( ch[cur][1] ) cur = ch[cur][1] , res |= ( 1 << dep );
                else cur = ch[cur][0];
            }
        return res;
    }
    void ins( int x ) {
        int cur = 0;
        for( int dep = 30 ; dep >= 0 ; -- dep ) {
            if( ( x >> dep ) & 1 ) {
                if( ch[cur][1] ) cur = ch[cur][1];
                else cur = ch[cur][1] = ++ cnt;
            } else {
                if( ch[cur][0] ) cur = ch[cur][0];
                else cur = ch[cur][0] = ++ cnt;
            }
        }
    }
    int sdep[MAXN] , res = 0;
    void dfs( int x , int fa ) {
        res = max( res , getmx( sdep[x] ) ) , ins( sdep[x] );
        for( int i = head[x] ; i ; i = nxt[i] ) {
            int v = to[i];
            if( v == fa ) continue;
            sdep[v] = sdep[x] ^ ww[i];
            dfs( v , x );
        }
    }
    
    int main() {
        cin >> n;
        for( int i = 1 , u , v , w ; i < n ; ++ i )
            u = read() , v = read() , w = read() , ade( u , v , w ) , ade( v , u , w );
        dfs( 1 , 1 );
        cout << res << endl;
    }
    
  • 相关阅读:
    java exception
    【洛谷P1627】 【CQOI2009】中位数
    切蛋糕
    【NOIP2015Day2T2】【洛谷P2679】子串
    【NOIP2017Day1T3】【洛谷P3953】逛公园
    【bzoj1082】【SCOI2005】栅栏
    搬砖
    花花的森林
    跳跳棋
    异或
  • 原文地址:https://www.cnblogs.com/yijan/p/11397238.html
Copyright © 2020-2023  润新知