POJ 3764 The xor-longest Path trie树解决位运算贪心
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=;
const double eps=1e-;
const long long modn=;
int n;
int ans=;
struct nod{
int y;
int next;
int v;
}e[maxn*];
int head[maxn]={},tot=;
int dis[maxn]={};
struct tri{
bool exist;
int v;
int next[];
}a[maxn*];
int tot1=;
void init(int x,int y,int z){
e[++tot].y=y;
e[tot].v=z;
e[tot].next=head[x];
head[x]=tot;
}
void doit(int x,int v,int k){
if(k<){
a[x].exist=;
a[x].v=v;
return;
}int z;
if(v&(<<k)){
z=;
}else{
z=;
}
if(!a[x].next[z]){
a[x].next[z]=++tot1;
}doit(a[x].next[z],v,k-);
}
void getit(int x,int v,int k){
if(k<){
int z=v^a[x].v;
ans=max(ans,z);
return;
}int z;
if(v&(<<k)){
z=;
}else{
z=;
}
if(!a[x].next[z]){
z=^z;
}getit(a[x].next[z],v,k-);
}
void dfs(int x,int fa,int val){
int y,v,z;
for(int i=head[x];i;i=e[i].next){
y=e[i].y;
v=e[i].v;
if(y!=fa){
z=val^v;
dis[y]=z;
dfs(y,x,z);
}
}
}
void yu(){
memset(a,,sizeof(a));
memset(e,,sizeof(e));
memset(head,,sizeof(head));
tot1=tot=;
memset(dis,,sizeof(dis));
ans=;
}
int main(){
while(~scanf("%d",&n)){
yu();
int x,y,z;
for(int i=;i<n;i++){
scanf("%d%d%d",&x,&y,&z);
init(x+,y+,z);
init(y+,x+,z);
}
dfs(,,);
for(int i=;i<=n;i++){
getit(,dis[i],);
doit(,dis[i],);
}
printf("%d\n",ans);
}
return ;
}
POJ 3764 The xor-longest Path trie树解决位运算贪心的更多相关文章
- 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
[题目链接] http://poj.org/problem?id=3764 [算法] 首先,我们用Si表示从节点i到根的路径边权异或和 那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就 ...
- 【bzoj1954】Pku3764 The xor-longest Path Trie树
题目描述 给定一棵n个点的带权树,求树上最长的异或和路径 输入 The input contains several test cases. The first line of each test ...
- POJ3764 The xor-longest path Trie树
代码写了不到30分钟,改它用了几个小时.先说题意,给你一颗树,边上有权,两点间的路径上的路径的边权抑或起来就是路径的xor值,要求的是最大的这样的路径是多少.讲到树上的两点的xor,一个常用的手段就是 ...
- HDU 4825 Xor Sum (trie树处理异或)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- HDU4825 Xor Sum(字典树解决最大异或问题)
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整 ...
- POJ-2777-CountColor(线段树,位运算)
链接:https://vjudge.net/problem/POJ-2777#author=0 题意: Chosen Problem Solving and Program design as an ...
- 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
题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...
随机推荐
- cookie知识点概述
cookie是什么 这个讲起来很简单,了解http的同学,肯定知道,http是一个不保存状态的协议,什么叫不保存状态,就是一个服务器是不清楚是不是同一个浏览器在访问他,在cookie之前,有另外的技术 ...
- python3爬虫.2.伪装浏览器
有的网页在爬取时候会报错返回 urllib.error.HTTPError: HTTP Error 403: Forbidden 这是网址在检测连接对象,所以需要伪装浏览器,设置User Agent ...
- Python3 多进程
多进程(multiprocessing)的用法和多线程(threading)类似,里面的函数也一样,start()为启动函数,join() 等待该进程运行结束,每一个进程也是由它的父进程产生 1.简单 ...
- 64_t1
TOPCOM-0.17.8-2.fc26.x86_64.rpm 13-Feb-2017 22:09 269054 TOPCOM-devel-0.17.8-2.fc26.i686.rpm 13-Feb- ...
- java程序out of memory【转】
相信大家都有感触,线上服务内存OOM的问题,是最难定位的问题,不过归根结底,最常见的原因: 本身资源不够 申请的太多 资源耗尽 58到家架构部,运维部,58速运技术部联合进行了一次线上服务内存OOM问 ...
- 初学Memcached安装及使用【转】
1.yum install memcached安装memecached 2.chkconfig memcached on设置memcached开机启动 3.service memcached star ...
- SyntaxError: Missing parentheses in call to 'print' 这个错误原因是Python版本问题
问题 print "www.baidu.com" Python2 print ("www.baidu.com") Python3 出 ...
- Windows: 如何配置IPv6隧道
清空隧道配置: netsh interface ipv6 set teredo disable netsh interface ipv6 6to4 set state disable netsh in ...
- HEVC代码记录(删除)
得到编码残差 TEncSearch.cpp 4543:rpcYuvResi->subtract( pcYuvOrg, pcYuvPred, 0, uiWidth );
- android4.0 锁屏实现(转)
转载请表明出处:http://blog.csdn.net/wdaming1986/article/details/8837023 好了,言归正传,说说锁屏了,其实把锁屏做成apk的形式,会引起很多问题 ...