Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5646   Accepted: 1226

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)

题意:给出一颗n个节点的边权树,求一条路径(u,v),使得路径上的边的权值异或值最大

思路:我们可以先0为根,求出其他节点i到根的路径的边权异或值d[i],对于u,v之间路径的边权异或结果就是d[u]^d[v], 那么问题转化为给出n个数,求任意两个异或的最大值

把每一个数以二进制形式从高位到低位插入trie中,然后依次枚举每个数,在trie中贪心,即当前为0则向1走,为1则向0走。

一开始写动态分配节点的trie一直tle。。。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int N = ; int n;
struct Edge {
int v, w, nex;
Edge() {}
Edge(int v, int w, int nex) : v(v), w(w), nex(nex) {}
};
Edge e[N << ];
int head[N], tot;
void add(int u, int v, int w) {
e[tot] = Edge(v, w, head[u]);
head[u] = tot++;
}
void read() {
memset(head, -, sizeof head);
tot = ;
int u, v, w;
for(int i = ; i < n; ++i) {
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
}
int d[N], vis[N];
void bfs() {
queue<int> que;
que.push();
d[] = ;
memset(vis, , sizeof vis);
int u, v, w;
vis[] = ;
while(!que.empty()) {
u = que.front(); que.pop();
for(int i = head[u]; ~i; i = e[i].nex) {
v = e[i].v; w = e[i].w;
if(vis[v]) continue;
d[v] = d[u] ^ w;
vis[v] = ;
que.push(v);
}
}
}
int ch[N * ][];
struct Trie {
int sz;
Trie() { sz = ; memset(ch[], , sizeof ch[]); }
void _insert(int bs[]) {
int u = ;
for(int i = ; i >= ; --i) {
int c = bs[i];
if(!ch[u][c]) {
memset(ch[sz], , sizeof ch[sz]);
ch[u][c] = sz++;
}
u = ch[u][c];
}
}
int _search(int bs[]) {
int u = , ans = ;
for(int i = ; i >= ; --i) {
int c = bs[i];
if(c == ) {
if(ch[u][]) { ans += ( << (i)); u = ch[u][]; }
else u = ch[u][];
}else {
if(ch[u][]) { ans += ( << (i)); u = ch[u][]; }
else u = ch[u][];
}
}
return ans;
}
}; int ans;
int b[];
void get(int x) {
int ls = ;
memset(b, , sizeof b);
while(x) {
b[ls++] = x % ;
x >>= ;
}
}
void solve() {
Trie mytrie;
ans = ;
for(int i = ; i < n; ++i) {
get(d[i]);
mytrie._insert(b);
ans = max(ans, mytrie._search(b));
}
printf("%d\n", ans);
}
int main() {
// freopen("in.txt", "r", stdin);
while(~scanf("%d", &n)) {
read();
bfs();
solve();
}
}

Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值的更多相关文章

  1. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

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

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

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

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

  4. 51Nod - 1295:XOR key (可持久化Trie求区间最大异或)

    给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R).求ALL 至 ARR 这R - L + 1个数中,与X 进行异或运算(Xor),得到的最大值 ...

  5. poj 1004:Financial Management(水题,求平均数)

    Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 126087   Accepted: ...

  6. SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax

    275. To xor or not to xor   The sequence of non-negative integers A1, A2, ..., AN is given. You are ...

  7. POJ 1149:PIGS 网络流经典题

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18345   Accepted: 8354 Description ...

  8. hdu 5265 技巧题 O(nlogn)求n个数中两数相加取模的最大值

    pog loves szh II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)

    题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...

随机推荐

  1. HTC vive开发:关于手柄按键

    一.关于左右手柄的对应关系 两个手柄和SteamVR_TrackedObject.EIndex是对应的,一个是EIndex.Device2,另一个是EIndex.Device3(有编号的那个) 在场景 ...

  2. 阿里提前批校招内推offer经历

    经过一个半月的阿里内推面试,今天终于收到了阿里的offer邮件 .阿里的内推面试一共有四轮,本人是7月19号投的内推邮件,8月28号收到了offer的邮件.首先本人谈谈内推的看法.内推是公司招聘人才的 ...

  3. PHP 站点相对包含,路径的问题解决方法(include,require)

    以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...

  4. Python中函数、类、模块和包的调用

    初学python阶段,大多数人对函数.类.模块和包的调用都搞得不是很清楚,这篇随笔就简单的进行说明. (1)函数 当函数定义好之后,可以直接调用. 比如:def summ(add1,add2),那么 ...

  5. using 声明与编译指令

    using std::cout; // using 声明 using namespace std; // using 编译指令,导入std里面的所有名称 一般使用using 声明,它只会导入指定的名称 ...

  6. Android实现双击事件的两种方式

    Work around的方法是先监听onTouch事件来监听连续点击次数,每次点击都布置一个间隔时间的延时任务,延时任务执行时判断间隔内是否还有点击,如果没有则发布点击次数,重置计数. 实现代码如下: ...

  7. HTTP Status 500 - The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application 错误

    解决方法链接:http://stackoverflow.com/questions/17709076/http-status-500-the-absolute-uri-http-java-sun-co ...

  8. MySql 里的IFNULL、NULLIF和ISNULL用法

    MySql 里的IFNULL.NULLIF和ISNULL用法 mysql中isnull,ifnull,nullif的用法如下: isnull(expr) 的用法: 如expr 为null,那么isnu ...

  9. centos 6.5 安装django

     首先做这个:python安装setuptools   http://blog.csdn.net/zhuying_linux/article/details/8167430  CentOS下将2.6升 ...

  10. yii小细节

    1.main.php增加导航栏严格区分大小写,否则会出现404错误 2.增加'分页'功能---前后台的models里面的search.php 添加 public function search($pa ...