bzoj1954 The xor-longest path
Description
给定一棵n个点的带权树,求树上最长的异或和路径Input
Output
Sample Input
1 2 3
2 3 4
2 4 6
Sample Output
HINT
The xor-longest path is 1->2->3, which has length 7 (=3 ⊕ 4)
注意:结点下标从1开始到N....
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define REP(i,k,n) for(int i=k;i<=n;i++)
#define in(a) a=read()
#define MAXN 300010
using namespace std;
inline int read(){
int x=,f=;
char ch=getchar();
for(;!isdigit(ch);ch=getchar())
if(ch=='-')
f=-;
for(;isdigit(ch);ch=getchar())
x=x*+ch-'';
return x*f;
}
queue <int> Q;
int n,cnt=,ans;
int vis[MAXN],sum[MAXN];
int tree[][];
int total=,head[MAXN],nxt[MAXN],to[MAXN],val[MAXN];
inline void adl(int a,int b,int c){
total++;
to[total]=b;
val[total]=c;
nxt[total]=head[a];
head[a]=total;
return ;
}
inline void bfs(){
Q.push();
vis[]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
for(int e=head[u];e;e=nxt[e])
if(!vis[to[e]]){
vis[to[e]]=;
sum[to[e]]=sum[u]^val[e];
Q.push(to[e]);
}
}
return ;
}
inline void insert(int x){
int u=;
for(int i=;i>=;i--){
int t=x&(<<i);
t=(t>>i);
if(!tree[u][t]) tree[u][t]=++cnt;
u=tree[u][t];
}
return ;
}
inline int query(int x){
int u=,tmp=;
for(int i=;i>=;i--){
int t=x&(<<i);
t=(t>>i);
if(tree[u][t^]) u=tree[u][t^],tmp+=(<<i);
else u=tree[u][t];
}
return tmp;
}
int main(){
in(n);
int a,b,c;
REP(i,,n-) in(a),in(b),in(c),adl(a,b,c),adl(b,a,c);
bfs();
REP(i,,n) insert(sum[i]);
REP(i,,n){
ans=max(ans,query(sum[i]));
}
cout<<ans;
return ;
}
bzoj1954 The xor-longest path的更多相关文章
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- 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 ...
- BZOJ1954: Pku3764 The xor-longest Path
题解: 在树上i到j的异或和可以直接转化为i到根的异或和^j到根的异或和. 所以我们把每个点到根的异或和处理出来放到trie里面,再把每个点放进去跑一遍即可. 代码: #include<cstd ...
- 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 ...
随机推荐
- Android SDK的安装与环境变量的配置
配置Andriod环境变量前提是要先安装好JAVA环境 1.下载Android SDK,点击安装,放在任意不含空格.特殊符号和中文的路径即可. 2.默认路径安装后,安装完成,开始配置环境变量. 3.打 ...
- Cesium 初始化Viewer
<pre name="code" class="javascript"><script> var viewer = new Cesium ...
- 74.VS2013和opencv3.1.0安装教程
一.先下载文件 1.VS2013 VS2013有很多版本,专业版,旗舰版,中文英文之类的,所对应的密钥也不一样.我选择的是简体中文专业版.下载链接如下. http://www.musnow.com/t ...
- 老版本ubuntu更新源地址以及sources.list的配置方法 转
转自(http://blog.csdn.net/snaking616/article/details/52966634) 1.国内可用的更新源地址: (1)中科大地址 http://mirrors.u ...
- 新一代的USB 3.0传输规格
通用序列总线(USB) 从1996问世以来,一统个人电脑外部连接界面,且延伸至各式消费性产品,早已成为现代人生活的一部分.2000年发表的USB 2.0 High-speed规格,提供了480Mbps ...
- pip安装模块时:error: command 'gcc' failed with exit status 1
用安装python模块出现error: command 'gcc' failed with exit status 1 问题: gcc编译缺少模块 解决方法: yum install gcc libf ...
- ansible报错Aborting, target uses selinux but python bindings (libselinux-python) aren't installed
报错内容: TASK [activemq : jvm configuration] ********************************************************** ...
- [ python ] 练习作业 - 1
1,写代码,有如下列表,按照要求实现每一个功能 li = ["alex","wusir","eric","rain",& ...
- webservice使用
soap方法 一:PHP本身的SOAP 所有的webservice都包括服务端(server)和客户端(client). 要使用php本身的soap首先要把该拓展安装好并且启用.下面看具体的code ...
- 洛谷 P2043质因子分解 题解
题目传送门 N的范围很小,所以使用O(n2)的算法就能过啦! #include<bits/stdc++.h> using namespace std; ],n; int main(){ c ...