码迷,mamicode.com
首页 > 其他好文 > 详细

P4551 最长异或路径

时间:2019-08-23 00:03:10      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:str   end   main   ring   string   void   efi   最大的   +=   

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;
}

P4551 最长异或路径

标签:str   end   main   ring   string   void   efi   最大的   +=   

原文地址:https://www.cnblogs.com/yijan/p/11397238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!