当前位置:网站首页>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';
}
边栏推荐
- 按键消抖的Verilog实现
- Multi merchant mall system function disassembly lecture 15 - platform side member label
- PHP <=> 太空船运算符(组合比较符)
- 【Unity3d Shader】角色投影与倒影
- 基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
- Basic line chart: the most intuitive presentation of data trends and changes
- Laravel8 implements interface authentication encapsulation using JWT
- [MCU simulation project] external interrupt 0 controls 8 LED flashes
- In PHP, you can use the abs() function to turn negative numbers into positive numbers
- Booking.com binke Shanghai noodles
猜你喜欢

研发了 5 年的时序数据库,到底要解决什么问题?

Analysis on the infectious problem of open source license

Supervit for deep learning

What is the problem of the time series database that has been developed for 5 years?

PHP connects to MySQL database, and database connects to static tool classes to simplify the connection.

Basic line chart: the most intuitive presentation of data trends and changes

第十八章:2位a~b进制中均位奇观探索,指定整数的 3x+1 转化过程,指定区间验证角谷猜想,探求4份黑洞数,验证3位黑洞数

How to build an enterprise level OLAP data engine for massive data and high real-time requirements?

Introduction to UFS CLK gate

One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
随机推荐
安装VMware报错failed to install the hcmon driver
Realization of online shopping mall system based on JSP
[MCU simulation project] external interrupt 0 controls 8 LED flashes
Kbpc1510-asemi large chip 15A rectifier bridge kbpc1510
[Reading Notes - > data analysis] Introduction to BDA textbook data analysis
Go Plus Security:一款Build Web3不可或缺的安全生态基础设施
2.9.4 Ext JS的布尔对象类型处理及便捷方法
Testing is not valued? Senior: you should think in another position
Wechat applet realizes music player (5)
JS Base64 encoding and decoding
redux
zk-SNARK:关于私钥、环签名、ZKKSP
电商运营小白,如何快速入门学习数据分析?
用GaussDB(for Redis)存画像,推荐业务轻松降本60%
【云原生】谈谈老牌消息中间件ActiveMQ的理解
E-commerce operator Xiaobai, how to get started quickly and learn data analysis?
[MCU simulation project] external interrupt 0 and 1 control two digit nixie tube to count
[cloud native] talk about the understanding of the old message middleware ActiveMQ
Six years of automated testing from scratch, I don't regret turning development to testing
[in depth study of 4g/5g/6g topic-42]: urllc-13 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -7-low delay technology-1-subcarrier spacing expansio