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个数 ...
随机推荐
- 74hc165三片级联
3片74HC165进行级联,用于扩展IO口,读取外界设备的数据. unsigned int read_74165(void) { unsigned ; unsigned ; //三片74hc165,需 ...
- Extracting and composing robust features with denosing autoencoders 论文
这是一篇发表于2008年初的论文. 文章主要讲了利用 denosing autoencoder来学习 robust的中间特征..进上步,说明,利用这个方法,可以初始化神经网络的权值..这就相当于一种非 ...
- ceRNA 调控机制
ceRNA 不同于mRNA, lncRNA, ncRNA 等概念,其指的既不是某一种类型的RNA(比如mRNA, lncRNA), 也不是某一类的RNA(如ncRNA); ceRNA 其实指的是不同种 ...
- 【Java面试题】21 Java中的异常处理机制的简单原理和应用。
异常指Java程序运行时(非编译)所发生的非正常情况或错误. java对异常进行了分类,不同类型的异常使用了不同的java类,所有异常的根类为java.lang.Throwable.Throwable ...
- Tomcat源码学习
Tomcat源码学习(一) 转自:http://carllgc.blog.ccidnet.com/blog-htm-do-showone-uid-4092-type-blog-itemid-26309 ...
- 记录下一个自己不常用的关键字-yield
yield 这个关键字 一直很少用,也不知道具体用途.按照习惯就查询了下MSDN. 意思大致是这样的:在迭代器块中用于向枚举数对象提供值或发出迭代结束信号 表现形式:1. yield return & ...
- linux下 安装mysql教程
安装环境:系统是 centos6.5 1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:我这里选择的5.6. ...
- 连接oracle服务器超慢--原因分析
连接oracle服务器超慢:有如下原因可能会影响. 网络不好:oracle服务器跟本地网络不好. oracle服务器内存不足:导致反应超慢 监听日志listener.log太大:导致响应超慢. 所以对 ...
- windows cmd命令大全/cmd命令提示符大全
刚接触电脑的时候是从DOS系统开始,DOS时代根本就没有Windows这样的视窗操作界面,只有一个黑漆漆的窗口,让你输入命令.所以学DOS系统操作,cmd命令提示符是不可或缺的.可以告诉大家,大多数的 ...
- 关于KEIL仿真的虚拟串口讲解
这个是最后的效果图,右下方是串口打印的设置 第一步:在程序上写入关于串口一的配置,以及初始化和串口输出的内容 第二步:需要的时候在进行配置,在OPTIONS OF TARGET一栏的c/c++中(其原 ...