poj3764 The XOR Longest Path【dfs】【Trie树】
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
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 ;
}
poj3764 The XOR Longest Path【dfs】【Trie树】的更多相关文章
- poj3764(dfs+Trie树+贪心)
题目链接:http://poj.org/problem?id=3764 分析:好题!武森09年的论文中有道题CowXor,求的是线性结构上的,连续序列的异或最大值,用的办法是先预处理出前n项的异或值, ...
- BZOJ5338 [TJOI2018] Xor 【可持久化Trie树】【dfs序】
题目分析: 很无聊的一道题目.首先区间内单点对应异或值的询问容易想到trie树.由于题目在树上进行,case1将路径分成两段,然后dfs的时候顺便可持久化trie树做询问.case2维护dfs序,对d ...
- POJ 3764 - The xor-longest Path - [DFS+字典树变形]
题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...
- POJ 3764 DFS+trie树
题意: 给你一棵树,求树中最长的xor路径.(n<=100000) 思路: 首先我们知道 A xor B =(A xor C) xor (B xor C) 我们可以随便选一个点DFS 顺便做出与 ...
- 51nod 1295 XOR key (可持久化Trie树)
1295 XOR key 题目来源: HackerRank 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查 ...
- 51nod 1295 XOR key | 可持久化Trie树
51nod 1295 XOR key 这也是很久以前就想做的一道板子题了--学了一点可持久化之后我终于会做这道题了! 给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X ...
- CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&指针&Xor)
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...
- The XOR Largest Pair (trie树)
题目描述 在给定的 NN 个整数 A_1,A_2,--,A_NA1,A2,--,AN 中选出两个进行xor运算,得到的结果最大是多少?xor表示二进制的异或(^)运算符号. 输入格式 第一行输入 ...
- POJ3764 The xor-longest Path(Trie树)
题目给一棵有边权的树,问树上任意两点路径上的边异或值最多是多少. 记录每个点u到根路径的异或值xor[u],那么任意两点u.v路径的异或值就是xor[u]^xor[v]. 于是这个问题就变成了从n个数 ...
随机推荐
- 查询_修改SQL Server 2005中数据库文件存放路径
1.查看当前的存放路径: select database_id,name,physical_name AS CurrentLocation,state_desc,size from sys.maste ...
- ChemDraw教程:如何查看和删除俗名
化学范畴里,允许用俗名表示ChemDraw原子标记或原子标记的一部分,可以定义俗名的快捷键也可以自由查看或删除俗名,熟练掌握可以提高ChemDraw软件使用效率,下面将具体介绍此部分内容. 一.查看俗 ...
- 帝国留言板管理员回复发送EMAIL通知客户
说明:修改1:e/admin/tool/ReGook.php /*回复表单*/ 43行处添加代码 ------------------------------------------------- ...
- LeetCode——Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- php-fpm打开错误日志的配置
nginx与apache不一样,在apache中可以直接指定php的错误日志,那样在php执行中的错误信息就直接输入到php的错误日志中,可以方便查询. 在nginx中事情就变成了这样:nginx只对 ...
- js 对象引用传值
1)当变量是一个对象(例如:{}或[]),使用 a = b = {} 这种形式传值的话,会变成会用传值,修改 a 时 b 会相应变化,修改 b 时 a 也一样会相应变化 var a = b = {}; ...
- 什么是LTE?
LTE是英文Long Term Evolution的缩写.LTE也被通俗的称为3.9G,具有100Mbps的数据下载能力,被视作从3G向4G演进的主流技术.它改进并增强了3G的空中接入技术,采用OFD ...
- MongoDB(一)-- 简介、安装、CRUD
一.Mongodb简介 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供可 ...
- Zookeeper(二)-- 客户端操作命令
一.前提 开启zookeeper服务端,用客户端连接.输入help,查看可使用命令,如下图所示: 操作无非就是增删改查等. 二.增加 格式:create [-s] [-e] path data acl ...
- Win 10安装Python及环境变量配置
一.Windows系统 很多童鞋问之前的教程怎么没有介绍安装python3.5的,现予以补充更新一下. (一)安装python3.5 1.下载 进入Python官网www.python.org,在“D ...