题目链接

求树上的一条最长异或路径。

定义f(u, v)为u到v的路径, 那么显然f(1, u)^f(1, v) = f(u, v), 想不到这个就没有办法做。

然后就可以用字典树查询+插入了。

用指针版本的狂T不止。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5+;
int head[maxn*], num, val[maxn], top = , cnt;
struct node
{
int to, nextt, w;
}e[maxn*];
void add(int u, int v, int w) {
e[num].to = v, e[num].w = w, e[num].nextt = head[u], head[u] = num++;
}
void init() {
num = cnt = ;
mem(val);
mem1(head);
}
void dfs(int u, int fa) {
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
val[v] = val[u]^e[i].w;
dfs(v, u);
}
}
struct Trie
{
int next[];
void init() {
next[] = next[] = ;
}
}trie[maxn];
void insert(int pre) {
int p = ;
for(int i = top-; i>=; i--) {
int tmp = pre>>i&;
if(!trie[p].next[tmp]) {
trie[p].next[tmp] = ++cnt;
trie[cnt].init();
}
p = trie[p].next[tmp];
}
}
int query(int pre) {
int p = , ret = ;
for(int i = top-; i>=; i--) {
int tmp = pre>>i&;
if(trie[p].next[tmp^]) {
ret |= <<i;
tmp ^= ;
}
p = trie[p].next[tmp];
}
return ret;
}
int main()
{
int n, x, y, z;
while(~scanf("%d", &n)) {
init();
for(int i = ; i<n-; i++) {
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
add(y, x, z);
}
val[] = ;
dfs(, -);
int ans = ;
trie[].init();
for(int i = ; i<n; i++) {
insert(val[i]);
ans = max(ans, query(val[i]));
}
printf("%d\n", ans);
}
return ;
}

poj 3764 The xor-longest Path Trie的更多相关文章

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

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

  2. 【POJ 3764】 The xor-longest path

    [题目链接] http://poj.org/problem?id=3764 [算法] 首先,我们用Si表示从节点i到根的路径边权异或和 那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就 ...

  3. 【POJ 3764】The Xor-longest Path

    题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...

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

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

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

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

  6. poj 3764 The xor-longest Path(字典树)

    题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...

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

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

  9. Poj 3764 The xor-longest Path(Trie树+xor+贪心)

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

  10. poj 3764 The xor-longest Path (01 Trie)

    链接:http://poj.org/problem?id=3764 题面: The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K ...

随机推荐

  1. express中路由设置的坑-----1

    router.get('/commodities/sortable', utils.logged, function (req, res) { Commodity.find({force_top:tr ...

  2. Delphi XE7中新并行库

    Delphi XE7中添加了新的并行库,和.NET的Task和Parellel相似度99%. 详细内容能够看以下的文章: http://www.delphifeeds.com/go/s/119574 ...

  3. 什么是xss盲打

    什么是xss盲打? 盲打仅仅是一种惯称的说法,就是不知道后台不知道有没有xss存在的情况下,不顾一切的输入xss代码在留言啊,feedback啊之类的地方,尽可能多的尝试xss的语句与语句的存在方式, ...

  4. collectionView 中cell间距设置建议

    应该是调节UICollectionViewFlowLayout的minimumInteritemSpacing属性,这个是调节同一行的cell之间的距离的. 使用-(CGFloat )collecti ...

  5. POJ 3356.AGTC

    问题简述: 输入两个序列x和y,分别执行下列三个步骤,将序列x转化为y (1)插入:(2)删除:(3)替换: 要求输出最小操作数. 原题链接:http://poj.org/problem?id=335 ...

  6. 我用过的Linux命令之chmod

    chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法. Linux系统中的每 ...

  7. mina、netty消息边界问题(采用换行符)

    在TCP连接开始到结束连接,之间可能会多次传输数据,也就是服务器和客户端之间可能会在连接过程中互相传输多条消息.理想状况是一方每发送一条消息,另一方就立即接收到一条,也就是一次write对应一次rea ...

  8. Linux编程环境介绍(3) -- linux下的c/c++程序开发

    目录: 1. 编辑器( Vi ) [vi 与 vim] vi(visual interface)是linux系统最重要的文本编辑器, 所有的 Unix-Like 系统都会内置vi文本编辑器.  vim ...

  9. Android ActionBar详解(三)--->ActionBar的Home导航功能

    FirstActivity如下: package cc.testsimpleactionbar2; import android.os.Bundle; import android.app.Activ ...

  10. FORM表单不刷新提交POST数据

    很多时候表单太多项,JQ懒的去处理了 使用这个提交吧.和她讨论出去旅游,去哪里好呢,此时还和以前一样吗? function testaction(){ var f = $("#publish ...