The xor-longest Path
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 3875 | Accepted: 850 |
Description
In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p:
⊕ is the xor operator.
We say a path the xor-longest path if it has the largest xor-length. Given an edge-weighted tree with n nodes, can you find the xor-longest path?
Input
The 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
4
0 1 3
1 2 4
1 3 6
Sample Output
7
Hint
The xor-longest path is 0->1->2, which has length 7 (=3 ⊕ 4)
這道題由於沒看到多組數據,WA了很久,題目比較簡單,但是運用了a xor b= a xor c xor b xor c,這應該是解異或題目常用的技巧。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#define MAXN 110000
#define MAXV MAXN
#define MAXE MAXV*2
#define MAXT MAXN*200
//AC
typedef long long qword; int n,m;
struct trie
{
int ch[];
}tree[MAXT];
int toptr=;
inline void new_node(int &now)
{
now=++toptr;
tree[now].ch[]=tree[now].ch[]=;
}
int dbg=;
void build_trie(qword x)
{
dbg++;
int now=;
int i;
for (i=;i>=;i--)
{
if (!tree[now].ch[(x&(1ll<<i))!=])
{
new_node(tree[now].ch[(x&(1ll<<i))!=]);
}
now=tree[now].ch[(x&(1ll<<i))!=];
}
if (dbg==)
dbg--;
} struct Edge
{
int np;
qword val;
Edge *next;
}E[MAXE],*V[MAXV];
int tope=-;
void addedge(int x,int y,qword z)
{
E[++tope].np=y;
E[tope].val=z;
E[tope].next=V[x];
V[x]=&E[tope];
}
int fa[MAXN],depth[MAXN],val[MAXN];
void dfs(int now,int v)
{
Edge *ne;
build_trie(v);
val[now]=v;
for (ne=V[now];ne;ne=ne->next)
{
if (ne->np==fa[now])continue;
fa[ne->np]=now;
dfs(ne->np,v^ne->val);
}
}
qword find(qword x)
{
int now=;
int ret=;
int i;
for (i=;i>=;i--)
{
if (tree[now].ch[(x&(1ll<<i))==])
{
now=tree[now].ch[(x&(1ll<<i))==];
ret+=1ll<<i;
}else
{
now=tree[now].ch[(x&(1ll<<i))!=];
}
}
if (!now)throw ;
return ret;
} int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int i,j,k;
qword x,y,z;
while (~scanf("%d",&n))
{
memset(V,,sizeof(V));
memset(fa,-,sizeof(fa));
toptr=;
tope=-;
tree[].ch[]=tree[].ch[]=;
for (i=;i<n;i++)
{
scanf(LL LL LL,&x,&y,&z);
addedge(x,y,z);
addedge(y,x,z);
}
fa[]=;
dfs(,);
qword ans=;
for (i=;i<n;i++)
{
ans=max(ans,find(val[i]));
}
printf(LL "\n",ans);
}
return ;
}
The xor-longest Path的更多相关文章
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- 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 ...
- Recursion-687. 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开发具体解释之ListView具体解释一
列表ListView介绍和实例 1.ListView -- ListActivity -- ListAdapter 2.ArrayAdapter结合ListView进行显示 3.SimpleA ...
- 条带深度 队列深度 NCQ IOPS
http://blog.csdn.net/striping/article/details/17449653 IOPS 即I/O per second,即每秒进行读写(I/O)操作的次数,多用于数据库 ...
- Linux系统调优1
Linux在进行系统调优的时候,首先要考虑整个操作系统的结构,然后针对各个部分进行优化,下面展示一个Linux系统的各个组成部分: 有上图可以看出,我们可以调整的有应用程序,库文件,内核,驱动,还有硬 ...
- careercup-数组和字符串1.5
1.5 利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能.比如,字符串”aabcccccaaa“会变成”a2b1c5a3“.若”压缩“后的字符串没有变短,则返回原先的字符串. 类似 le ...
- rpm安装mysql 默认安装目录
MySQL安装完成后不象SQL Server默认安装在一个目录,它的数据库文件.配置文件和命令文件分别在不同的目录,了解这些目录非常重要,尤其对于Linux的初学者,因为Linux本身的目录结构就比较 ...
- Unity monodev环境搭建
断点调试功能可谓是程序员必备的功能了.Unity3D支持编写js和c#脚本,但很多人可能不知道,其实Unity3D也能对程序进行断点调试的.不过这个断点调试功能只限于使用Unity3D自带的MonoD ...
- IOS Remote Notification
1. 本地证书合成 rm *.pem echo "export cert..." openssl pkcs12 -clcerts -nokeys -out push_cert.pe ...
- as3 打开窗口类
package FlashCode.utils{ import flash.display.Sprite; import flash.net.URLRequest; import flash.net. ...
- index页面数据展示为设定的命名
数据库表里面字段的值想用另一种命名形式展示,如1是 是,2是 否 解决方法: 用到formatter ,{field: 'params', title: '参数', width: 100, sort ...
- [XML] C# XmlHelper操作Xml文档的帮助类 (转载)
点击下载 XmlHelper.rar 主要功能如下所示 /// <summary> /// 类说明:XmlHelper /// 编 码 人:苏飞 /// 联系方式:361983679 // ...