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. zabbix监控ftp

    [root@agent ~]# yum -y install vsftpd [root@agent ~]# systemctl start vsftpd[root@agent ~]# systemct ...

  2. JAVA编程思想 Ch3.5题

    练习5:创建一个class类,包含连两个String字段 :name.says.在main方法中创建两个Dog方法 一个命名为spot 叫声为 Ruff,另一个命民为scruffy,叫声为:Wuff: ...

  3. P1522 牛的旅行 Cow Tours(floyd)

    题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...

  4. The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 K题 center

    You are given a point set with nn points on the 2D-plane, your task is to find the smallest number o ...

  5. 涉及secureCRT中文显示的一些设置

    1.secureCRT中文显示乱码: 如果你的linux本身是显示着中文的,可进行如下设置: 选项->会话选项 外观->字符编码改为UTF-8,确定即可 2.secureCRT中文横向显示 ...

  6. C. Anton and Fairy Tale(数学推式子)

    \(数学题,式子并不难推,但边界是真的烦\) \(\color{Red}{Ⅰ.其实可以发现,当m>=n时,每次都可以粮食补到n,所以一定是在第n天消耗完毕}\) \(\color{Purple} ...

  7. Collection接口【集合】和Iterator迭代器类

    1.1集合的概述 前面基础学习并使用过集合ArrayList<E>,那么集合究竟是什么呢? 集合:集合是Java中提供的一种容器,可以用来存储多个数据. 那么意思就是说集合是容器,但是容器 ...

  8. node基础知识-说说对node的理解

    一.说说你对node的理解 从定义+特点+作用来说对node的理解 定义:node是基于Chrmo v8引擎的JavaScript运行环境; 特点:具有事件驱动,非阻塞I/O模型,高并发和轻量级,单线 ...

  9. 一文教你快速学会在matlab的simulink中调用C语言进行仿真

    本文介绍如何在matlab的simulink中嵌入C语言进行多输入多输出的仿真:matlab版本位2015b: 创作不易,如果本文帮到了您: 如果本文帮到了您,请帮忙点个赞

  10. 关于Fragment的点击切换数据滞留问题

    场景再现:当我使用tabLayout + Fragment 切换不同的fragment时,出现了数据重复显示的问题: 思考逻辑: - 每次切换fragment都会重新获取数据,但是list集合是全局的 ...