POJ3764
题目 POJ3764 The xor-longest Path
[原题传送门](http://poj.org/problem?id=3764)
主要思路:
1.求出每个点到根节点(这里是树,所以直接取0)路径上所有权值xor和为d[i],则任意两点间路径xor和则为 d[x]^d[y](至于证明,~~作者太懒,不想写~~)
2.接着用trie树跑出 max(d[x]^d[y]) (0<=x<n && 0<=y<n)
Code
#include<cstdio>
#include<cstring>
//#include<windows.h>
using namespace std;
#define rg register int
#define I inline int
#define V inline void
#define ll long long
#define db double
#define B inline bool
#define F1(i,a,b) for(rg i=a;i<=b;++i)
#define F2(i,a,b) for(rg i=a;i>=b;--i)
#define ed putchar('\n')
#define bl putchar(' ')
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
//#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char buf[1<<21],*p1=buf,*p2=buf;
const int N=100005;
template<typename TP>V read(TP &x)
{
TP f=1;x=0;register char c=getchar();
for(;c<'0'||c>'9';c=getchar()) if(c=='-') f=-1;
for(;c>='0'&&c<='9';c=getchar()) x=(x<<3)+(x<<1)+(c^'0');
x*=f;
}
template<typename TP>V print(TP x)
{
if(x<0) x=-x,putchar('-');
if(x>9) print(x/10);
putchar(x%10+'0');
}
int n,a,b,c,ans,cnt=1,t[N<<5][2];
struct node{
int v,w,nxt;
}e[N<<1];
int tot,h[N],d[N];
template<typename TP>V add(TP u,TP v,TP w)
{
e[++tot].v=v;
e[tot].w=w;
e[tot].nxt=h[u];
h[u]=tot;
}
template<typename TP>V dfs(TP x,TP fa)
{
// print(d[x]),system("pause"),ed;
for(TP i=h[x];i;i=e[i].nxt)
{
TP v=e[i].v,w=e[i].w;
if(v==fa) continue;
d[v]=d[x]^w;
dfs(v,x);
}
}
struct T{
template<typename TP>V insert(TP val)
{
TP rt=1;
F2(i,30,0)
{
TP id=val>>i&1;
if(!t[rt][id]) t[rt][id]=++cnt;
rt=t[rt][id];
}
}
template<typename TP>I search(TP val)
{
TP rt=1,sum=0;
F2(i,30,0)
{
TP id=val>>i&1;
if(t[rt][id^1]) rt=t[rt][id^1],sum|=1<<i;
else rt=t[rt][id];
}
return sum;
}
}trie;
V init()
{
ans=tot=0,cnt=1;
memset(h,0,sizeof h);
memset(t,0,sizeof t);
}
int main()
{
while(~scanf("%d",&n))
{
init();
F1(i,1,n-1)
{
read(a),read(b),read(c);
add(a,b,c),add(b,a,c);
}
d[0]=0,dfs(0,-1);
F1(i,0,n-1)
{
trie.insert(d[i]);
ans=Max(ans,trie.search(d[i]));
}
print(ans),ed;
// F1(i,0,n-1) print(d[i]),bl;
}
return 0;
}
POJ3764的更多相关文章
- 【poj3764】 The xor-longest Path
http://poj.org/problem?id=3764 (题目链接) 今天的考试题,看到异或就有点虚,根本没往正解上想.. 题意 给出一棵带权树,请找出树上的一条路径,使其边上权值的异或和最大. ...
- bzoj1954 poj3764
对于xor有一个非常重要的性质A xor B xor B=A 并且满足交换律和结合律这道题是求无根树上最长的xor路径我们知道,无根树的题目我们都是要想办法转化为有根树来处理当我们确定了一个根,根到每 ...
- POJ3764 The xor-longest Path
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6361 Accepted: 1378 Description In ...
- POJ3764 The xor-longest Path(Trie树)
题目给一棵有边权的树,问树上任意两点路径上的边异或值最多是多少. 记录每个点u到根路径的异或值xor[u],那么任意两点u.v路径的异或值就是xor[u]^xor[v]. 于是这个问题就变成了从n个数 ...
- POJ3764 The xor-longest path Trie树
代码写了不到30分钟,改它用了几个小时.先说题意,给你一颗树,边上有权,两点间的路径上的路径的边权抑或起来就是路径的xor值,要求的是最大的这样的路径是多少.讲到树上的两点的xor,一个常用的手段就是 ...
- poj3764(dfs+Trie树+贪心)
题目链接:http://poj.org/problem?id=3764 分析:好题!武森09年的论文中有道题CowXor,求的是线性结构上的,连续序列的异或最大值,用的办法是先预处理出前n项的异或值, ...
- [POJ3764]最长异或路径
Description: 给定一棵n个点的带权树,结点下标从1开始到N.寻找树中找两个结点,求最长的异或路径. Hint: \(n<=10^5\) Solution: 真是01Trie傻逼题,居 ...
- [luogu4551][POJ3764]最长异或路径
题目描述 给定一棵n个点的带权树,结点下标从1开始到N.寻找树中找两个结点,求最长的异或路径. 异或路径指的是指两个结点之间唯一路径上的所有边权的异或. 分析 处理出各个节点到根节点的异或距离,然后我 ...
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
随机推荐
- 08Dockerfile基本使用
使用Dockerfile创建镜像 Dockerfile是一个文本格式的配置文件,用户可以使用Dockerfile赖快速创建自定义的镜像. Dockerfile由一行行命令组成,#开头为注释. 1:Do ...
- winfrom_权限设置_TreeView的相关问题
1.获取TreeView的值: 循环TreeView,获取checked每个节点的Text,串起来用逗号“,”隔开,保存到数据库. List<string> list = new List ...
- __imp__SetupDiDestroyDeviceInfoList
error LINK2019 unresolved external symbol __imp__SetupDiDestroyDeviceInfoList 分类: 转载文章2012-11-02 15: ...
- windows 无法找到unistd.h 的解决方法
//#include <unistd.h> #ifndef _UNISTD_H #define _UNISTD_H #include <io.h> #include < ...
- LeetCode 腾讯精选50题--2的幂
在二进制中,2的幂的数字用二进制表示时只会有一位表示为1,其余都为0,基于这个前提,可以有两种方案: 1. 做位移操作 2. 与数值取反并与原数值做与操作,判断是否与原来的数值相同 对于方案1,我的想 ...
- Java秒杀实战 (四)JMeter压测
转自:https://blog.csdn.net/qq_41305266/article/details/81071278. 一.JMeter入门 下载链接 http://jmeter.apache. ...
- 如何对Nginx日志文件进行切割保存
日积月累下,日志文件会越来越大,日志文件太大严重影响服务器效率,须要定时对日志文件进行切割. 切割的方式有按月切割.按天切割.按小时切割,一般都是按天切割. 那么如何进行切割呢? 思路: 创建日志文件 ...
- Yii 2.0 GII 访问404错误
网上大部分都是普通的开启和配置资料 按照网上资料配置 访问localhost/index/php?r=gii 总是提示404错误 解决方法如下: Yii基础版中的 web.php 代码如下 if (Y ...
- docker container 导入和导出
目录 docker container 导入和导出 1.前言 2.docker container 的导出 3.docker container 的导入 4.镜像和容器 导出和导入的区别 docker ...
- Computer Vision_1_Active Appearance Models:Active Appearance Models——2001
此为计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面. 1. Active Appearance Models 活动表观模型和活动轮廓模型基本思想来源 Snake,现在 ...