【洛谷】P4551 最长异或路径 的题解

【洛谷】P4551 最长异或路径 的题解

洛谷传送门

题解

神奇的字典树咩呀qaq

看到异或,我们首先很容易想到 trie 树 还有 线性基。但是这题明显是字典树。

指定一个根 r o o t root root,用 t r i e [ u ] [ v ] trie[u][v] trie[u][v] 表示 u u u 和 v v v 之间的路径的边权异或和,那么 t r i e [ u ] [ v ] = t r i e [ r o o t ] [ u ] ⊕ t r i e [ r o o t ] [ v ] trie[u][v]=trie[root][u]\oplus trie[root][v] trie[u][v]=trie[root][u]⊕trie[root][v],因为 LCA 以上的部分异或两次抵消了。

那么,如果将所有 t r i e [ r o o t ] [ u ] trie[root][u] trie[root][u] 插入到一棵 trie 中,就可以对每个 t r i e [ r o o t ] [ u ] trie[root][u] trie[root][u] 快速求出和它异或和最大的 t r i e [ r o o t ] [ v ] trie[root][v] trie[root][v]:

从 trie 的根开始,如果能向和 T ( r o o t , u ) T(root, u) T(root,u) 的当前位不同的子树走,就向那边走,否则没有选择。

对于每一位进行贪心,如果这一位有一个与它不同的,即异或后是 1 1 1,那我们就顺着这条路往下走;否则就顺着原路往下走。

贪心的正确性:如果这么走,这一位为 1 1 1;如果不这么走,这一位就会为 0 0 0。而高位是需要优先尽量大的。由于高位大决定一切,所以我们贪心是对的。

代码

cpp 复制代码
#include <bits/stdc++.h>
#define re register
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
namespace fastIO {
	inline int read() {
		register int x = 0, f = 1;
		register char c = getchar();
		while (c < '0' || c > '9') {
			if(c == '-') f = -1;
			c = getchar();
		}
		while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
		return x * f;
	}
	inline void write(int x) {
		if(x < 0) putchar('-'), x = -x;
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
		return;
	}
}
using namespace fastIO;
int head[100005], nxt[100005 << 1], to[100005 << 1], weight[100005 << 1], cnt;
int n, dis[100005], ch[100005 << 5][2], tot = 1, ans;
void insert(int x) {
	for(int i = 30, u = 1; i >= 0; i --) {
		int c = ((x >> i) & 1);
		if(!ch[u][c]) ch[u][c] = ++ tot;
		u = ch[u][c];
	}
}
void get(int x) {
	int res = 0;
	for(int i = 30, u = 1; i >= 0; i --) {
		int c = ((x >> i) & 1);
		if(ch[u][c ^ 1]) {
			u = ch[u][c ^ 1];
			res |= (1 << i);
		} 
		else
			u = ch[u][c];
	}
	ans = max(ans, res);
}
void add(int u, int v, int w) {
	nxt[++ cnt] = head[u];
	head[u] = cnt;
	to[cnt] = v;
	weight[cnt] = w;
}
void dfs(int u, int fa) {
	insert(dis[u]);
	get(dis[u]);
	for(int i = head[u]; i; i = nxt[i]) {
		int v = to[i];
		if(v == fa) continue;
		dis[v] = dis[u] ^ weight[i];
		dfs(v, u);
	}
}
int main() {
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	n = read();
	for(int i = 1; i < n; i ++) {
		int u, v, w;
		u = read(), v = read(), w = read();
		add(u, v, w); 
		add(v, u, w);
	}
	dfs(1, 0);
	write(ans), putchar('\n');
	return 0;
}
相关推荐
真的很上进6 分钟前
2025最全TS手写题之partial/Omit/Pick/Exclude/Readonly/Required
java·前端·vue.js·python·算法·react·html5
linweidong12 分钟前
GO 基础语法和数据类型面试题及参考答案(下)
算法·后端面试·泛型·go面试·go面经·go求职
圈圈编码1 小时前
LeetCode Hot100刷题——合并两个有序链表
java·数据结构·算法·leetcode·链表
老歌老听老掉牙1 小时前
旋量理论:刚体运动的几何描述与机器人应用
python·算法·机器学习·机器人·旋量
无聊的小坏坏2 小时前
用递归算法解锁「子集」问题 —— LeetCode 78题解析
算法·深度优先
m0_738596322 小时前
十大排序算法
算法·排序算法
jingfeng5142 小时前
详解快排的四种方式
数据结构·算法·排序算法
MoRanzhi12032 小时前
245. 2019年蓝桥杯国赛 - 数正方形(困难)- 递推
python·算法·蓝桥杯·国赛·递推·2019
henyaoyuancc3 小时前
vla学习 富
人工智能·算法
Gyoku Mint3 小时前
机器学习×第五卷:线性回归入门——她不再模仿,而开始试着理解你
人工智能·python·算法·机器学习·pycharm·回归·线性回归