【POJ 3764】 The xor-longest path
【题目链接】
http://poj.org/problem?id=3764
【算法】
首先,我们用Si表示从节点i到根的路径边权异或和
那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就是Sx xor Sy
问题就转化为了 : 在若干个数中,找到两个数异或的最大值,可以用Trie树加速,具体细节笔者不再赘述
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 200010 int i,n,tot,u,v;
int head[MAXN];
long long s[MAXN];
long long w,ans; struct Edge
{
int to;
long long w;
int nxt;
} e[MAXN<<]; inline void add(int u,int v,long long w)
{
tot++;
e[tot] = (Edge){v,w,head[u]};
head[u] = tot;
}
inline void dfs(int u,int fa)
{
int i,v;
long long w;
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (v == fa) continue;
s[v] = s[u] ^ w;
dfs(v,u);
}
}
class Trie
{
private :
int tot;
int child[MAXN*][];
public :
inline void clear()
{
tot = ;
memset(child,,sizeof(child));
}
inline void insert(long long x)
{
int i,t,now = ;
for (i = ; i < ; i++)
{
if (x & ( << (- i))) t = ;
else t = ;
if (!child[now][t]) child[now][t] = ++tot;
now = child[now][t];
}
}
inline long long getans(long long x)
{
int i,now = ,t;
long long ans = ,val;
for (i = ; i < ; i++)
{
if (x & ( << ( - i))) t = ;
else t = ;
val = << ( - i);
if (child[now][t^])
{
now = child[now][t^];
ans |= val;
} else now = child[now][t];
}
return ans;
}
} T; int main()
{ while (scanf("%d",&n) != EOF)
{
T.clear();
tot = ; ans = ;
for (i = ; i < n; i++) head[i] = ;
for (i = ; i < n; i++)
{
scanf("%d%d%lld",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
dfs(,-);
for (i = ; i < n; i++) T.insert(s[i]);
for (i = ; i < n; i++) ans = max(ans,T.getans(s[i]));
printf("%lld\n",ans);
} return ;
}
【POJ 3764】 The xor-longest path的更多相关文章
- 【POJ 3764】The Xor-longest Path
题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- jQuery——开关灯
js对象与jquery对象的相互转化: 1.$(js对象) 2.$(selector).get(索引).$(selector)[索引] <!DOCTYPE html> <html l ...
- Object.keys() https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
Object.keys() 方法会返回一个由一个给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和使用for...in 循环遍历该对象时返回的顺序一致 (两者的主要区别是 一个 for-in ...
- 调用CAD内的颜色选择对话框
colordialog类 int color; acedSetColorDialog(color,TRUE,0); 第一个函数返回的是颜色的RGB值
- 解决Webpack中提示syntax 'classProperties' isn't currently enabled的错误
当我们使用了一些JavaScript的一些新特性的时候,但是有没有在webpack.config.js里面或者是.babelrc文件中配置相关插件,就可以解决了. error:Support for ...
- 洛谷——P1759 通天之潜水
P1759 通天之潜水 题目背景 直达通天路·小A历险记第三篇 题目描述 在猴王的帮助下,小A终于走出了这篇荒山,却发现一条波涛汹涌的河拦在了自己的面前.河面上并没有船,但好在小A有n个潜水工具. ...
- 准备MPI编程环境——Visual Studio
准备 下载并安装Visual Studio 2017 下载并安装MPI (建议使用MSMPI,相对简单方便一点,可以从微软官网下载获得) 配置 新建空白项目 在该项目中新建源文件 右击项目-> ...
- CF36E Two Paths (欧拉回路+构造)
题面传送门 题目大意:给你一张可能有重边的不保证联通的无向图,现在要在这个图上找出两条路径,恰好能覆盖所有边一次,根据边的编号输出方案,无解输出-1 一道很不错的欧拉路径变形题 首先要知道关于欧拉路径 ...
- Spring 注解注入的几种方式(转)
平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...
- 使用redis和简单token机制校验身份的思路
1. 登录时生成token, 以token为键,以用户信息为值,存储在redis中,设置key过期时间 2. 需要身份验证的接口,带上token 3. 接口校验redis中token是否存在 4. 存 ...
- Python OS & sys模块
os模块(* * * *) os模块是与操作系统交互的一个接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname" ...