链接:http://poj.org/problem?id=3764

题面:

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

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)

思路;

树上dfs一遍跟区间差不多的写法

实现代码;

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
const int M = 4e5+;
int tot;
int ch[*M][],cnt,head[M];
int val[*M],a[M],pre[M],nex[M],dp[M],ans; struct node{
int to,next;
ll w;
}e[M*]; void add(int u,int v,int w){
e[++cnt].to = v;e[cnt].w = w;e[cnt].next = head[u];head[u] = cnt;
} void init(){
tot = ; ans = ; cnt = ;
ch[][] = ch[][] = ;
memset(head,,sizeof(head));
} void ins(ll x){
int u = ;
for(int i = ;i >= ;i --){
int v = (x>>i)&;
if(!ch[u][v]){
ch[tot][] = ch[tot][] = ;
val[tot] = ;
ch[u][v] = tot++;
}
u = ch[u][v];
}
val[u] = x;
} int query(int x){
int u = ;
for(int i = ;i >= ;i --){
int v = (x>>i)&;
if(ch[u][v^]) u = ch[u][v^];
else u = ch[u][v];
}
return x^val[u];
} void dfs(int u,int fa,int val){
ins(val);
for(int i = head[u];i;i = e[i].next){
int v = e[i].to;
if(v == fa) continue;
ans = max(ans,query(val^e[i].w));
dfs(v,u,val^e[i].w);
}
} int main()
{
int n,u,v,w;
while(scanf("%d",&n)!=EOF){
init();
for(int i = ;i < n;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w); add(v,u,w);
}
dfs(,-,);
printf("%d\n",ans);
}
}

poj 3764 The xor-longest Path (01 Trie)的更多相关文章

  1. hdu 4825 Xor Sum (01 Trie)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...

  2. 【POJ 3764】The Xor-longest Path

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

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

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

  4. 【POJ 3764】 The xor-longest path

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

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

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

  6. POJ 3376 Finding Palindromes(扩展kmp+trie)

    题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...

  7. POJ - 3376 Finding Palindromes(拓展kmp+trie)

    传送门:POJ - 3376 题意:给你n个字符串,两两结合,问有多少个是回文的: 题解:这个题真的恶心,我直接经历了5种错误类型 : ) ... 因为卡内存,所以又把字典树改成了指针版本的. 字符串 ...

  8. bzoj 4260: Codechef REBXOR (01 Trie)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=4260 题面: 4260: Codechef REBXOR Time Limit: 10 S ...

  9. [一本通学习笔记] 字典树与 0-1 Trie

    字典树中根到每个结点对应原串集合的一个前缀,这个前缀由路径上所有转移边对应的字母构成.我们可以对每个结点维护一些需要的信息,这样即可以去做很多事情. #10049. 「一本通 2.3 例 1」Phon ...

随机推荐

  1. Dynamics 365中的应用程序介绍

    本人微信和易信公众号:微软动态CRM专家罗勇 ,回复275或者20180630可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me ...

  2. 如何打开iPhone 中的heic格式照片

    苹果手机拍出来的heic照片相比常见的格式占存更小,但是在安卓和电脑上这种格式无法直接查看,那怎么在电脑上查看呢,需要将其转换格式就可以查看,关于如何打开iPhone 中的heic格式照片,下面即将为 ...

  3. CentOS 7安装指南

    CentOS 7安装指南(U盘版) 一.准备阶段 1.下载CentOS7镜像文件(ISO文件)到自己电脑,官网下载路径: http://isoredirect.centos.org/centos/7/ ...

  4. SBC数据格式转换软件

    北京博信施科技有限公司是一家专业从事数据格式转换.数据处理领域研发软件产品和解决方案实施的技术型公司.在当今信息时代,PDF文档格式是在Internet上进行电子文档发行和数字化信息传播的理想文档格式 ...

  5. Redis订阅与发布

    发布与订阅模型在许多编程语言中都有实现,也就是我们经常说的设计模式中的一种--观察者模式.在一些应用场合,例如发送方并不是以固定频率发送消息,如果接收方频繁去咨询发送方,这种操作无疑是很麻烦并且不友好 ...

  6. 作业三——安卓系统文件助手APP原型设计

    原型地址:https://modao.cc/app/X2totLUvbcxBwtJGk6AI04FZnGD4s08#screen=s43A40176351539085924682 MVP支持的浏览器: ...

  7. 测者的测试技术手册:自动化单元工具EvoSuie的代码覆盖报告

    EvoSuite是由Sheffield等大学联合开发的一种开源工具,用于自动生成测试用例集,生成的测试用例均符合Junit的标准,可直接在Junit中运行.得到了Google和Yourkit的支持. ...

  8. 微信小程序下拉刷新和上拉加载的实现

    一: 下拉刷新 下拉刷新两个步骤就能实现. 1.在要实现下拉刷新的页面的json配置文件里面加上 "enablePullDownRefresh": true, //开启下拉刷新 & ...

  9. GitHub下载克隆clone指定的分支tag代码

    需求描述: 这边有很多tag分支版本的代码,我想克隆下载指定版本到我服务器上面 例如:我想下载tag:1.4.1的代码 解决方法: 命令:git clone --branch [tags标签] [gi ...

  10. keepalived+nginx负载均衡+ApacheWeb实现高可用

    1.Keepalived高可用软件 Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此,kee ...