当前位置:网站首页>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;
}
边栏推荐
- laravel中将session由文件保存改为数据库保存
- Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
- 强化实时数据管理,英方软件助力医保平台安全建设
- SPI master rx time out中断
- Leetcode-231-2的幂
- Vs tool word highlight with margin
- 分步式監控平臺zabbix
- ThinkPHP URL 路由简介
- The unity vector rotates at a point
- [vulnhub range] thales:1
猜你喜欢
Numpy -- data cleaning
Mysql database basic operation DQL basic query
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
平衡二叉树(AVL)
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
Enterprise log analysis system elk
The unity vector rotates at a point
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
随机推荐
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
HAVE FUN | “飞船计划”活动最新进展
分步式监控平台zabbix
laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
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
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
A JS script can be directly put into the browser to perform operations
强化实时数据管理,英方软件助力医保平台安全建设
模仿企业微信会议室选择
【C 语言】 题集 of Ⅹ
95. (cesium chapter) cesium dynamic monomer-3d building (building)
Set the route and optimize the URL in thinkphp3.2.3
Migration and reprint
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
Performance measure of classification model
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)