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: ...
随机推荐
- MyBatis 源码篇-DataSource
本章介绍 MyBatis 提供的数据源模块,为后面与 Spring 集成做铺垫,从以下三点出发: 描述 MyBatis 数据源模块的类图结构: MyBatis 是如何集成第三方数据源组件的: Pool ...
- MyBatis 源码篇-日志模块1
在 Java 开发中常用的日志框架有 Log4j.Log4j2.Apache Common Log.java.util.logging.slf4j 等,这些日志框架对外提供的接口各不相同.本章详细描述 ...
- 6-MySQL DBA笔记-查询优化
第6章 查询优化 查询优化是研发人员比较关注也是疑问较多的领域.本章首先为读者介绍常用的优化策略.MySQL的优化器.连接机制,然后介绍各种语句的优化,在阅读本章之前,需要先对EXPLAIN命令,索引 ...
- set-cookie中的SameSite属性
原文:set-cookie中的SameSite属性 再见,CSRF:讲解set-cookie中的SameSite属性 2016-04-14 13:18:42 来源:360安全播报 作者:暗羽喵 阅读: ...
- Pattern Recognition and Machine Learning-02-1.0-Introduction
Introduction The problem of searching for patterns in data is a fundamental one and has a long and s ...
- C# list to dictionary
示例: 新建一个类: public class Lang { public string En; public string Ch; } 实例化并转为字典: List<Lang> lang ...
- springboot有第三方jar打包成jar
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...
- Redis缓存策略设计及常见问题
Redis缓存设计及常见问题 缓存能够有效地加速应用的读写速度,同时也可以降低后端负载,对日常应用的开发至关重要.下面会介绍缓存使用技巧和设计方案,包含如下内容:缓存的收益和成本分析.缓存更新策略的选 ...
- NORDIC 协议栈下使用硬件定时器
在使能蓝牙协议栈后,RTC0.TIMER0被蓝牙协议栈使用,RTC1被软件定时器使用,所以才程序中使用定时器的时候需要避开.
- 8. Object References, Mutability, and Recycling
1. Variables Are Not Boxes # Think variables as sticky notes a = [1, 2, 3] b = a a.append(4) print b ...