当前位置:网站首页>01tire+ chain forward star +dfs+ greedy exercise one
01tire+ chain forward star +dfs+ greedy exercise one
2022-07-07 16:24:00 【Soup key TJ】
The longest XOR path
Title Description
Given a tree n n n A weighted tree with two points , Node subscript from 1 1 1 Start to n n n. Look for two nodes in the tree , Find the longest XOR path .
XOR path refers to the XOR of all edge weights on the unique path between two nodes .
Input format
The first line is an integer n n n, Represents the number of points .
Next n − 1 n-1 n−1 That's ok , give u , v , w u,v,w u,v,w , Each represents... On the tree u u u Point and v v v Point has edge , The weight of the edge is w w w.
Output format
a line , An integer represents the answer .
Examples #1
The sample input #1
4
1 2 3
2 3 4
2 4 6
Sample output #1
7
Tips
The longest XOR sequence is 1 , 2 , 3 1,2,3 1,2,3, The answer is 7 = 3 ⊕ 4 7=3\oplus 4 7=3⊕4.
Data range
1 ≤ n ≤ 100000 ; 0 < u , v ≤ n ; 0 ≤ w < 2 31 1\le n \le 100000;0 < u,v \le n;0 \le w < 2^{31} 1≤n≤100000;0<u,v≤n;0≤w<231.
The process is all in the notes
#include<bits/stdc++.h>
using namespace std;
const int N=6e5+10;
typedef long long ll;
int son[N][2],idx=1,s=0;
int n,head[N],dis[N],nt[N],to[N],weight[N],cnt;
void add(int u,int v,int w){
// Chain forward star edge
nt[++cnt]=head[u];
head[u] = cnt;
to[cnt]=v;
weight[cnt]=w;
}
void insert(int k){
int p=1;
for(int i=30;i>=0;i--){
// Due to the scope of the topic w<2^31, So for XOR and just 30 Who can
int u=k>>i&1;// obtain k Of the i A binary value
if(!son[p][u]) son[p][u]=++idx;// View the current node p Child nodes of u Whether the number has been given , If there is no mark, it will be numbered
p=son[p][u];// Move to the next node conversion position
}
}
void dodo(int k){
int p=1,da=0;
for(int i=30;i>=0;i--){
int u=k>>i&1;// obtain k Of the i A binary value
// The current in trie Node of tree u; u My two sons are son[u][0/1];
// Our greedy consideration
//trie The tree goes down from the top , Corresponding to binary bits from high to low
// And we hope that the high binary bits will be XOR as much as possible 1
// That is to say, we are trie As the tree goes down from its roots , Try to choose an XOR that allows the current one to get 1 The path of
if(son[p][u^1]){
// XOR has the characteristic that two identical numbers of XOR are equal to no XOR ; In order for this XOR to get 1, We need to go to the next level of traversal son[p][u^1]
p=son[p][u^1];// There is son[p][u^1], Then greedily select and move to the next node conversion position
da|=(1<<i);//da Store answers , Here will be da Of the i Binary bit assignment 1
}
else p=son[p][u];// If it doesn't exist son[p][u^1], We can only give up this one , Go to son[p][u]
}
s=max(s,da);// Update answers
}
void dfs(int u,int v){
// seek dis Array for conversion
insert(dis[u]);// Insert the binary digits of this value trie In the tree
dodo(dis[u]);// From the top down , Greedy choice , Update answers
for(int i=head[u];i;i=nt[i]){
// Traverse to i For the starting edge
int vv=to[i];// Arrival point
if(vv==v) continue;
dis[vv]=dis[u]^weight[i];// XOR all edge weights on the unique path between two nodes
dfs(vv,u);// Continue to drill down
}
}
int main(){
cin>>n;
for(int i=1;i<n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);// Chain forward star plus two-way edge , In all directions
add(v,u,w);
}
dfs(1,0);// The title shows , Node from 1 Start
cout<<s;
return 0;
}
边栏推荐
- three.js打造酷炫下雪效果
- Unity3d click events added to 3D objects in the scene
- Wireless sensor networks -- ZigBee and 6LoWPAN
- Detailed explanation of several ideas for implementing timed tasks in PHP
- AE learning 02: timeline
- Sysom case analysis: where is the missing memory| Dragon lizard Technology
- Balanced binary tree (AVL)
- Dotween -- ease function
- Shader_ Animation sequence frame
- Laravel5.1 路由 -路由分组
猜你喜欢
pycharm 终端部启用虚拟环境
SPI master RX time out interrupt
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
【Vulnhub靶场】THALES:1
Vs tool word highlight with margin
Statistical learning method -- perceptron
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
[vulnhub range] thales:1
通知Notification使用全解析
随机推荐
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
目标跟踪常见训练数据集格式
企业级日志分析系统ELK
分步式監控平臺zabbix
logback.xml配置不同级别日志,设置彩色输出
SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
MySQL中, 如何查询某一天, 某一月, 某一年的数据
Tragedy caused by deleting the console statement
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
IP地址和物理地址有什么区别
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
U3D_ Infinite Bessel curve
How to determine whether the checkbox in JS is selected
【Vulnhub靶场】THALES:1
The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
Talk about the cloud deployment of local projects created by SAP IRPA studio
Plate - forme de surveillance par étapes zabbix
asyncio 概念和用法