poj 3764 The xor-longest Path Trie
求树上的一条最长异或路径。
定义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的更多相关文章
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- 【POJ 3764】 The xor-longest path
[题目链接] http://poj.org/problem?id=3764 [算法] 首先,我们用Si表示从节点i到根的路径边权异或和 那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就 ...
- 【POJ 3764】The Xor-longest Path
题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)
题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...
- poj 3764 The xor-longest Path(字典树)
题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...
- 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 ...
- 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 ...
- Poj 3764 The xor-longest Path(Trie树+xor+贪心)
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6455 Accepted: 1392 ...
- poj 3764 The xor-longest Path (01 Trie)
链接:http://poj.org/problem?id=3764 题面: The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K ...
随机推荐
- mac电脑批量解压android apk文件图形化工具--apkDecode
mac电脑apk文件解压软件,简单的用图形界面将apktools包装了下,使用起来非常简单,可以将apk文件批量解压缩,方便大家查看一些东东,仅供学习目的. 使用步骤如下: 1 下载apkDecode ...
- 如何判断是否按下Ctrl键 - C#
可根据Control.ModifierKeys来判断用户是否按下了组合键. if ((Control.ModifierKeys & Keys.Control) == Keys.Control) ...
- 用js获取周、月第一天和最后一天(转载)
var getCurrentWeek = function (day) { var days = ["周日", "周一", "周二", &q ...
- 修改UISearchBar背景颜色
UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性.方法一:是直接将 UISearc ...
- C++_基础_类和对象2
内容: (1)构造函数 (2)初始化列表及其必要性 (3)支持自定义类型转换的构造函数 (4)this指针 (5)const对象和成员函数 (6)析构函数 1.构造函数1.1 格式: class 类名 ...
- Convert Sorted List to Balanced Binary Search Tree (BST)
(http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list ...
- SGU 149. Computer Network( 树形dp )
题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, d ...
- mysql 的not null 与 null的区别(转,恍然大悟)
相信很多用了mysql很久的人,对这两个字段属性的概念还不是很清楚,一般会有以下疑问: 1.我字段类型是not null,为什么我可以插入空值 2.为毛not null的效率比null高 3.判断字段 ...
- js基础参数获取
1 获取浏览器中url中的参数,会自动把问号"?"去掉 function getParamsFromHref() { //调试用 var wyl_ = window.locatio ...
- 基于jQuery带标题的图片3D切换焦点图
今天给大家分享一款基于jQuery带标题的图片3D切换焦点图.这款焦点图适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. 实现的代码. htm ...