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 ...
随机推荐
- 初识smarty
个人体会(不完全正确):就是smarty就是为了更好的使得php/html结合做出来的一个框架. ,
- iTextSharp操作pdf之pdfCreater
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- SICP第三章题解
目录 SICP第三章题解 ex3-17 ex3-18 ex3-19 队列 ex3-21 ex3-22 ex3-24 ex3-25 3.4 并发:时间是一个本质问题 ex3-38 3.4.2 控制并发的 ...
- 前端代码编辑器ace 语法提示 代码提示
本文主要是介绍ace编辑器的语法提示,自动完成.其实没什么可特别介绍的,有始有终吧,把项目中使用到的ace的功能都介绍下. { enableBasicAutocompletion: false, // ...
- python开发学习-day09(队列、多路IO阻塞、堡垒机模块、mysql操作模块)
s12-20160312-day09 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- ADO.NET Connection Pooling at a Glance
ADO.NET Connection Pooling at a Glance Establishing a connection with a database server is a hefty a ...
- 在Hboxlayout中组件的显示比例
有两种方法: 在插入布局的时候设置 mainLayout->addWidget(list,1,Qt::AlignCenter); mainLayout->addWidget(stack,3 ...
- Python全栈开发之8、装饰器详解
一文让你彻底明白Python装饰器原理,从此面试工作再也不怕了.转载请注明出处http://www.cnblogs.com/Wxtrkbc/p/5486253.html 一.装饰器 装饰器可以使函数执 ...
- 分页查询和redis
问题 我在做论坛的是时候遇到了如下的问题.论坛里可以有很多的主题topic,每个topic对应到很多回复reply.现在要查询某个topic下按照replyTime升序排列的第pageNo页的repl ...
- Echarts怎么用后台传来的json数据
Echarts怎么用后台传来的json数据 <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...