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. 素数&欧拉函数

    素数表 const int maxN找[1,maxN)内的素数 int prime[int I]第I个素数 const int maxN=1e5+5; int prime[maxN]; bool ma ...

  2. Vant Weapp小程序蹲坑之使用card组件显示价格

    问题 在基于mpvue+Vant Weapp组件库实战过程中,问题越来越多.网络上所谓的"坑"总结,仅仅不过是其开发中所遭所遇之"坑"而已--估计后面的&quo ...

  3. FastReport.Net中使用列表和数组作为报表数据源

    大多数现代报告工具允许您使用几乎任何数据库,然而,并不是所有报表工具都能以一个数据源的列表或数组来工作.本文中将展示如何使用FastReport .Net报表工具来实现. 请注意以下重要几点: 清单中 ...

  4. Appium-desktop 元素定位

    1.打开 appium-desktop ,点击 start session 2.打开后,点击屏幕右上角的搜索按钮 3.然后会打开配置页面,在本地服务配置信息同上面写的代码链接配置.填入正确的信息后,点 ...

  5. HBase Filter 过滤器之RowFilter详解

    前言:本文详细介绍了HBase RowFilter过滤器Java&Shell API的使用,并贴出了相关示例代码以供参考.RowFilter 基于行键进行过滤,在工作中涉及到需要通过HBase ...

  6. 线性回归 - LinearRegression - 预测糖尿病 - 量化预测的质量

    线性回归是分析一个变量与另外一个或多个变量(自变量)之间,关系强度的方法. 线性回归的标志,如名称所暗示的那样,即自变量与结果变量之间的关系是线性的,也就是说变量关系可以连城一条直线. 模型评估:量化 ...

  7. django项目的uwsgi方式启停脚本

    #!/bin/sh NAME="fushentang" if [ ! -n "$NAME" ];then echo "no arguments&quo ...

  8. 消息队列,RabbitMQ、Kafka、RocketMQ

    目录 1.消息列队概述 1.1消息队列MQ 1.2AMQP和JMS 1.2.1AMQP 1.2.2JMS 1.2.3AMOP 与 JMS 区别 1.3消息队列产品 1.3.1 Kafka 1.3.2 ...

  9. Polycarp and Div 3 CodeForces - 1005D

    这个题目其实很简单,有很多的方法写,然后我还是不会写,感觉自己好菜, 我开始想的是dp,但是不知道怎么dp,看了网上题解,豁然开朗 dp[i] 表示前面i个数满足条件的数有多少,f[s]表示前缀和为s ...

  10. Java 函数式接口

    目录 Java 函数式接口 1. 函数式接口 1.1 概念 1.2 格式 1.3 函数式接口的使用 2. 函数式编程 2.1 Lambda的延迟执行 性能浪费的日志案例 使用Lambda表达式的优化 ...