POJ3764 The xor-longest Path
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 6361 | Accepted: 1378 |
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
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)
Source
trie树+贪心
DFS预处理出每个结点到根的路径的异或和。两点之间路径的异或和等于各自到根的路径的异或和的异或。
将所有的异或和转化成二进制串,建成trie树。
对于每个二进制串,在trie树上贪心选取使异或值最大的路径(尽量通往数值相反的结点),记录最优答案。
↑为了防止匹配到自身的一部分,每个二进制串都应该先查完再插入trie树。
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt,w;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v,int w){
e[++mct].v=v;e[mct].nxt=hd[u];e[mct].w=w;hd[u]=mct;return;
}
int t[mxn*][],cnt=;
int n,ans=;
int f[mxn];
void insert(int x){
int now=;
for(int i=;i>=;i--){
int v=(x>>i)&;
if(!t[now][v])t[now][v]=++cnt;
now=t[now][v];
}
return;
}
void query(int x){
int now=;
int res=;
for(int i=;i>=;i--){
int v=(x>>i)&;
if(t[now][v^]){
v^=;
res+=(<<i);
}
now=t[now][v];
}
ans=max(res,ans);
return;
}
void DFS(int u,int fa,int num){
f[u]=num;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==fa)continue;
DFS(v,u,num^e[i].w);
}
return;
}
void init(){
memset(hd,,sizeof hd);
memset(t,,sizeof t);
// memset(f,0,sizeof f);
mct=;cnt=;ans=;
return;
}
int main(){
while(scanf("%d",&n)!=EOF){
init();
int i,j,u,v,w;
for(i=;i<n;i++){
u=read();v=read();w=read();
u++;v++;
add_edge(u,v,w);
add_edge(v,u,w);
}
DFS(,,);
for(i=;i<=n;i++){
// printf("f[%d]:%d\n",i,f[i]);
query(f[i]);
insert(f[i]);
}
printf("%d\n",ans);
}
return ;
}
POJ3764 The xor-longest Path的更多相关文章
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- 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 ...
- 【poj3764】 The xor-longest Path
http://poj.org/problem?id=3764 (题目链接) 今天的考试题,看到异或就有点虚,根本没往正解上想.. 题意 给出一棵带权树,请找出树上的一条路径,使其边上权值的异或和最大. ...
- POJ3764,BZOJ1954 The xor-longest Path
题意 In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of ...
- 【poj3764】The xor-longest Path
The xor-longest Path Description In an edge-weighted tree, the xor-length of a path p is defined as ...
- FB面经Prepare: Find Longest Path in a Multi-Tree
给的多叉树, 找这颗树里面最长的路径长度 解法就是在子树里面找最大的两个(或一个,如果只有一个子树的话)高度加起来. 对于每一个treenode, 维护它的最高的高度和第二高的高度,经过该点的最大路径 ...
- SP1437 Longest path in a tree(树的直径)
应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距 ...
- Educational DP Contest G - Longest Path (dp,拓扑排序)
题意:给你一张DAG,求图中的最长路径. 题解:用拓扑排序一个点一个点的拿掉,然后dp记录步数即可. 代码: int n,m; int a,b; vector<int> v[N]; int ...
随机推荐
- 将函数传给webworker
var zWorker = function (func,cb){ var node = document.createElement('script'),workerId='worker' + Da ...
- ${pageContext.request.contextPath}无效
发现在Tomcat7.0.58,在jsp页面使用${pageContext.request.contextPath}获取不到项目名称,网上找了很多答案试了都无效: 把Tomcat版本换成Tomcat7 ...
- 20145222GDB调试汇编堆栈过程分析
GDB调试汇编堆栈过程分析 实践代码example.c #include<stdio.h> short addend1 = 1; static int addend2 = 2; const ...
- Hashtable Dictionary List 谁效率更高
一 前言 很少接触HashTable晚上回来简单看了看,然后做一些增加和移除的操作,就想和List 与 Dictionary比较下存数据与取数据的差距,然后便有了如下的一此测试, 当然我测的方法可能不 ...
- Web Api 2 接口API文档美化
使用用第三方提供的swgger ui 帮助提高 web api 接口列表的阅读性,并且可以在页面中测试服务接口. 运行程序如下: 注意:在IE中必须输入红色部分. 并且可以对方法进行测试. 在开发we ...
- scrollLeft的相关问题(js横向无缝滚动)
<div id="demo"> <div id="innerdemo"> <div id="demo1"> ...
- C 语言学习的第 05 课:了解课程配套的平台
在此之前,已经同授课老师沟通,确认课程的配套平台是Coding.net.对于大多数(甚至是全部)同学来说,这个平台应该是极其陌生的.不过不用担心,且还是娓娓道来. 定义:Coding.net是一个集代 ...
- centos安装php扩展
我自己的方法 先卸载php 重新安装 再安装扩展 1 rpm -qa|grep php 查看php相关 2 rpm-e 名称 一个个删除,个别需要先删除其他组件才能删除, 3 再用 rpm -qa|g ...
- 关于图像文章垂直无缝连接滚动——JS实现
<!-- 作者:chenyehuacecil@163.com 时间:2015-02-04 描述:实现整篇文章从下到上的无缝连接滚动--><html xmlns="http: ...
- Android Studio2.0 教程MAC版 -快捷键篇
本文转至 Android Studio2.0 教程从入门到精通MAC版 - 提高篇 ( OPEN 开发经验库) 第二篇我们开发了一个Hello World应用,并介绍Android Sutdio的界面 ...