【题目链接】

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

  1. 【POJ 3764】The Xor-longest Path

    题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...

  2. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  3. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  4. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  5. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  6. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  7. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  8. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  9. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

随机推荐

  1. PB连接SQLITE

    sqlite3数据库,简单而功能强大,比起ini文件保存用户设置,更简单安全,为什么使用数据库存用户设置,由开发者自己去想吧进入话题:pb中可以用ole DB方式在不注册odbc的情况下直接连接数据库 ...

  2. html5——伸缩布局

    基本概念 1.主轴:Flex容器的主轴主要用来配置Flex项目,默认是水平方向 2.侧轴:与主轴垂直的轴称作侧轴,默认是垂直方向的 3.方向:默认主轴从左向右,侧轴默认从上到下 4.主轴和侧轴并不是固 ...

  3. Linq处理decimal字段汇总Sum()为NULL

    xxxxxxxx.Sum(f => f.jifen).GetValueOrDefault(0)

  4. 【译】x86程序员手册03 - 2.1内存组织和分段

    2.1 Memory Organization and Segmentation 内存组织和分段 The physical memory of an 80386 system is organized ...

  5. mysql幻读

    开启a,b两个数据库连接,a.b都开启事务后,b插入了一条数据R并提交,验证对a的操作造成的影响 select select for update update R update R 后 select ...

  6. CentOS7将firewall切换为iptables防火墙

  7. HDU_1026_Ignatius and the Princess I_BFS(保存路径)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  8. springboot配置多数据源(JdbcTemplate方式)

    在实际开发中可能会遇到需要配置多个数据源的情况,比如:需要使用多个host.需要使用多种数据库(MySql.Oracle.SqlServer…) 如果使用springboot开发,可做如下配置: Co ...

  9. openstack——neutron网络服务

    一.neutron 介绍:   Neutron 概述 传统的网络管理方式很大程度上依赖于管理员手工配置和维护各种网络硬件设备:而云环境下的网络已经变得非常复杂,特别是在多租户场景里,用户随时都可能需要 ...

  10. Unittest加载执行用例的方法总结

    前言 说到测试框架,unittest是我最先接触的自动化测试框架之一了, 而且也是用的时间最长的, unittest框架有很多方法加载用例,让我们针对不同的项目,不同项目的大小及用例的多少自己选择加载 ...