当前位置:网站首页>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:00:00 【jangyi.】
E. XOR Tree
题意:
给定一棵树,每个结点有权值 a [ i ] a[i] a[i],如果树上任意两个结点之间的简单路径上的权值异或和不为0,则称这颗树是好的。问:至少修改几个结点的权值(可修改为任意数)使该树是一颗好树。
分析:
任意一条 u − u- u−> v v v的路径的异或和为 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)为 u , v u,v u,v的最近公共祖先。因此如果存在 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),则说明在 x x x的子树中至少有一个结点需要修改。因为可以修改的值是任意的,所以如果要修改某个结点,那么该结点所经过的简单路径可以不用再考虑(直接当这个子树不存在)。
那么我们容易想到,修改 x x x这个结点是最优的选择。
对于每个结点 x x x,先处理其子树,如果子树中存在两个结点的异或和等于 a [ x ] a[x] a[x],则直接删除 x x x结点,答案+1。否则我们就将这个子树合起来,合并过程中采用启发式合并优化,可以使复杂度降到 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';
}
边栏推荐
- ACM mm 2022 | end to end multi granularity comparative learning for video text retrieval
- 【单片机仿真项目】外部中断0和1控制两位数码管进行计数
- 微信小程序实现音乐播放器(4)(使用pubsubjs实现页面间通信)
- 在 Istio 服务网格内连接外部 MySQL 数据库
- Three ways of redis cluster
- PHP implements the algorithm of adding from 1 to 100
- One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
- 基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
- Leetcode: 102. Sequence traversal of binary tree
- 【读书笔记->数据分析】BDA教材《数据分析》书籍介绍
猜你喜欢

Advanced content of MySQL -- three MySQL logs that must be understood binlog, redo log and undo log

Can't the container run? The Internet doesn't have to carry the blame

Asemi rectifier bridge gbu1510 parameters, gbu1510 specifications, gbu1510 package

测试工作不受重视?学长:你应该换位思考

括号嵌套问题(建议收藏)

Zkevm: summary of zkevm and L1 by Mina's CEO

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

加班一周开发了报表系统,这个低代码免费IT报表神器太好用了

Bracket nesting problem (recommended Collection)

《opencv学习笔记》-- 重映射
随机推荐
基于SSM选课信息管理系统
Opencv learning notes - remapping
The second article, which is still unfinished, will be introduced again, and continue to explain oracledb_ Exporter monitors Oracle, a very low intrusive monitoring scheme.
STM32状态机编程实例——全自动洗衣机(下)
1311_硬件设计_ICT概念、应用以及优缺点学习小结
MySQL索引失效场景以及解决方案
[cloud native] talk about the understanding of the old message middleware ActiveMQ
用GaussDB(for Redis)存画像,推荐业务轻松降本60%
WAF details
Operator new, operator delete supplementary handouts
第十八章:2位a~b进制中均位奇观探索,指定整数的 3x+1 转化过程,指定区间验证角谷猜想,探求4份黑洞数,验证3位黑洞数
Find My技术|物联网资产跟踪市场规模达66亿美元,Find My助力市场发展
General test case writing specification
PHP 对象转换数组
Why are more and more users of Bing search?
booking.com缤客上海面经
在 Istio 服务网格内连接外部 MySQL 数据库
Brief tutorial for soft exam system architecture designer | case analysis and problem solving skills
STM32 state machine programming example - full automatic washing machine (Part 2)
How does redis implement persistence? Explain the AOF trigger mechanism and its advantages and disadvantages in detail, and take you to quickly master AOF