poj 3764 字典树
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7332 | Accepted: 1555 |
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 u and 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
题意:
给出一棵树,求这棵树中的最大的异或路径。
代码:
//预处理出来每个点到根的异或值sxor,然后u,v之间的异或路径值就是sxor[u]^sxor[v],lca就消去了。然后把每个点的sxor值插入字典
//树(二进制字典树),枚举每个点贪心的找他的最大异或路径。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int head[MAXN*+],tot,sxor[MAXN*+],sz,nod[MAXN*+][],ans;
struct Edge
{
int to,w,next;
}edge[MAXN*+];
void init()
{
memset(sxor,,sizeof(sxor));
memset(head,-,sizeof(head));
nod[][]=nod[][]=;
tot=;
sz=;ans=;
}
void add(int x,int y,int z)
{
edge[tot].to=y;
edge[tot].w=z;
edge[tot].next=head[x];
head[x]=tot++;
edge[tot].to=x;
edge[tot].w=z;
edge[tot].next=head[y];
head[y]=tot++;
}
void insert(int x)
{
int rt=;
for(int i=;i>=;i--){
int id=(x>>i)&;
if(nod[rt][id]==){
nod[rt][id]=++sz;
nod[sz][]=nod[sz][]=;
}
rt=nod[rt][id];
}
}
void dfs(int x,int fa,int sum)
{
for(int i=head[x];i!=-;i=edge[i].next){
int y=edge[i].to;
if(y==fa) continue;
sxor[y]=(sum^edge[i].w);
dfs(y,x,sum^edge[i].w);
}
}
void solve(int x)
{
int sum=,rt=;
for(int i=;i>=;i--){
int id=(x>>i)&;
if(nod[rt][!id]){
sum|=(<<i);
rt=nod[rt][!id];
}else if(nod[rt][id]) rt=nod[rt][id];
else break;
}
ans=max(ans,sum);
}
int main()
{
int n;
while(scanf("%d",&n)==){
int x,y,z;
init();
for(int i=;i<n;i++){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
dfs(,-,);
for(int i=;i<n;i++){
solve(sxor[i]);
insert(sxor[i]);
}
printf("%d\n",ans);
}
return ;
}
poj 3764 字典树的更多相关文章
- POJ 2418 字典树
题目链接:http://poj.org/problem?id=2418 题意:给定一堆树的名字,现在问你每一棵树[无重复]的出现的百分比,并按树名的字典序输出 思路:最简单的就是用map来写,关于字典 ...
- POJ 2503 字典树
题目链接:http://poj.org/problem?id=2503 题意:给定一个词典,输入格式为[string1' 'string2] 意思是string2的值为string1. 然后给定一波 ...
- POJ 2001 字典树(入门题)
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...
- POJ 2513 字典树+并查集+欧拉路径
Description: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木 ...
- poj 3764 The xor-longest Path(字典树)
题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...
- POJ 3764 - The xor-longest Path - [DFS+字典树变形]
题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...
- ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)
题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...
- POJ 3764 (异或+字典树)
早就听过用字典树求异或最大值,然而没做过.发现一碰到异或的题就GG,而且因为以前做过的一道类似的题(事实上并不类似)限制了思路,蠢啊= =. 题意:一棵带权的树,求任意两点间路径异或的最大值. 题解: ...
- POJ 3764 The xor-longest Path ( 字典树求异或最值 && 异或自反性质 && 好题好思想)
题意 : 给出一颗无向边构成的树,每一条边都有一个边权,叫你选出一条路,使得此路所有的边的异或值最大. 分析 : 暴力是不可能暴力的,这辈子不可能暴力,那么来冷静分析一下如何去做.假设现在答案的异或值 ...
随机推荐
- Linux内核学习笔记(1)-- 进程管理概述
一.进程与线程 进程是处于执行期的程序,但是并不仅仅局限于一段可执行程序代码.通常,进程还要包含其他资源,像打开的文件,挂起的信号,内核内部数据,处理器状态,一个或多个具有内存映射的内存地址空间及一个 ...
- 【RL系列】马尔可夫决策过程——Gambler's Problem
Gambler's Problem,即“赌徒问题”,是一个经典的动态编程里值迭代应用的问题. 在一个掷硬币游戏中,赌徒先下注,如果硬币为正面,赌徒赢回双倍,若是反面,则输掉赌注.赌徒给自己定了一个目标 ...
- 项目Beta冲刺(团队)第二天
1.昨天的困难 夜间模式实现的时候一点击开关就会出现app黑屏卡死的状态,recreate()方法实现有问题 服务器有点问题 2.今天解决的进度 成员 进度 陈家权 研究如何实现私信模块 赖晓连 最新 ...
- Eclipse的黑色主题背景(github)
MoonRise UI Theme An early version of a dark UI theme for Eclipse 4+. Requirements Eclipse 4.2+ In ...
- lintcode-392-打劫房屋
392-打劫房屋 假设你是一个专业的窃贼,准备沿着一条街打劫房屋.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自 ...
- ArrayList和LinkedList源码分析
ArrayList 非线程安全 ArrayList内部是以数组存储元素的.类有以下变量: /*来自于超类AbstractList,使用迭代器时可以通过该值判断集合是否被修改*/ protected t ...
- Java 成员初始化顺序
package com.cwcec.test; class Fu { int num = 5; //构造代码块 { System.out.println("Fu constructor co ...
- Swift-存储属性,计算属性,类属性
//类的属性定义 class Student: NSObject { // 定义属性 // 定义存储属性 var age : Int = var name :String? var mathScore ...
- BOM对象属性定时器的调用
使count中的内容,自动切换 <body> <h1 id="count"></h1> </body> //获取count var ...
- (五)hadoop系列之__集群搭建SSH无密访问多台机器
免密码ssh设置 现在确认能否不输入口令就用ssh登录localhost: $ ssh localhost 如果不输入口令就无法用ssh登陆localhost,执行下面的命令: . 并修改hosts映 ...