题解:

在树上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

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
1 2 3
2 3 4
2 4 6

Sample Output

7

HINT

The xor-longest path is 1->2->3, which has length 7 (=3 ⊕ 4)
注意:结点下标从1开始到N....

Source

BZOJ1954: Pku3764 The xor-longest Path的更多相关文章

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

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

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

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

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

随机推荐

  1. Nat网络地址转换

    Nat中的术语 -------------------------------------------------------------------------------------------- ...

  2. 转 IHttpModule不起作用

    在 Visual Studio 中,测试 IHttpModule(httpModules) 正常,但是放到服务器上去就不起作用了,这多半得多服务器 IIS 配置入手. 一.看“应用程序池”的“托管管道 ...

  3. 【Nhibernate】HQL 分页

    HQL IQuery query = NHibernateHelper.OpenSession() .CreateQuery( @"from Product"); query.Se ...

  4. windows下编译php5.2.17这是闹哪样?

    参考:http://demon.tw/software/compile-php-on-windows.html

  5. 研读代码必须掌握的Eclipse快捷键

    1. Ctrl+左键 和F3 这个是大多数人经常用到的,用来查看变量.方法.类的定义跳到光标所在标识符的定义代码.当按执行流程阅读时,F3实现了大部分导航动作. 2 Ctrl+Shift+G在工作空间 ...

  6. HDFS(Hadoop Distributed File System )

    HDFS(Hadoop Distributed File System ) HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表 ...

  7. iPhone 7-b

    iPhone 7就要出了!据悉,苹果秋季新品发布会将于9月7日举行,大家来看看iPhone7的概念设计有多逆天. 新机一出,大家最关心的都是价格问题,那就一起看看大家关注的价格问题: 4.7寸的iPh ...

  8. java、js的编码、解码

    如果在地址栏挂载参数,特别是包含中文,往往要进行编码,取值时再解码,以下是java和js中编码.解码的各自方法. java: @Test public void test3() throws Unsu ...

  9. PAT-乙级-1003. 我要通过!(20)

    1003. 我要通过!(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue “答案正确”是自动判题系统给出的最 ...

  10. ORACLE 更新关联多张表

    UPDATE T_XMLC_BILL_HEAD_BAK T1 SET (T1.SENDRECEIVEFLAG, T1.SENDRECEIVEOPERATOR, T1.SENDRECEIVEDATE, ...