题目链接

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

定义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. 使用session插件并且实现登录验证

    var express = require('express'); var cookieParser = require('cookie-parser'); var bodyParser = requ ...

  2. 【转】python import的用法

    [转自http://blog.sina.com.cn/s/blog_4b5039210100ennq.html] 在python用import或者from...import来导入相应的模块.模块其实就 ...

  3. sass的cd进入目录

    今天在弄sass安装进入项目文件命令 直接f:

  4. poj 3680 Intervals(费用流)

    http://poj.org/problem?id=3680 巧妙的构图. 题目:给定N个区间(ai,bi)权值wi,求最大权和且每个点最多覆盖K次. 构图:将区间端点离散化,将第i个点连第i+1个点 ...

  5. 【leetcode边做边学】二分查找应用

    很多其它请关注我的HEXO博客:http://jasonding1354.github.io/ 简书主页:http://www.jianshu.com/users/2bd9b48f6ea8/lates ...

  6. Android解析XML

    在Android平台上可以使用Simple API for XML(SAX) . Document Object Model(DOM)和Android附带的pull解析器解析XML文件. 下面是本例子 ...

  7. JavaScript之数组学习

    在JavaScript中,数组用关键字Array来声明.声明数组的同时还可以指定数组初始元素的大小,也就是数组的长度;下面代码定义了一个数组长度为6的数组; var beatles=Array(6); ...

  8. SQL Server 使用ROW_NUMBER()进行分页

    代码示例: WITH domain AS(SELECT ROW_NUMBER() OVER(ORDER BY ID DESC) ids,* FROM dbo.DomainInfo) SELECT * ...

  9. 块对象block小结(2)

    MRC环境下 ////  main.m//  blcok////  Created by ios on 16/4/6.//  Copyright © 2016年 ios. All rights res ...

  10. 如何在一个工作站里创建多个工程(同一个xcode下面创建多个工程)

    第一步,理解: 怎么会有一个xcode下面创建两个工程这一说呢,一个工程代表一个项目,意思就是有两个项目了.错.其实在一个窗口下面并不是两个工程,而是一个workspace 即一个工作站.在工作站里面 ...