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. linux redis基础应用 主从服务器配置

    Redis基础应用 redis是一个开源的可基于内存可持久化的日志型,key-value数据库redis的存储分为内存存储,磁盘存储和log文件三部分配置文件中有三个参数对其进行配置 优势:和memc ...

  2. 关于编译FFMPEG的初级教程

    首先我们要下载相关工具,这里不多说,大家按照我的地址去下载文件就好了 MINGW下载地址:http://prdownloads.sourceforge.net/mingw/MinGW-3.1.0-1. ...

  3. hi3531的时钟系统

    时钟管理模块对芯片时钟输入.时钟生成和控制进行统一的管理,包括: 时钟管理模块有以下两部分输入:

  4. dojo中的dojox/grid/EnhancedGrid表格报错

    1.错误截图 2.错误出处 <body class="claro"> <div id="gridContainer"> <span ...

  5. 如何修改WinPE Boot的.wim镜像文件

    1. 使用imagex /apply或imagex /mountrw将WIM镜像文件mount到某个文件夹,假设为d:\tmp\winpe_x86\mount.  例: imagex /mountrw ...

  6. MyEclipse完善提示配置

    MyEclipse完善提示配置 一般的,MyEclipse中的提示以"."后进行提示,不是很完善.现在,修改提示配置,让提示更完善! 具体操作如下: 1.打开MyEclipse,单 ...

  7. org.apache.subversion.javahl.ClientException: Attempted to lock an already-locked dir

    1.错误描述 org.apache.subversion.javahl.ClientException: Attempted to lock an already-locked dir svn: Co ...

  8. JavaScript解决select下拉框中的内容太长显示不全的问题

    JavaScript解决select下拉框中的内容太长显示不全的问题 1.说明 有些情况下,select下拉框的内容过长,导致部分看不见: 现在通过鼠标事件,让下拉框中的内容显示完全 2.实现源码 & ...

  9. 决策树系列(五)——CART

    CART,又名分类回归树,是在ID3的基础上进行优化的决策树,学习CART记住以下几个关键点: (1)CART既能是分类树,又能是分类树: (2)当CART是分类树时,采用GINI值作为节点分裂的依据 ...

  10. http协议的补充二

    一,浏览器到服务器request 1.1,浏览器里面的内容 请求(浏览器->服务器) GET /day09/hello HTTP/1.1 Host: localhost:8080 User-Ag ...