Description

多组数据

给你一颗树,

然后求一条最长异或路径,

异或路径长度定义为两点间简单路径上所有边权的异或和。

Solution

首先 dfs 一遍,求出所有的点到根节点(随便选一个)的边权的异或和,用 D 数组来存下。

不难发现,树上 x 到 y 的路径上所有边权的 xor 结果就等于 D[x] xor D[y]。这是因为根据 xor 的性质 (a xor a = 0),“ x 到根 ” 和 “ y 到根 ”这两条路径重叠的部分恰好抵消掉。

所以,问题就变成了从 D[1]~D[N] 这 N 个数中选出两个,xor 的结果最大。

可以用 Trie 树来快速求解。

upd:wa了不下十次数组开大点就能A..  Trie的空间是玄学=.=

Code

#include<cstdio>
#include<cstring>
#include<iostream>
#define N 200015
#define int long long
using namespace std;

];
],d[N<<];
int n,cnt,tot,maxn;

struct Edge{
    int to,nxt,dis;
}edge[N<<];

struct Trie{
    int zero,one;
}trie[N<<];

void add(int x,int y,int z){
    edge[++cnt].to=y;
    edge[cnt].nxt=head[x];
    edge[cnt].dis=z;
    head[x]=cnt;
}

void clear(){
    tot=maxn=;
    memset(d,,sizeof d);
    memset(vis,,sizeof vis);
    memset(head,,sizeof head);
    memset(edge,,sizeof edge);
    memset(trie,,sizeof trie);
}

void dfs(int now){
    vis[now]=;
    for(int i=head[now];i;i=edge[i].nxt){
        int to=edge[i].to;
        if(vis[to]) continue;
        d[to]=d[now]^edge[i].dis;
        dfs(to);
    }
}

void insert(int x){
    ;
    ;~i;i--){
        <<i)){
            if(!trie[now].one) trie[now].one=++tot;
            now=trie[now].one;
        }
        else{
            if(!trie[now].zero) trie[now].zero=++tot;
            now=trie[now].zero;
        }
    }
}

int query(int x){
    ,sum=;
    ;~i;i--){
        <<i));
        if(k){
            <<i,now=trie[now].one;
            else now=trie[now].zero;
        }
        else{
            <<i,now=trie[now].zero;
            else now=trie[now].one;
        }
    }
    return sum;
}

signed main(){
    while((scanf("%lld",&n))!=EOF){
        clear();
        ;i<n;i++){
            scanf("%lld%lld%lld",&x,&y,&z);
            add(x+,y+,z);add(y+,x+,z);
        }
        dfs();
        ;i<=n;i++)
            maxn=max(maxn,query(d[i])),insert(d[i]);
        printf("%lld\n",maxn);
    }
    ;
}

[POJ 3764] The xor-longest Path的更多相关文章

  1. 【POJ 3764】 The xor-longest path

    [题目链接] http://poj.org/problem?id=3764 [算法] 首先,我们用Si表示从节点i到根的路径边权异或和 那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就 ...

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

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

  3. 【POJ 3764】The Xor-longest Path

    题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...

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

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

  5. poj 3764 The xor-longest Path(字典树)

    题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...

  6. 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 ...

  7. 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 ...

  8. ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)

    题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...

  9. Poj 3764 The xor-longest Path(Trie树+xor+贪心)

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

  10. poj 3764 The xor-longest Path (01 Trie)

    链接:http://poj.org/problem?id=3764 题面: The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K ...

随机推荐

  1. ubuntu常用命令操作

    建立文件夹软链接 ln -s 源文件 目标文件 当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在其它的目录下 ...

  2. GIT 查看 删除 添加远程库

    查看远程库 $ git remote -vorigin https://github.com/autoliuweijie/MachineLearning.git (fetch)origin https ...

  3. 简化的CDN架构分析

    CDN架构的设计目标是通过复制系统资源(即Web服务器)的方式来获得高性能和高扩展性,为了能确保在海量内容下可以稳定提供高性能的服务.系统资源的复制可以在本地和地理两个尺度上进行.如果是本地复制则响应 ...

  4. MyBatis入门一

    本人只是刚刚学习MyBatis,作为学习路程的记录,写的不好,不完善的地方请多多包涵: 首先,先比较一下Hibernate和MyBatis两种框架之间的区别: 两种都是ORM框架,但是Hibernat ...

  5. 学习笔记︱Nvidia DIGITS网页版深度学习框架——深度学习版SPSS

    DIGITS: Deep Learning GPU Training System1,是由英伟达(NVIDIA)公司开发的第一个交互式深度学习GPU训练系统.目的在于整合现有的Deep Learnin ...

  6. PCI9054 突发模式数据传输 (burst mode data transfer )

    C mode target slave , 之前看PCI9054 datasheet知道这个burst mode ,也看了时序图,但是一直缺乏一个感性的认识. 今天网上买的 USB逻辑分析仪到货了,接 ...

  7. mysql常用基础操作语法(十一)~~字符串函数【命令行模式】

    注:sql的移植性比较强,函数的移植性不强,一般为数据库软件特有,例如mysql有mysql的函数,oracle有oracle的函数. 1.concat连接字符串: 从上图中可以看出,直接使用sele ...

  8. Java Web项目(Extjs)报错三

    1. Java Web项目(Extjs)报错三 具体报错如下: at org.jbpm.pvm.internal.processengine.SpringHelper.createProcessEng ...

  9. Linux显示系统的诊断信息

    Linux显示系统的诊断信息 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ dmesg [ 1.492480] EDD information not ava ...

  10. GetBitmapFromScreen

    int GetBitmapFromScreen() { char *lpBuf; HBITMAP hBitmap,hOld ; HDC hDC,hcDC; BITMAP bb;BITMAPINFO b ...