博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3764 The xor-longest Path (01 Trie)
阅读量:4598 次
发布时间:2019-06-09

本文共 2306 字,大约阅读时间需要 7 分钟。

链接:http://poj.org/problem?id=3764

题面:

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

Description

In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p:

_{xor}length(p)=\oplus_{e \in p}w(e)

⊕ is the xor operator.

We say a path the xor-longest path if it has the largest xor-length. Given an edge-weighted tree with n nodes, can you find the xor-longest path?  

Input

The input contains several test cases. The first line of each test case contains an integer n(1<=n<=100000), The following n-1 lines each contains three integers u(0 <= u < n),v(0 <= v < n),w(0 <= w < 2^31), which means there is an edge between node u and v of length w.

Output

For each test case output the xor-length of the xor-longest path.

Sample Input

40 1 31 2 41 3 6

Sample Output

7

Hint

The xor-longest path is 0->1->2, which has length 7 (=3 ⊕ 4)

 

思路;

树上dfs一遍跟区间差不多的写法

 

实现代码;

#include
#include
#include
using namespace std;#define ll long longconst int M = 4e5+10;int tot;int ch[32*M][2],cnt,head[M];int val[32*M],a[M],pre[M],nex[M],dp[M],ans;struct node{ int to,next; ll w;}e[M*2];void add(int u,int v,int w){ e[++cnt].to = v;e[cnt].w = w;e[cnt].next = head[u];head[u] = cnt;}void init(){ tot = 1; ans = 0; cnt = 0; ch[0][0] = ch[0][1] = 0; memset(head,0,sizeof(head));}void ins(ll x){ int u = 0; for(int i = 31;i >= 0;i --){ int v = (x>>i)&1; if(!ch[u][v]){ ch[tot][0] = ch[tot][1] = 0; val[tot] = 0; ch[u][v] = tot++; } u = ch[u][v]; } val[u] = x;}int query(int x){ int u = 0; for(int i = 31;i >= 0;i --){ int v = (x>>i)&1; if(ch[u][v^1]) u = ch[u][v^1]; else u = ch[u][v]; } return x^val[u];}void dfs(int u,int fa,int val){ ins(val); for(int i = head[u];i;i = e[i].next){ int v = e[i].to; if(v == fa) continue; ans = max(ans,query(val^e[i].w)); dfs(v,u,val^e[i].w); }}int main(){ int n,u,v,w; while(scanf("%d",&n)!=EOF){ init(); for(int i = 1;i < n;i++){ scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } dfs(0,-1,0); printf("%d\n",ans); }}

 

转载于:https://www.cnblogs.com/kls123/p/10724745.html

你可能感兴趣的文章
Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween
查看>>
JavaScript空判断
查看>>
洛谷 P1439 【模板】最长公共子序列(DP,LIS?)
查看>>
python timeit
查看>>
Wireless Network 并查集
查看>>
51nod 1019 逆序数
查看>>
20145202马超《JAVA》预备作业1
查看>>
云推送注意(MSDN链接)
查看>>
IDEA 生成 jar 包
查看>>
加减乘除混合版
查看>>
linux基础6-bash shell编程
查看>>
掌握这几种微服务模式助你成为更出色的工程师
查看>>
为什么很多语言选择在JVM上实现
查看>>
绘制dot 图
查看>>
CSS Reset CSS Framework
查看>>
如何用WinCC发送报警消息至微信
查看>>
LeetCode算法扫题系列19
查看>>
nginx获取经过层层代理后的客户端真实IP(使用正则匹配)
查看>>
YII实现dropDownList 联动事件
查看>>
搞定PHP面试 - 正则表达式知识点整理
查看>>