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: ...
随机推荐
- dl in image process
deep learning目前为止无论在分类还是检测上,都是整体的处理,而不会出现像sift这样的局部特征,这个问题或许如果能克服掉,能让检测效果更进一大步.
- shell使用ps -ef|grep xxx时不显示grep xxx进程的方法
在使用ps -ef|grep xxx时会将grep xxx的进程也带出来, 而在脚本中如果想要截取此命令结果的一部分,则grep xxx的进程会显得多余,如下: [root@localhost ~]# ...
- SMTP实现发送邮箱1
#include "stdafx.h" #include <iostream> #include <WinSock2.h> using namespace ...
- 使用js输出1000以内的水仙花数
什么是水仙花数 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI).自恋数.自幂数.阿姆斯壮数或阿姆斯特 ...
- C8051F环境搭建
https://www.silabs.com/ USB调试器 U-EC6: 支持JTAG模式.C2模式 JTAG接口定义: 适用型号C8051F00x C8051F01x C8051F02x C805 ...
- bash shell脚本之查看当前日期以及登陆用户
查看当前日期以及登陆用户: cat test1: #!/bin/bash # This script displays the date and who's logged on echo -n The ...
- JPA的API介绍、工具类抽取
1.Persistence对象 Persistence对象主要作用是用于获取EntityManagerFactory对象的 .通过调用该类的createEntityManagerFactory静态方法 ...
- WPF - 仿QQ2014
声明:非原创.项目是网上发现的,以学习为目的重写了部分代码,合理地调整了下布局,巧妙地简化了下Style样式.重写还算是有价值的,并非完全复制. 效果: 获取项目源码:https://pan.baid ...
- 【github】github的使用
一.上传本地代码 1.在github上新建一个repository(命名为英文) 2.打开cmd,进入上传代码所在目录 3.输入如下命令 第一步:git init --建仓第二步:git add * ...
- tensorflow保存数据为.pb格式和加载pb文件
转自:https://blog.csdn.net/u014264373/article/details/79943389 https://blog.csdn.net/fu6543210/article ...