[POJ 3764] The xor-longest Path
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的更多相关文章
- 【POJ 3764】 The xor-longest path
[题目链接] http://poj.org/problem?id=3764 [算法] 首先,我们用Si表示从节点i到根的路径边权异或和 那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就 ...
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- 【POJ 3764】The Xor-longest Path
题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- poj 3764 The xor-longest Path(字典树)
题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...
- 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 ...
- 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 ...
- ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)
题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...
- Poj 3764 The xor-longest Path(Trie树+xor+贪心)
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6455 Accepted: 1392 ...
- poj 3764 The xor-longest Path (01 Trie)
链接:http://poj.org/problem?id=3764 题面: The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K ...
随机推荐
- Hive分区和桶
SMB 存在的目的主要是为了解决大表与大表间的 Join 问题,分桶其实就是把大表化成了“小表”,然后 Map-Side Join 解决之,这是典型的分而治之的思想.在聊 SMB Join 之前,我们 ...
- Redis实现数据库读写分离
Redis简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作 ...
- 网络基础tcp/ip协议一
计算机网络: 硬件方面:通过线缆将网络设备和计算机连接起来 软件方面:操作系统,应用软件,应用程序通过通信线路互连 实现资源共享,信息传递 计算机网络的功能: 数据通信 资源共享 增加可靠性 提高系统 ...
- PyQt5多点触控写字板实现及困惑
Qt支持程序多点触控,就想使用PyQt5做一个触控画板,经过几番周折,查阅了TouchEvent官方文档,又参考了一篇QT for Android的例子,采用eventfilter过滤器来识别触屏事件 ...
- An internal error occurred during: "Building workspace". java.lang.StackOverflowError
1 错误描述 2 错误原因 由上述描述可以,MyEclipse可用内存不足,导致堆内存溢出 3 解决办法 修改MyEclipse内存设置 #utf8 (do not remove) #utf8 (do ...
- ASP.NET 页面双向静态化
而我们预期的结果应该如下图,实际只请求两次. 用301重定向可以解决该循环请求产生的问题. OK, let's begin. 本文的Demo和Source是基于上一篇的,如果下面的一些文件或文件夹没有 ...
- Flex下拉框
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- jQuery.proxy()的用法
一:参考范文一 第一次接触jQuery.proxy()时感觉这个方法不实用,不明白它到底是个什么意思.今天来将jQuery官网上的解释进行一下翻译,顺便添加自己的理解和一些示例.proxy也可称为代理 ...
- monkey日志分析
Monkey测试的og分析,我们可以通过几个关键词来判断测试是否通过.1)Monkey finished打开LOG,查看log的最下端,是否有类似以下字段:## Network stats: elap ...
- 不为人知的scanf
这是一篇为老谭洗白的文章 前几天,我见有人在群里说,谭浩强那本书不咋样,还不少错误.我就看了看他发出来的错误 #include<stdio.h> int main(){ int a,b; ...