P4551 最长异或路径(01字典树)

题目链接

分析

众所周知,同一个数异或偶数次=0,相当于没有异或;
所以 dis(i , j) 等价于 dis(root , i) ^ dis(root , j)
(其中dis(i , j)表示 i 到 j 路径上的异或值,root是树根)。
因为中间 root 到 lca(i , j) 的部分异或了两次,抵消掉了。

所以我们可以先dfs预处理处每个点到1(根节点)的路径异或值,放到一个数组里;
那么此时问题就转换成:给一个数组,任意选两个数 ^ 一下 ,求^的最大值。(板子题)
板子题链接

先别走!!!再来一道题加深对01字典树的应用!!!
题目链接
参考题解

代码

#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<map>
#include<algorithm>
#include<deque>
#include<stack>
#include<set>
// #include <unordered_map>
#include<math.h>
#include<string.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0);
using namespace std;
 
#define pb push_back
#define coutl cout<<"------------"<<endl;
#define fi first
#define se second

#define ire(x) scanf("%d",&x)
#define iire(a,b) scanf("%d %d",&a,&b)
#define lre(x) scanf("%lld",&x)
#define llre(a,b) scanf("%lld %lld",&a,&b)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define endl "\n"
#define PI acos(-1.0)
//#define int long long
typedef long long ll;
typedef unsigned long long ull;
      
typedef pair<int, int> PII;
typedef pair<double, int> PDI;
typedef pair<ll, ll> PLL;
typedef pair<double, double> PDD;
typedef pair<double, pair<int, double> > PDID;
typedef pair<char, char> PCC;
typedef pair<char, pair<int, int> > PCII;
typedef pair<int, pair<int, int> > PIII;
typedef pair<int, pair<int, pair<int, int> > > PIIII;
typedef pair<ll, pair<int, int> > PLII;

const int maxn = 2e5 + 7;
const int N = 2e6 + 7;
const int M = 4e6 + 7;
const int mod = 3*5*7*11*13*17*19*23;
const int inv = mod - mod/2;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1);
const double eps = 1e-8;
  
ll gcd(ll a,ll b) {return b==0 ? a : gcd(b,a%b);}
ll lcm(ll a,ll b) {return a*b / gcd(a,b);}
ll qmi(ll a,ll b,ll p) {ll ans = 1; while(b) { if(b & 1) ans = ans * a % p; a = a * a % p; b >>= 1; } return ans;}
int lowbit(int x) {return x & (-x);}

int h[maxn],ne[maxn*2],e[maxn*2],w[maxn*2],idx;
vector<int> v;	//每个点到1的路径异或值
int tr[maxn * 31][2];	//01字典树
int n;

void add(int a,int b,int c)
{
	e[idx]=b; w[idx]=c; ne[idx]=h[a]; h[a]=idx++;
}

void dfs(int u,int fa,int sum)
{
	v.push_back(sum);
	
	for(int i=h[u];i!=-1;i=ne[i])
	{
		int j = e[i];
		if(j == fa) continue;
		
		dfs(j,u,sum^w[i]);
	}
}

void insert(int x)
{
	int p = 0;
	for(int i=30;i>=0;i--)
	{
		int u = x >> i & 1;
		
		if(!tr[p][u]) tr[p][u] = ++idx;
		p = tr[p][u];
	}
}

int query(int x)
{
	int ans = 0;
	int p = 0;
	for(int i=30;i>=0;i--)
	{
		int u = x >> i & 1;
		if(tr[p][!u])
		{
			p = tr[p][!u];
			ans = (ans << 1) + (!u);
		}
		else
		{
			p = tr[p][u];
			ans = (ans << 1) + u;
		}
	}
	return ans;
}

int main()
{
	memset(h,-1,sizeof h);
	
	ire(n);
	for(int i=1;i<n;i++)
	{
		int a,b,c;
		iire(a,b);
		ire(c);
		
		add(a,b,c);
		add(b,a,c);
	}
	
	dfs(1,-1,0);	//算出每个点到1的路径异或值
	
	//问题就转换成:给一个数组,任意选两个数,求^最大值(01字典树板子题)
	idx = 0;
	int ans = 0;
	for(auto x : v)
	{
		insert(x);
		
		int y = query(x);	//找和x异或后最大的数
		ans = max(ans,x^y);
	}
	
	cout<<ans<<'\n';
	
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值