当前位置:网站首页>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;
}
边栏推荐
- SPI master RX time out interrupt
- Sysom case analysis: where is the missing memory| Dragon lizard Technology
- What is the difference between IP address and physical address
- markdown公式编辑教程
- 【知识小结】PHP使用svn笔记总结
- Set the route and optimize the URL in thinkphp3.2.3
- asyncio 概念和用法
- Notification uses full resolution
- Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
- Leetcode-136- number that appears only once (solve with XOR)
猜你喜欢

spark调优(三):持久化减少二次查询
![[vulnhub range] thales:1](/img/fb/721d08697afe9b26c94fede628c4d1.png)
[vulnhub range] thales:1

Unity3D_ Class fishing project, bullet rebound effect is achieved

Balanced binary tree (AVL)

平衡二叉树(AVL)

TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比

Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping

模仿企业微信会议室选择

pycharm 终端部启用虚拟环境

"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
随机推荐
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
Shader_ Animation sequence frame
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
Asyncio concept and usage
Laravel5.1 Routing - routing packets
Shader basic UV operations, translation, rotation, scaling
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
PHP实现微信小程序人脸识别刷脸登录功能
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
spark调优(三):持久化减少二次查询
Wireless sensor networks -- ZigBee and 6LoWPAN
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
pycharm 终端部启用虚拟环境
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
95. (cesium chapter) cesium dynamic monomer-3d building (building)
Sysom case analysis: where is the missing memory| Dragon lizard Technology
Prometheus API deletes all data of a specified job