The xor-longest Path
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6853   Accepted: 1464

Description

In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p:

⊕ is the xor operator.

We say a path the xor-longest path if it has the largest xor-length. Given an edge-weighted tree with n nodes, can you find the xor-longest path?  

Input

The input contains several test cases. The first line of each test case contains an integer n(1<=n<=100000), The following n-1 lines each contains three integers u(0 <= u < n),v(0 <= v < n),w(0 <= w < 2^31), which means there is an edge between node u and v of length w.

Output

For each test case output the xor-length of the xor-longest path.

Sample Input

4
0 1 3
1 2 4
1 3 6

Sample Output

7

Hint

The xor-longest path is 0->1->2, which has length 7 (=3 ⊕ 4)

先求出来到节点1的,然后字典树从高位到低位。异或和的性质以及贪心?。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e5+;
int head[N],tot,sz,val[N];
struct node
{
    int v,next,w;
} e[N<<];
struct Trie
{
    int next[];
    void init()
    {
        memset(next,,sizeof(next));
    }
} Ti[N*];
void add(int u,int v,int w)
{
    e[tot].v=v;
    e[tot].next=head[u];
    e[tot].w=w;
    head[u]=tot++;
    e[tot] .v=u;
    e[tot].next=head[v];
    e[tot].w=w;
    head[v]=tot++;
}
void Add(int x)
{
    int u=,ind;
    for(int i=; i>=; --i)
    {
        if((<<i)&x) ind=;
        else ind=;
        if(!Ti[u].next[ind])
        {
            Ti[u].next[ind]=++sz;
            Ti[sz].init();
        }
        u=Ti[u].next[ind];
    }
}
int get(int x)
{
    int u=,num=,ind;
    for(int i=; i>=; --i)
    {
        if((<<i)&x) ind=;
        else ind=;
        if(Ti[u].next[ind])
        {
            u=Ti[u].next[ind];
            num|=(<<i);
        }
        else u=Ti[u].next[!ind];
    }
    return num;
}
void dfs(int x,int v,int fa)
{
    val[x]=v;
    for(int i=head[x]; i+; i=e[i].next)
    {
        int to=e[i].v;
        if(to==fa) continue;
        dfs(to,e[i].w^v,x);
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        tot=sz=;
        int x,y,z;
        memset(head,-,sizeof(head));
        for(int i=; i<n; ++i)
        {
            scanf("%d%d%d",&x,&y,&z);
            add(x+,y+,z);
        }
        Ti[].init();  //必须初始化
        dfs(,,);
        int maxx=;
        for(int i=; i<=n; ++i)
        {
            maxx=max(maxx,get(val[i]));
            Add(val[i]);
        }
        printf("%d\n",maxx);
    }
}

poj3764字典树路径最大异或和的更多相关文章

  1. AcWing:143. 最大异或对(01字典树 + 位运算 + 异或性质)

    在给定的N个整数A1,A2……ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少? 输入格式 第一行输入一个整数N. 第二行输入N个整数A1A1-ANAN. 输出格式 输出一 ...

  2. HDU4825 Xor Sum(字典树解决最大异或问题)

    Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整 ...

  3. hdu5536 Chip Factory 字典树+暴力 处理异或最大 令X=(a[i]+a[j])^a[k], i,j,k都不同。求最大的X。

    /** 题目:hdu5536 Chip Factory 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:给定n个数,令X=(a[i]+a[j] ...

  4. POJ 3764 The xor-longest( 树上异或前缀和&字典树求最大异或)

    In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edg ...

  5. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)

    http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...

  6. newcoder 筱玛的迷阵探险(搜索 + 01字典树)题解

    题目描述 筱玛是个快乐的男孩子. 寒假终于到了,筱玛决定请他的朋友们一起来玩迷阵探险. 迷阵可以看做一个n×nn×n的矩阵A,每个格子上有一个有一个数Ai,j. 入口在左上角的(1,1)处,出口在右下 ...

  7. 算法笔记--字典树(trie 树)&& ac自动机 && 可持久化trie

    字典树 简介:字典树,又称单词查找树,Trie树,是一种树形结构,是哈希树的变种. 优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较. 性质:根节点不包含字符,除根节点外每一个 ...

  8. cf842D 01字典树|线段树 模板见hdu4825

    一般异或问题都可以转换成字典树的问题,,我一开始的想法有点小问题,改一下就好了 下面的代码是逆向建树的,数据量大就不行 /*3 01字典树 根据异或性质,a1!=a2 ==> a1^x1^..^ ...

  9. HDU 4825 Xor Sum (模板题)【01字典树】

    <题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...

随机推荐

  1. OpenStack 删除instance 和其附加的volumes

    在openstack里面有时候删除instance时,volume无法跟着删除,可以自己编写脚本来实现, 脚本代码如下: #!/bin/bash for i in $(cat /root/host-d ...

  2. vue中 $refs的基本用法

    骚年,我看你骨骼惊奇,有撸代码的潜质,这里有324.57GB的前端学习资料传授于你!什么,你不信??? 先随便看几个图: 肯定没看够.再来个GIF图热个身??? 那么问题来了,如果你也想入坑前端或者学 ...

  3. bfs—Catch That Cow—poj3278

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 87152   Accepted: 27344 ...

  4. DeepWalk论文精读:(1)解决问题&相关工作

    模块1 1. 研究背景 随着互联网的发展,社交网络逐渐复杂化.多元化.在一个社交网络中,充斥着不同类型的用户,用户间产生各式各样的互动联系,形成大小不一的社群.为了对社交网络进行研究分析,需要将网络中 ...

  5. USACO 2.1 海明码 Hamming Codes (模拟+位运算+黑科技__builtin_popcount(n))

    题目描述 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个单位 ...

  6. 从0开始搭建精灵宝可梦的检测APP

    从0开始搭建精灵宝可梦的检测APP 本文为本人原创,转载请注明来源链接 环境要求 Tensorflow1.12.0 cuda 9.0 python3.6.10 Android Studio Anaco ...

  7. FileStream提示文件正在由另一进程使用的解决方法

    文件正在由另一进程使用…… FileStream fs = new FileStream(strFilePath, FileMode.Open,FileAccess.Read,FileShare.Re ...

  8. Java——TCP/IP超详细总结

    网络的基础知识 一.协议 1.简介: 在计算机网络与信息通信领域里,人们经常提及“协议”一词.互联网中常用的具有代表性的协议有IP.TCP.HTTP等.而LAN(局域网)中常用的协议有IPX/SPX” ...

  9. mui指南

    转自https://www.cnblogs.com/koleyang/p/5146623.html http://dev.dcloud.net.cn/mui/ui/index.html#mask ht ...

  10. 浅析java中ClassLoader如何加载Class

    我的博客地址:https://blog.csdn.net/qq_41907991 ClassLoader是一个经常出现又让很多人望而却步的词.本文试图以最浅显易懂的方式来讲解ClassLoader,希 ...