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

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 uand 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)

Source

题意:

给一棵带边权的树,想找得到两个点,他们的路径上的权值异或最小。

思路:

首先我们任意找一个作为根,可以用dfs求出其他节点到根的路径的异或,记为xordis

那么对于树上的任意两个节点i, j,i到j的路径的异或和应该是xordis[i] ^ xordis[j]

因为i到j的路径,相当于i到根,根到j,其中重叠的部分,他们的异或值正好是0

因此这道题就变成了找两点异或值最小,https://www.cnblogs.com/wyboooo/p/9824293.html 和这道题就差不多了

最后还需要注意,search找到的最大值是除根以外的,还需要和xordis比较一下,取较大值。

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f int n;
const int maxn = 1e5 + ;
struct edge{
int v, w;
int nxt;
}e[maxn * ];
int head[maxn], tot = ;
int xordis[maxn];
int trie[maxn * + ][], treetot = ; void addedge(int u, int v, int w)
{
e[tot].v = v;
e[tot].w = w;
e[tot].nxt = head[u];
head[u] = tot++;
e[tot].v = u;
e[tot].w = w;
e[tot].nxt = head[v];
head[v] = tot++;
} void dfs(int rt, int fa)
{
for(int i = head[rt]; i != -; i = e[i].nxt){
int v = e[i].v;
if(v == fa)continue;
xordis[v] = xordis[rt] ^ e[i].w;
dfs(v, rt);
}
} void init()
{
memset(head, -, sizeof(head));
tot = ;
memset(xordis, , sizeof(xordis));
memset(trie, , sizeof(trie));
} void insertt(int x)
{
int p = ;
for(int i = ; i >= ; i--){
int ch = x >> i & ;
if(trie[p][ch] == ){
trie[p][ch] = ++tot;
}
p = trie[p][ch];
}
} int searchh(int x)
{
int p = , ans = ;
for(int i = ; i >= ; i--){
int ch = x >> i & ;
if(trie[p][ch ^ ]){
p = trie[p][ch ^ ];
ans |= << i;
}
else{
p = trie[p][ch];
}
}
return ans;
} int main()
{
while(scanf("%d", &n) != EOF){
init();
for(int i = ; i < n - ; i++){
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
}
dfs(, -); /*for(int i = 0; i < n; i++){
printf("%d\n", xordis[i]);
}*/ int ans = ;
for(int i = ; i < n; i++){
insertt(xordis[i]);
//cout<<searchh(xordis[i])<<endl;
ans = max(ans, searchh(xordis[i]));
ans = max(ans, xordis[i]);
}
printf("%d\n", ans);
}
return ;
}

最新文章

  1. j疑难杂症:java.lang.VerifyError: class org.hibernate.type.WrappedMaterializedBlobType overrides final method getReturnedClass.()Ljava/lang/Class;
  2. http概述
  3. &lt;转&gt;struts2中Convention中的basePackage与locators配置种种
  4. 【Android UI设计与开发】之具体解释ActionBar的使用
  5. jQuery 标签淡入淡出 个人随笔
  6. 搭建maven项目简介
  7. 如何学习.Net的步骤
  8. JS进阶书籍
  9. Android开发之音乐播放器
  10. js处理json js递归
  11. Python学习(九) —— 正则表达式与re模块
  12. Python学习笔记第二十四周(JavaScript补充)
  13. PHPActiveRecord 学习一
  14. 在timer的时候突然改变影片简介,先前的不暂停
  15. Future接口和Callable接口以及FeatureTask详解
  16. Python 在字符串中处理html 和xml
  17. Java中的基本数据类型及其封装类
  18. 图像处理标准图像lena的故事图The Lenna Story behind image processing
  19. CSS学习笔记(12)--Flex 布局教程:实例篇
  20. R语言平均值和加权平均值

热门文章

  1. 第二百八十七节,MySQL数据库-条件语句、循环语句、动态执行SQL语句
  2. C++ 判断
  3. css -- 背景图片自适应屏幕大小
  4. 查找——图文翔解HashTree(哈希树)
  5. 实现QQ第三方登录教程(php)
  6. jQuery页面刷新(局部、全部)问题分析
  7. 帝国CMS 列表模板list.var支持程序代码
  8. Sublime Text2安装Package Control
  9. Java精选笔记_文件上传与下载
  10. 第十五篇:关于TCP通信程序中数据的传递格式