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

For each test case output the xor-length of the xor-longest path.

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的更多相关文章

  1. poj3764 The XOR Longest Path【dfs】【Trie树】

    The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10038   Accepted:  ...

  2. 题解 bzoj1954【Pku3764 The xor – longest Path】

    做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...

  3. 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 ...

  4. 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 ...

  5. FB面经Prepare: Find Longest Path in a Multi-Tree

    给的多叉树, 找这颗树里面最长的路径长度 解法就是在子树里面找最大的两个(或一个,如果只有一个子树的话)高度加起来. 对于每一个treenode, 维护它的最高的高度和第二高的高度,经过该点的最大路径 ...

  6. SP1437 Longest path in a tree(树的直径)

    应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距 ...

  7. Educational DP Contest G - Longest Path (dp,拓扑排序)

    题意:给你一张DAG,求图中的最长路径. 题解:用拓扑排序一个点一个点的拿掉,然后dp记录步数即可. 代码: int n,m; int a,b; vector<int> v[N]; int ...

  8. [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 ...

  9. [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 ...

  10. 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 ...

随机推荐

  1. Android注解支持(Support Annotations)

    注解支持(Support Annotations) Android support library从19.1版本开始引入了一个新的注解库,它包含很多有用的元注解,你能用它们修饰你的代码,帮助你发现bu ...

  2. 使用like时left outer join和inner join的区别

    --select top 10000 * into #s from search set statistics time on set statistics io on select userId,c ...

  3. Obj2002java

    计算球体积 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. Vim程序编辑器

    Vim的三种模式: 1) 一般模式 以 vi 打开一个档案就直接进入一般模式了(这是默认的模式).在这个模式中, 你可以使用『上下左右』按键来移动光标,你可以使用『删除字符』或『删除整行』来处理档案内 ...

  5. Java基础知识强化之IO流笔记22:FileInputStream / FileOutputStream 复制文本文件案例1

    1. 使用字节流FileInputStream / FileOutputStream 复制文本文件案例: 分析: (1)数据源:从哪里来 a.txt   --   读取数据  --  FileInpu ...

  6. RMQ问题与ST算法

    RMQ(Range Minimum/Maximum Query)问题是求区间最值问题. 对于长度为 n 的数组 A,进行若干次查询,对于区间 [L,R] 返回数组A中下标在 [L,R] 中的最小(大) ...

  7. C#中的static静态变量的用法

    静态全局变量 定义:在全局变量前,加上关键字 static 该变量就被定义成为了一个静态全局变量. 特点: A.该变量在全局数据区分配内存. B.初始化:如果不显式初始化,那么将被隐式初始化为0. 静 ...

  8. JQuery判断子Iframe 加载完成的技术解决

    当需要我们给当前页面动态创建Iframe子框架的时候,并且同时需要操作子Iframe里的方法的时候,我们发现无法成功实现.这是为什么呢?经小程总结,发现子Iframe还没有来的及加载完成,就去执行里面 ...

  9. 解决Visual Studio 找不到报表控件、rdlc中文乱码

    找回报表控件 运行安装程序中的 ..\packages\Reporting Services\RVAddon.msi 工具栏,右键选择ReportViewer,注意选择的版本 如果不能编辑报表文件(. ...

  10. (转)C#中的泛型

    来源:http://www.cnblogs.com/JimmyZhang/archive/2008/12/17/1356727.html .Net 1.1版本最受诟病的一个缺陷就是没有提供对泛型的支持 ...