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. docker容器介绍

                       Docker容器 一.什么是Docker? Docker时Docker.Lnc公司开源的一个基于LXC技术之上搭建的Container容器引擎,源代码托管在Git ...

  2. CF1285 --- Dr. Evil Underscores

    CF1285 --- Dr. Evil Underscores 题干 Today as a friendship gift, Bakry gave Badawy \(n\) integers \(a_ ...

  3. 【React踩坑记六】create-react-app创建的react项目通过iP地址访问(实现局域网内访问)

    同项目组的小伙伴想用自己的电脑访问我电脑上开发阶段的create-react-app创建的react项目. 试过了了各种内网穿透工具ngrok以及localtunnel等. 奈何打开效率实在太过于龟速 ...

  4. mybatis源码学习(四):动态SQL的解析

    之前的一片文章中我们已经了解了MappedStatement中有一个SqlSource字段,而SqlSource又有一个getBoundSql方法来获得BoundSql对象.而BoundSql中的sq ...

  5. Elasticsearch: 权威指南 » 深入搜索 » 多字段搜索 » 多数字段 good

      跨字段实体搜索  » 多数字段编辑 全文搜索被称作是 召回率(Recall) 与 精确率(Precision) 的战场: 召回率 --返回所有的相关文档:精确率 --不返回无关文档.目的是在结果的 ...

  6. javaweb系统调优方案

    1. java代码优化 java代码优化6大原则 : https://blog.csdn.net/bunny1024/article/details/72803708 java代码优化: https: ...

  7. MODIS系列之NDVI(MOD13Q1)五:NDVI处理流程

    前言:(个人建议) 1.进行数据处理工作,由于通常数据量比较大.所以在个人电脑中,要将基础数据.不断增长的过程数据.结果数据等分门别类,使得简单易找. 2.将重要数据备份,因为在数据处理过程中,由于出 ...

  8. POJ2376Cleaning Shifts(区间覆盖贪心)

    应该还是蛮简单的一题,但是因为模拟太差,一直没调出来....... \(显而易见的应该按照左区间从小到大排序,相等按照右区间大到小排序\). \(那么第一个区间的l一定要是1,而且必拿(否则没有区间能 ...

  9. P1680 奇怪的分组(组合数+逆元)

    传送门戳我 首先将n减去所有的Ci,于是是原问题转换为:n个相同的球放入m个不同盒子里,不能为空,求方案数. 根据插空法:n个球,放到m个箱子里去不能为空,也就是有m-1块板子放在n-1个空隙之间 那 ...

  10. Python-SHA256加密算法接口测试

    前言 小伙伴们在做接口测试的时候,是否遇到一些需要加密的接口,但是不知如何进行测试呢?今天我们来学习一下SHA256加密算法接口如何进行测试的. SHA256加密算法介绍: 比特币挖矿的御用算法 SH ...