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. JavaScript操作符汇总

    操作符 JavaScript 有赋值.比较.算术.位.逻辑.字符串和特殊运算符.本章描述了操作符,以及关于操作符优先级的一些信息. 表 2.1 JavaScript 所有操作符简明列表. 表 2.1 ...

  2. phpcms V9 默认模板文件目录结构_PHPCms教程

    PHPCMSV9 默认模板目录结构 templates   框架系统模板目录            announce    公告                show.html       内容页  ...

  3. 基于LVDS/M-LVDS的数据通信

    现在有两种方案:一种基于 M-LVDS (基于总线的多节点通信) ,有其 特定的电气要求:另外一种是基于 LVDS 的点到点通信,具体说明如 下: 基于 M-LVDS 的总线通信: 基于 M-LVDS ...

  4. HighCharts基本折线图

    1.设计源码 <!DOCTYPE html> <!-- To change this license header, choose License Headers in Projec ...

  5. 如何给filter添加自定义接口

    .在Cfilter类的定义中实现Interface接口的函数的定义: //-----------------------Interface methods----------------------- ...

  6. h264多slice

    1, h264编码以macroblock为最小单位(简称MB),多个连续的MB组成一个slice,每个slice编码输出一个NALU 2, 划分slice的方式可以按照固定个数MB的方式: 也可以以对 ...

  7. Dshell----开源攻击分析框架

    前言 随着互联网的高速发展,网络安全问题变得至关重要,随着网络的不断规模化和复杂化,网络中拒绝服务(Denial of Service,DoS)攻击和分布式拒绝服务(Distributed Denia ...

  8. 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)

    深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...

  9. 教你如何制作网页上的友情链接--JavaScript基础

    大部分网站的首页都有友情链接的功能,此功能可通过location对象的href属性来实现…… href属性:设置或检索完整的url字符串 1."友情链接制作"示例代码: <! ...

  10. 搭建基于eclipse的纯的JavaWeb项目

    -----------2016.9.9--------------------- 步骤: 1.搭建一个Java项目 2,字该项目下新建一个文件夹,表示根,名字为webapp(name随意) 3,在we ...