BZOJ1954: Pku3764 The xor-longest Path
题解:
在树上i到j的异或和可以直接转化为i到根的异或和^j到根的异或和。
所以我们把每个点到根的异或和处理出来放到trie里面,再把每个点放进去跑一遍即可。
代码:
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<set> #include<queue> #include<string> #define inf 1000000000 #define maxn 100000+5 #define maxm 4000000+5 #define eps 1e-10 #define ll long long #define pa pair<int,int> #define for0(i,n) for(int i=0;i<=(n);i++) #define for1(i,n) for(int i=1;i<=(n);i++) #define for2(i,x,y) for(int i=(x);i<=(y);i++) #define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define for4(i,x) for(int i=head[x],y;i;i=e[i].next) #define mod 1000000007 using namespace std; inline 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 go,next;ll w;}e[*maxn];
int n,tot,cnt,head[maxn],t[maxm][];
ll a[maxn];
inline void insert(int x,int y,ll z)
{
e[++tot]=(edge){y,head[x],z};head[x]=tot;
e[++tot]=(edge){x,head[y],z};head[y]=tot;
}
inline void dfs(int x,int fa)
{
for4(i,x)if((y=e[i].go)!=fa)
{
a[y]=a[x]^e[i].w;
dfs(y,x);
}
}
inline void add(ll x)
{
int now=;
for3(i,,)
{
int j=x>>i&;
if(!t[now][j])t[now][j]=++cnt;
now=t[now][j];
}
}
inline ll query(ll x)
{
int now=;ll ret=;
for3(i,,)
{
int j=x>>i&^;
if(t[now][j])ret|=(ll)<<i;else j^=;
now=t[now][j];
}
return ret;
} int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); n=read();
for1(i,n-){int x=read(),y=read(),z=read();insert(x,y,z);}
dfs(,);
cnt=;
for1(i,n)add(a[i]);
ll ans=;
for1(i,n)ans=max(ans,query(a[i]));
cout<<ans<<endl; return ; }
1954: Pku3764 The xor-longest Path
Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 383 Solved: 161
[Submit][Status]
Description

给定一棵n个点的带权树,求树上最长的异或和路径
Input
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
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....
Source
BZOJ1954: Pku3764 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 ...
- 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 ...
随机推荐
- CentOS6.5下 yum安装LAMP
CentOS下yum安装LAMP 1. 用yum安装Apache,Mysql,PHP. 1.1安装Apache yum install httpd httpd-devel 安装完成后,用/etc/ ...
- C++ 编写 CorelDRAW CPG 插件例子(2)—ClearFill
这是另一个例子: 贴上主要代码: #include "stdafx.h" #include <tchar.h> #import "libid:95E23C91 ...
- 【cheerio】nodejs的抓取页面模块
http://baike.baidu.com/link?url=8V1CZsEzNE05ujOzISquom_pvFj16sWu1rRb8js11pmd9HNq7ePW_aKfG9oyXj6Txuu5 ...
- 【BZOJ 1103】 [POI2007]大都市meg
Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...
- input输入框的各种样式
输入框景背景透明: <input style="background:transparent;border:1px solid #ffffff"> 鼠标划过输入框,输入 ...
- Extjs4.2布局——Ext.container.ViewportView
先贴出官方文档的关于此布局的描述:“ 一个专门的容器用于可视应用领域(浏览器窗口). Viewport渲染自身到网页的documet body区域, 并自动将自己调整到适合浏览器窗口的大小,在窗口大小 ...
- Educational Codeforces Round 5 A
Problem A:http://codeforces.com/contest/616/problem/A A. Comparing Two Long Integers 果然还是我太天真了(长整数比较 ...
- ural 1160
最小生成树 第一次敲 套用几个函数 其实挺容易的 #include <cstdio> #include <cstring> #include <vector> # ...
- 想让安卓 APP 如丝般顺滑?
随着安卓手机市场占有率的节节攀升,随便在大街上找几个人估计 80% 用的都是安卓手机吧!用安卓手机的人这么多,不知道大家是否曾经感觉到过 APP 卡顿.死机?是否遇到应用程序无响应.闪退?本文就为大家 ...
- HDU1432+几何
题意:给N个点,求最多有多少个点在同一直线上 方法一:求出所有能形成的直线,两两比较,统计最多有多少条重合. #include<stdio.h> #include<stdlib.h& ...