当前位置:网站首页>Longest XOR path (dfs+01trie)
Longest XOR path (dfs+01trie)
2022-06-29 17:56:00 【eva_ can(not)survive】
The longest XOR path - Luogu https://www.luogu.com.cn/problem/P4551 First use dfs Node 1 Find the XOR path value to all points , According to the XOR property, the XOR distance between any two points can be obtained , In this case, the problem is to find the maximum XOR distance between two points , It can be transformed into finding the maximum XOR value of a sequence for a number , You can use it 01trie To write the
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#define lowbit(x) ((x)&(-x))
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
using pll=pair<ll,ll>;
const int MAXN=1e6+5;
const int INF=0x3f3f3f3f;
const ll NNF=0x3f3f3f3f3f3f3f3f;
const int MAXBIT=31;
int n;
int head[MAXN];
int ver[MAXN];
int cost[MAXN];
int num[MAXN];
int nxt[MAXN];
int tri[MAXN][2];
int rec=1;
int cnt;
void add(int x,int y,int c){
ver[++cnt]=y;
cost[cnt]=c;
nxt[cnt]=head[x];
head[x]=cnt;
}
int d[MAXN];
void dfs(int p,int fa){
// printf("===\n");
for(int i=head[p];i;i=nxt[i]){
int v=ver[i];
if(v==fa) continue;
d[v]=d[p]^cost[i];
dfs(v,p);
}
}
void insert(int x){
int cur=1;
for(int i=MAXBIT;i>=0;i--){
int bit=x>>i&1;
if(!tri[cur][bit]) tri[cur][bit]=++rec;
cur=tri[cur][bit];
}
num[cur]=x;
}
int find_max(int x){
int cur=1;
for(int i=MAXBIT;i>=0;i--){
int bit=x>>i&1;
if(tri[cur][bit^1]) cur=tri[cur][bit^1];
else cur=tri[cur][bit];
}
return x^num[cur];
}
int main(){
scanf("%d",&n);
int x,y,c;
for(int i=1;i<n;i++){
scanf("%d %d %d",&x,&y,&c);
add(x,y,c);
add(y,x,c);
}
dfs(1,-1);
for(int i=1;i<=n;i++){
insert(d[i]);
}
int mx=0;
for(int i=1;i<=n;i++){
mx=max(mx,find_max(d[i]));
}
printf("%d",mx);
return 0;
}
边栏推荐
- 小程序容器是什么技术?能助力物联网企业红海突围?
- Mongotemplate - distinct use
- Cross border independent station language Unicode to Hebrew
- Partial mock of static class of phpunit operation
- 自动化软件测试 - 利用短信转发器结合Selenium读取短信验证码
- 跨境独立站语言unicode转希伯来语
- Maxcompute string replacement function -replace
- DevCloud加持下的青软,让教育“智”上云端
- selenium 组合键操作
- 软件快速交付真的需要以安全为代价吗?
猜你喜欢

How to create and delete MySQL triggers

Top 30 open source software
![[target tracking] |stark configuration win OTB](/img/29/a6b3b99b7d2349499aede9e76ab29a.png)
[target tracking] |stark configuration win OTB

Analyze the implementation principle of zero copy mechanism, applicable scenarios and code implementation

Software testing - you may not understand the basic theoretical knowledge

分布式 | 几步快速拥有读写分离
![Fill in the next right node pointer of each node [make good use of each point - > reduce the space-time complexity as much as possible]](/img/33/bda0a898bfe3503197026d1f62e851.png)
Fill in the next right node pointer of each node [make good use of each point - > reduce the space-time complexity as much as possible]

基于gis三维可视化的智慧城市行业运用

ISO 32000-2 international standard 7.7

SRM supplier collaborative management system function introduction
随机推荐
Top 30 open source software
Parental delegation mechanism
Visio annotation, annotation location
Bloom filter:
双亲委派机制
Have you grasped the most frequently asked question in the interview about massive data processing?
How to create and delete MySQL triggers
reflex
迈动互联中标大家保险集团
selenium 组合键操作
Sword finger offer 13 Robot range of motion (BFS)
SRM supplier collaborative management system function introduction
第42期:MySQL 是否有必要多列分区
【Try to Hack】Cookie和Session
How QQ opens online customer service
Timer interrupt experiment based on stm32f103zet6 library function
跨境独立站语言unicode转希伯来语
Software testing - you may not understand the basic theoretical knowledge
小程序容器是什么技术?能助力物联网企业红海突围?
mac安装php7.2