【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 200010int 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<<1];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*31][2];public :inline void clear(){tot = 0;memset(child,0,sizeof(child));}inline void insert(long long x){int i,t,now = 0;for (i = 0; i < 31; i++){if (x & (1 << (30- i))) t = 1;else t = 0; if (!child[now][t]) child[now][t] = ++tot;now = child[now][t]; } }inline long long getans(long long x){int i,now = 0,t;long long ans = 0,val;for (i = 0; i < 31; i++){if (x & (1 << (30 - i))) t = 1;else t = 0;val = 1 << (30 - i);if (child[now][t^1]) {now = child[now][t^1];ans |= val;} else now = child[now][t];}return ans;} } T;int main() {while (scanf("%d",&n) != EOF){T.clear();tot = 0; ans = 0;for (i = 0; i < n; i++) head[i] = 0; for (i = 1; i < n; i++){scanf("%d%d%lld",&u,&v,&w);add(u,v,w);add(v,u,w);}dfs(0,-1);for (i = 0; i < n; i++) T.insert(s[i]);for (i = 0; i < n; i++) ans = max(ans,T.getans(s[i])); printf("%lld\n",ans);}return 0; }
转载于:https://www.cnblogs.com/evenbao/p/9250966.html
【POJ 3764】 The xor-longest path相关推荐
- 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)
[POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- BZOJ 2287 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. &q ...
- 【POJ 3026】Borg Maze
[POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队能够无限切割 问搜索到全部alien所须要的总步数 即求一个无向图 包括全部的点而且总权值最小(最小生成树 BFS+最小生成 ...
- 【POJ 3273】 Monthly Expense (二分)
[POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...
- 【POJ 2485】 Highways
[POJ 2485] Highways 最小生成树模板 Prim #includeusing namespace std;int mp[501][501]; int dis[501]; bool vi ...
- 2287. 【POJ Challenge】消失之物(数组递推\分治优化背包)
2287. [POJ Challenge]消失之物 这题的思想和P4564 [CTSC2018]假面优化的思想一样,应该反过来说,假面那个题应该是借鉴这题的思路. 显然不能枚举每个物品消失O(n)O( ...
- bzoj2287【POJ Challenge】消失之物 缺一01背包
bzoj2287[POJ Challenge]消失之物 缺一01背包 链接 bzoj 思路 分治solve(l,r,arr)表示缺少物品\([l,r]\)的dp数组arr. 然后solve(l,mid ...
- 【POJ - 1364】King(差分约束判无解)
题干: Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ` ...
- *【POJ - 2796】 Feel Good (前缀和优化+单调栈维护)
题干: Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 12409 Accepted: 3484 Ca ...
- 【POJ - 2398】Toy Storage (计算几何,二分找位置,叉积,点和直线的位置关系)
题干: Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished play ...
最新文章
- 在图像生成领域里,GAN这一大家族是如何生根发芽的
- python代码需要背吗-python程序需要编译吗
- 安全管家安卓_pc-cillin和腾讯电脑管家比较哪个更好
- struts2常见错误及解决总结
- 蓝桥杯 ALGO-31 算法训练 开心的金明
- 展望Silverlight 5.0新版本更新与发展
- 十年前的网易,新浪,维基百科,百度百科在手机上的打开效果
- 秒杀场景_同步秒杀分析和实战_01
- echarts map 点击地图区域变色_绘制炫酷的地图,不只是pyecharts.map!
- UITableView 系列二 :资料的设定方式 (Navigation Controller切换视图) (实例)
- 再谈strncpy函数--值得一看的好文章
- linux用vi编辑后保存退出命令是什么,linux vi保存退出命令
- Win7英文版安装中文语言包过程及方法总结
- 超好用的纸飞机串口调试助手,支持绘图和关键字高亮
- 自媒体学习教程 新手怎么开始学习自媒体
- 【视频来了】那些未曾学到的Esp8266技术干货,都在本系列公开课直播中一一吸收,奉献开源于国内物联网!
- 适合程序员编程的笔记本电脑如何选择?
- pic16f1829 c语言,PIC16F1829 串口初始化程序及应用
- 网络维护和服务器面试常考题,网络维护面试题
- C++判断Office版本