当前位置:网站首页>Educational Codeforces Round 132 (Rated for Div. 2) E. XOR Tree
Educational Codeforces Round 132 (Rated for Div. 2) E. XOR Tree
2022-07-26 04:01:00 【jangyi.】
E. XOR Tree
The question :
Given a tree , Each node has a weight a [ i ] a[i] a[i], If the exclusive or sum of the weights on the simple path between any two nodes in the tree is not 0, Call this tree good . ask : At least modify the weights of several nodes ( Can be modified to any number ) Make the tree a good tree .
analysis :
Any one of them u − u- u−> v v v The exclusive or sum of paths of is s u m [ u ] ∧ s u m [ v ] ∧ a [ l c a ( u , v ) ] sum[u] \wedge sum[v] \wedge a[lca(u,v)] sum[u]∧sum[v]∧a[lca(u,v)], l c a ( u , v ) lca(u,v) lca(u,v) by u , v u,v u,v The latest public ancestor of . So if there is s u m [ u ] ∧ s u m [ v ] = a [ x ] , x = l c a ( u , v ) sum[u] \wedge sum[v]=a[x],x=lca(u,v) sum[u]∧sum[v]=a[x],x=lca(u,v), It's in x x x At least one node in the subtree of needs to be modified . Because the value that can be modified is arbitrary , So if you want to modify a node , Then the simple path that the node passes through can be ignored ( Directly when this subtree does not exist ).
Then it's easy for us to think , modify x x x This node is the best choice .
For each node x x x, Process its subtree first , If the XOR sum of two nodes in the subtree is equal to a [ x ] a[x] a[x], Then delete x x x node , answer +1. Otherwise, we will combine this subtree , Heuristic merging optimization is adopted in the merging process , It can reduce the complexity to l o g log log.
code:
int n, m, a[N];
int b[N], ans;
set<int> st[N];
vector<int> g[N];
void dfs(int u, int fa) {
b[u] = a[u];
if(fa != -1) b[u] ^= b[fa];
for(int i : g[u]) {
if(i != fa) {
dfs(i, u);
}
}
}
void cals(int u, int fa) {
bool f = 0;
st[u].insert(b[u]);
for(int i : g[u]) {
if(i != fa) {
cals(i, u);
if(st[u].size() < st[i].size()) swap(st[u], st[i]);
for(auto t : st[i]) f |= st[u].count(a[u] ^ t);
for(auto t : st[i]) st[u].insert(t);
}
}
if(f) {
ans++;
st[u].clear();
}
}
void solve() {
re(n);
for(int i = 0; i < n; i++) re(a[i]);
for(int i = 0; i < n - 1; i++) {
int x, y; re(x), re(y);
x--; y--;
g[x].pb(y); g[y].pb(x);
}
dfs(0, -1);
cals(0, -1);
cout << ans << '\n';
}
边栏推荐
- PHP 对象转换数组
- PHP < => spacecraft operator (combined comparator)
- Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
- Summary of senior report development experience: understand this and do not make bad reports
- Laravel8 implements interface authentication encapsulation using JWT
- [MCU simulation project] external interrupt 0 controls 8 LED flashes
- 想要做好软件测试,可以先了解AST、SCA和渗透测试
- 《opencv学习笔记》-- 边缘检测和canny算子、sobel算子、LapIacian 算子、scharr滤波器
- zk-SNARK:关于私钥、环签名、ZKKSP
- 座椅/安全配置升级 新款沃尔沃S90行政体验到位了吗
猜你喜欢

《opencv学习笔记》-- 霍夫变换

【读书笔记->数据分析】01 数据分析导论

开源许可证的传染性问题浅析

Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network

基本折线图:最直观呈现数据的趋势和变化

leetcode: 102. 二叉树的层序遍历

Kbpc1510-asemi large chip 15A rectifier bridge kbpc1510

ASEMI整流桥GBU1510参数,GBU1510规格,GBU1510封装

Supervit for deep learning

Wechat applet to realize music player (4) (use pubsubjs to realize inter page communication)
随机推荐
One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
php eval() 函数可以将一个字符串当做 php 代码来运行
Laravel8 implements interface authentication encapsulation using JWT
5 years, 1.4W times, NFT og's road to immortality Web3 column
Trust sums two numbers
Idea2020.3.1 cannot be opened (double click cannot be opened), but it can be opened through idea.bat.
Synchronous FIFO based on shift register
ASEMI整流桥GBU1510参数,GBU1510规格,GBU1510封装
[深入研究4G/5G/6G专题-42]: URLLC-13-《3GPP URLLC相关协议、规范、技术原理深度解读》-7-低延时技术-1-子载波间隔扩展
括号嵌套问题(建议收藏)
Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
What is the problem of the time series database that has been developed for 5 years?
深度学习之SuperViT
PHP installation spool supports dtls protocol
微信小程序实现音乐播放器(4)(使用pubsubjs实现页面间通信)
Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
PHP connects to MySQL database, and database connects to static tool classes to simplify the connection.
[Reading Notes - > data analysis] Introduction to BDA textbook data analysis
通用测试用例写作规范
Opencv learning notes -- Hough transform