B1954 The xor-longest Path
给定一颗带权值的,节点数为n的树,求树上路径最大异或和。
solution: 先dfs将所有点到根的异或和算出来。然后放进tire树中贪心。
#include<cstdio>
#include<iostream>
using namespace std;
const int manx=100010;
int n;
struct node
{
int point;
int nxt;
int val;
};
node line[manx<<1];
int head[manx],tail;
int dis[manx];
int t[manx<<5][2],end;
void add(int a,int b,int c)
{
line[++tail].point=b;
line[tail].val=c;
line[tail].nxt=head[a];
head[a]=tail;
}
void dfs(int now,int f,int sum)
{
dis[now]=sum;
for(int i=head[now];i;i=line[i].nxt)
if(f!=line[i].point)
dfs(line[i].point,now,sum^line[i].val);
return ;
}
void ins(int val)
{
int now=0;
for(int i=(1<<30);i;i>>=1)
{
int nxt= (i&val) ? 1 : 0;
if(!t[now][nxt]) t[now][nxt]=++end;
now=t[now][nxt];
}
return ;
}
int find(int val)
{
int res=0,now=0;
for(int i=(1<<30);i;i>>=1)
{
int nxt=(i&val) ? 0 : 1;
if(t[now][nxt]) now=t[now][nxt],res+=i;
else now=t[now][nxt^1];
}
return res;
}
int main()
{
scanf("%d",&n);
int a,b,c;
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c),add(b,a,c);
}
dfs(1,0,0);
for(int i=1;i<=n;i++)
ins(dis[i]);
int ans=0;
for(int i=1;i<=n;i++)
ans=max(ans,find(dis[i]));
printf("%d",ans);
}
B1954 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 ...
- 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 ...
- [LeetCode] Longest Univalue Path 最长相同值路径
Given a binary tree, find the length of the longest path where each node in the path has the same va ...
- [Swift]LeetCode687. 最长同值路径 | Longest Univalue Path
Given a binary tree, find the length of the longest path where each node in the path has the same va ...
- Recursion-687. Longest Univalue Path
Given a binary tree, find the length of the longest path where each node in the path has the same va ...
随机推荐
- vue-cli中webpack配置解析
版本号 vue-cli 2.8.1 (终端通过vue -V 可查看) vue 2.2.2 webpack 2.2.1 目录结构 ├── README.md ├── build │ ├── build. ...
- edis Cluster实现原理
一.Redis Cluster主要特性和设计 集群目标 1)高性能和线性扩展,最大可以支撑到1000个节点:Cluster架构中无Proxy层,Master与slave之间使用异步replic ...
- javascript判断input框只能输入数字的方法
javascript 只允许输入数字有很多方法,总结如下 1,只允许输入数字和小数点. <input onKeypress="return (/[\d.]/.test(String.f ...
- (转)blkid命令 获取文件系统类型、UUID
blkid命令 获取文件系统类型.UUID 原文:http://www.cnblogs.com/dkblog/archive/2011/08/30/2159630.html 在Linux下可以使用b ...
- 使用CSS选择器进行元素定位
在selenium webdriver中,支持使用CSS选择器来进行元素定位,事实在真的投入工作,大量编辑用例和元素定位的时候,使用css 和 xpath才是经常需要用到的. 之前有专门讲过使用xpa ...
- java.lang.IllegalArgumentException: Page directive: invalid value for import 问题处理
1.问题说明: 项目原来用的tomcat版本是apache-tomcat-6.0,后来为了安全原因将版本升至apache-tomcat-7.0,发现有的jsp页面出现下面的异常: java.lang. ...
- CF 540D——Bad Luck Island——————【概率dp】
Bad Luck Island time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 批量删除QQ空间说说
第一步:用电脑打开浏览器登录你的QQ空间 第二步:点击你的说说栏目 第三步:按下电脑的F12键或者点击右上角的菜单一栏,点击开发者工具 第四步:看到右半边屏幕,找到一个叫Console的菜单,并且点击 ...
- 详解HTML中的表单元素
代码详讲: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- Unity Shader 阴影
最近在看Unity shader开发实战详解,刚开始看阴影部分,稍微有了点思路.在这里写点笔记,算是小结吧. .阴影实现方法 一种是通过模拟光照的原理,用向量的方法 找到被光线照射的点 映射到平面的位 ...