题意

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:
\[
_{xor}length(p)=\oplus_{e \in p}w(e)
\]
\(⊕\) 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?  

给一颗边权树,求出树上最长的异或和路径.

分析

参照jklover的题解。

利用异或的优秀性质,可以处理出节点 1 到每个点的距离 dis ,那么 u 和 v 之间的异或和距离直接就是 dis[u] ^ dis[v] .被重复计算的部分自身异或两次抵消了.

那么将 dis 数组求出后,问题就变为在这个数组中找两个数,使得这对数异或值最大.

使用 Trie 树的经典做法解决即可.

时间复杂度:31倍线性。

代码

POJ用vector会TLE,只能自己写边表。

//#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read()
{
    rg T data=0,w=1;
    rg char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        data=data*10+ch-'0';
        ch=getchar();
    }
    return data*w;
}
template<class T>il T read(rg T&x)
{
    return x=read<T>();
}
typedef long long ll;
using namespace std;

co int N=1e5+1,L=N*31;
int tot,root,ch[L][2],bin[31];
void turn(int x)
{
    for(int i=0;i<=30;++i,x>>=1)
        bin[i]=x&1;
}
int newnode()
{
    ++tot;
    memset(ch[tot],0,sizeof ch[tot]);
    return tot;
}
void ins()
{
    int u=root;
    for(int i=30;i>=0;--i)
    {
        if(!ch[u][bin[i]])
            ch[u][bin[i]]=newnode();
        u=ch[u][bin[i]];
    }
}
int find()
{
    int u=root,res=0;
    for(int i=30;i>=0;--i)
    {
        if(ch[u][bin[i]^1])
            res+=(1<<i),u=ch[u][bin[i]^1];
        else
            u=ch[u][bin[i]];
    }
    return res;
}

typedef pair<int,int> pii;
struct edge // edit 2: slow vector
{
    int to,nx,w;
}e[N*2];
int ecnt,h[N],v[N];
void add(int x,int y,int w)
{
    e[++ecnt].to=y,e[ecnt].w=w;
    e[ecnt].nx=h[x],h[x]=ecnt;
}
void dfs(int x,int fa,int val)
{
    for(int i=h[x];i;i=e[i].nx)
    {
        int y=e[i].to;
        if(y==fa) continue;
        dfs(y,x,v[y]=val^e[i].w);
    }
}

int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    int n;
    while(scanf("%d",&n)!=EOF) // edit 1: several test cases
    {
        tot=0,root=newnode();
        ecnt=0;
        fill(h+1,h+n+1,0);
        for(int i=1;i<n;++i)
        {
            int u=read<int>()+1,v=read<int>()+1,w=read<int>();
            add(u,v,w),add(v,u,w);
        }
        v[1]=0;dfs(1,0,0);
        turn(v[1]);ins();
        int ans=0;
        for(int i=2;i<=n;++i)
        {
            turn(v[i]);ins();
            ans=max(ans,find());
        }
        printf("%d\n",ans);
    }
    return 0;
}

POJ3764,BZOJ1954 The xor-longest Path的更多相关文章

  1. poj3764 The XOR Longest Path【dfs】【Trie树】

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

  2. 题解 bzoj1954【Pku3764 The xor – longest Path】

    做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...

  3. Solve Longest Path Problem in linear time

    We know that the longest path problem for general case belongs to the NP-hard category, so there is ...

  4. Why longest path problem doesn't have optimal substructure?

    We all know that the shortest path problem has optimal substructure. The reasoning is like below: Su ...

  5. BZOJ1954: Pku3764 The xor-longest Path

    题解: 在树上i到j的异或和可以直接转化为i到根的异或和^j到根的异或和. 所以我们把每个点到根的异或和处理出来放到trie里面,再把每个点放进去跑一遍即可. 代码: #include<cstd ...

  6. FB面经Prepare: Find Longest Path in a Multi-Tree

    给的多叉树, 找这颗树里面最长的路径长度 解法就是在子树里面找最大的两个(或一个,如果只有一个子树的话)高度加起来. 对于每一个treenode, 维护它的最高的高度和第二高的高度,经过该点的最大路径 ...

  7. SP1437 Longest path in a tree(树的直径)

    应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距 ...

  8. Educational DP Contest G - Longest Path (dp,拓扑排序)

    题意:给你一张DAG,求图中的最长路径. 题解:用拓扑排序一个点一个点的拿掉,然后dp记录步数即可. 代码: int n,m; int a,b; vector<int> v[N]; int ...

  9. [LeetCode] Longest Univalue Path 最长相同值路径

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

随机推荐

  1. NET中IL指令详解

    名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且 ...

  2. 【WIN7】windows\system32 下的几乎所有文件的简单说明【2】

    1: System32的详解 C:\WINDOWS\system32... 2:   3: 这个 system32 文件夹中包含了大量的用于 Windows 的文件. 这里主要用于存储 DLL 文件, ...

  3. Spark1.x和2.x如何读取和写入csv文件

    看很多资料,很少有讲怎么去操作读写csv文件的,我也查了一些.很多博客都是很老的方法,还有好多转来转去的,复制粘贴都不能看.下面我在这里归纳一下,以免以后用到时再费时间去查.前端实现文件下载和拖拽上传 ...

  4. 《 Python 学习手册 》读书笔记(1)

    关于运行程序 交互提示模式下编写代码 terminal中直接输入python开启 通过导入模块,运行文件中的语句 import exec(open('module.py').read()) UNIX可 ...

  5. 并发-AQS源码分析

    AQS源码分析 参考: http://www.cnblogs.com/waterystone/p/4920797.html https://blog.csdn.net/fjse51/article/d ...

  6. spring注解方式注入bean

    用注解的方式注入bean,spring的配置文件也要增加一些约束和导入注解所在的包 applicationContext.xml <?xml version="1.0" en ...

  7. Object有哪些方法?

    有9个方法 1 clone 2 toString() 3 getClass 4 finalize 5 equals 6 hascode 7 notify 8 notifall 9 wait

  8. 编辑器上的vim插件,常用到的快捷件

    [vim]常用到的插件命令 [Esc] 退出编辑(输入,插入)模式,回到命令模式中(常用)i, I进入插入模式(Insert mode):i 为『从目前光标所在处插入』, I 为『在目前所在行的第一个 ...

  9. ActiveMQ 性能调优

    本章重点 学习普通的性能调优技巧 怎么优化生产者和消费者 调优实例 简介 ActiveMQ 的性能高度依赖于多种不同的因素,包括:网络代理拓扑架构,传输层,底层网络的服务质量和速度,硬件,操作系统和 ...

  10. Pycharm for mac 快捷键

    cmd b 跳转到声明处(cmd加鼠标) opt + 空格 显示符号代码 (esc退出窗口 回车进入代码) cmd []光标之前/后的位置 opt + F7 find usage cmd backsp ...