【洛谷】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;
}
相关推荐
xu_yule5 分钟前
算法基础—搜索(2)【记忆化搜索+BFS+01BFS+Floodfill]
数据结构·算法
s090713619 分钟前
Xilinx FPGA使用 FIR IP 核做匹配滤波时如何减少DSP使用量
算法·fpga开发·xilinx·ip core·fir滤波
老马啸西风21 分钟前
成熟企业级技术平台-10-跳板机 / 堡垒机(Bastion Host)详解
人工智能·深度学习·算法·职场和发展
子夜江寒22 分钟前
逻辑回归简介
算法·机器学习·逻辑回归
软件算法开发36 分钟前
基于ACO蚁群优化算法的多车辆含时间窗VRPTW问题求解matlab仿真
算法·matlab·aco·vrptw·蚁群优化·多车辆·时间窗
another heaven1 小时前
【软考 磁盘磁道访问时间】总容量等相关案例题型
linux·网络·算法·磁盘·磁道
tap.AI1 小时前
理解FSRS算法:一个现代间隔重复调度器的技术解析
算法
老马啸西风1 小时前
成熟企业级技术平台-09-加密机 / 密钥管理服务 KMSS(Key Management & Security Service)
人工智能·深度学习·算法·职场和发展
Ulana1 小时前
计算机基础10大高频考题解析
java·人工智能·算法
Ayanami_Reii2 小时前
区间不同数的个数-树状数组/线段树/莫队/主席树
数据结构·c++·算法·线段树·树状数组·主席树·莫队