当前位置:网站首页>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;
}
边栏推荐
- Dotween -- ease function
- 一个普通人除了去工厂上班赚钱,还能干什么工作?
- 无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
- Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
- iptables只允许指定ip地址访问指定端口
- 深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
- 修改配置文件后tidb无法启动
- 统计学习方法——感知机
- Laravel5.1 Routing - routing packets
- Numpy -- epidemic data analysis case
猜你喜欢

Power of leetcode-231-2

Balanced binary tree (AVL)

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

Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition

Sysom case analysis: where is the missing memory| Dragon lizard Technology

Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?

Step by step monitoring platform ZABBIX

Numpy -- epidemic data analysis case

Plate - forme de surveillance par étapes zabbix

Statistical learning method -- perceptron
随机推荐
121. 买卖股票的最佳时机
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
[vulnhub range] thales:1
How does geojson data merge the boundaries of regions?
【知识小结】PHP使用svn笔记总结
Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
A JS script can be directly put into the browser to perform operations
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
分步式監控平臺zabbix
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
laravel中将session由文件保存改为数据库保存
企业级日志分析系统ELK
laravel构造函数和中间件执行顺序问题
Laravel changed the session from file saving to database saving
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
How to implement backspace in shell