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 ...
随机推荐
- php异步加载、多线程fsockopen()、fputs()
index.php <?php function test() { $fp=fsockopen("localhost", 80, $errno, $errstr, 30); ...
- Spark Streaming揭秘 Day7 再探Job Scheduler
Spark Streaming揭秘 Day7 再探Job Scheduler 今天,我们对Job Scheduler再进一步深入一下,对一些更加细节的源码进行分析. Job Scheduler启动 在 ...
- dom树的介绍,及原理分析
三.解析和DOM树的构建 1.解析: 由于解析渲染引擎是一个非常重要的过程,我们将会一步步的深入,现在让我们来介绍解析. 解析一个文档,意味着把它转换为一个有意义的结构——代码可以了解和使用的东西,解 ...
- shapefile文件
基本信息编辑 ESRI公司的Shapefile文件是描述空间数据的几何和属性特征的非拓扑实体矢量数据结构的一种格式. 内容编辑 一个Shapefile文件最少包括三个文件: 主文件(*.shp).-- ...
- Create a SharePoint Application Page for Anonymous Access
http://dishasharepointworld.blogspot.com/2011/07/how-to-create-sharepoint-application_1072.html http ...
- Nhibernate 多对多级联更新
问题是这样的,有两个表:文章(Article)和分类(Lable),这两者之间的关系是多对多关联,如果你用Nhibernate来保存数据的话非常的好操作,新建Article,然后把Lable值赋值给A ...
- hibernate中session
hibernate中的session是一级缓存,可以理解为进程级的缓存.在进程运行期间一直存在. session可以理解为一个可以操作数据库的对象 具体如何操作数据库? session中有方法, 如果 ...
- PAT-乙级-1038. 统计同成绩学生(20)
1038. 统计同成绩学生(20) 时间限制 250 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求读入N名学生的成绩,将 ...
- uva 10306
有点不同的完全背包问题 但思路还是一样的 /************************************************************************* > ...
- CodeForces 279B Books
http://codeforces.com/problemset/problem/279/B 题意 :Valera 有很多的空闲时间,所以他决定看书,给出n本书,编号1到n,和看每本书需要的时间,他看 ...