bzoj1954 The xor-longest path
Description

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 ...
随机推荐
- Django之前端插件定制之表头
什么是插件? 插件只是辅助,是开发过程中的一个阶段.一般项目一期会用各种插件,迅速将功能.界面搭出来,二期时就改成自己的代码了.大点的公司都有自己的js库,自己开发类似jquery的库. 那接下来就写 ...
- 数据类型的判断 --Object.prototype.toString.call(obj)精准检测对象类型
数据类型的判断 typeof typeof返回一个表示数据类型的字符串,返回结果包括:number.boolean.string.symbol.object.undefined.function等7种 ...
- python3爬虫.3.下载网页图片
目标,豆瓣读书, 下载页面书籍图片. import urllib.request import re #使用正则表达式 def getJpg(date): jpgList = re.findall(r ...
- linux学习记录.1.安装
最近想了想决定开始学习linux. 在百度了一番后开始了安装,虚拟机VirtualBox,ubuntu. 基于VirtualBox虚拟机安装Ubuntu图文教程: http://blog.csdn.n ...
- perl6 Socket
Perl6 中的SOCKET就是相当于Perl5 的 IO::Socket::INET. 官方介绍如下: #下面是客户端multi method new( :$host, :$port, :, :$e ...
- [Leetcode] N-Queens 系列
N-Queens 系列题解 题目来源: N-Queens N-Queens II N-Queens The n-queens puzzle is the problem of placing n qu ...
- Linux内核同步原语之原子操作【转】
转自:http://blog.csdn.net/npy_lp/article/details/7262388 避免对同一数据的并发访问(通常由中断.对称多处理器.内核抢占等引起)称为同步. ——题记 ...
- Tslib触摸屏官网【转】
转自:https://github.com/kergoth/tslib C library for filtering touchscreen events tslib consists of the ...
- 省市区ajax联动
function setCity1(){ var areaId1 = $('#areaId1').val(); var cityId1 = $('#cityId1'); var cityOpt = $ ...
- Java network programming-guessing game
猜数字游戏 游戏的规则如下: 当客户端第一次连接到服务器端时,服务器端生产一个[0,50]之间的随机数字,然后客户端输入数字来猜该数字,每次客户端输入数字以后,发送给服务器端,服务器端判断该客户端发送 ...