题目 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的更多相关文章

  1. 【poj3764】 The xor-longest Path

    http://poj.org/problem?id=3764 (题目链接) 今天的考试题,看到异或就有点虚,根本没往正解上想.. 题意 给出一棵带权树,请找出树上的一条路径,使其边上权值的异或和最大. ...

  2. bzoj1954 poj3764

    对于xor有一个非常重要的性质A xor B xor B=A 并且满足交换律和结合律这道题是求无根树上最长的xor路径我们知道,无根树的题目我们都是要想办法转化为有根树来处理当我们确定了一个根,根到每 ...

  3. POJ3764 The xor-longest Path

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6361   Accepted: 1378 Description In ...

  4. POJ3764 The xor-longest Path(Trie树)

    题目给一棵有边权的树,问树上任意两点路径上的边异或值最多是多少. 记录每个点u到根路径的异或值xor[u],那么任意两点u.v路径的异或值就是xor[u]^xor[v]. 于是这个问题就变成了从n个数 ...

  5. POJ3764 The xor-longest path Trie树

    代码写了不到30分钟,改它用了几个小时.先说题意,给你一颗树,边上有权,两点间的路径上的路径的边权抑或起来就是路径的xor值,要求的是最大的这样的路径是多少.讲到树上的两点的xor,一个常用的手段就是 ...

  6. poj3764(dfs+Trie树+贪心)

    题目链接:http://poj.org/problem?id=3764 分析:好题!武森09年的论文中有道题CowXor,求的是线性结构上的,连续序列的异或最大值,用的办法是先预处理出前n项的异或值, ...

  7. [POJ3764]最长异或路径

    Description: 给定一棵n个点的带权树,结点下标从1开始到N.寻找树中找两个结点,求最长的异或路径. Hint: \(n<=10^5\) Solution: 真是01Trie傻逼题,居 ...

  8. [luogu4551][POJ3764]最长异或路径

    题目描述 给定一棵n个点的带权树,结点下标从1开始到N.寻找树中找两个结点,求最长的异或路径. 异或路径指的是指两个结点之间唯一路径上的所有边权的异或. 分析 处理出各个节点到根节点的异或距离,然后我 ...

  9. poj3764 The XOR Longest Path【dfs】【Trie树】

    The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10038   Accepted:  ...

随机推荐

  1. HTML练习二--动态加载轮播图片

    接上一篇https://www.cnblogs.com/shuaimeng/p/11106655.html demo下载: https://pan.baidu.com/s/1dhvzHwTHKiguy ...

  2. JS基础_全局作用域

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. select input 等控件进行清空操作

    <html> <head> <meta charset="utf-8" /> <title></title> <s ...

  4. C++ STL 之 deque容器 打分案例(内含sort排序用法)

    #include <iostream> #include <vector> #include <time.h> #include <deque> #in ...

  5. MFC基础笔记

    List Control // List Control初始化,下面代码需要放在OnInitDialog()函数里面// 设置扩展风格:正行选中 m_list.SetExtendedStyle(LVS ...

  6. github 提交和更新代码

    …or create a new repository on the command line   echo "# flutterPluginsWorks" >> RE ...

  7. XXX_initcall()函数分析

    1. 先看这些宏的定义(定义在文件include/linux/init.h中) #define pure_initcall(fn) __define_initcall("0",fn ...

  8. PHP面试题--基础

    1.PHP语言的一大优势是跨平台,什么是跨平台?一.PHP基础: PHP的运行环境最优搭配为Apache+MySQL+PHP,此运行环境可以在不同操作系统(例如windows.Linux等)上配置,不 ...

  9. C 字符串几点

    1.字符串结尾必须为“\0” 2.多种处理函数在<string.h> 3.常用字符串处理函数: 1.strlen 求字符串长度(\0不算在内) 2.strcpy(a,b) 将b复制到a中 ...

  10. 团队第三次作业:Alpha版本第一周小结

    姓名 学号 周前计划安排 每周实际工作记录 自我打分 XXX 061109 1.原型设计与编码任务分配 2.构思程序个性化测试模块的初步实现 1.原型设计与编码任务分配 2.设计了部分类及其成员函数( ...