当前位置:网站首页>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';
}
边栏推荐
- Laravel8 implements interface authentication encapsulation using JWT
- Summary of senior report development experience: understand this and do not make bad reports
- Apple removed the last Intel chip from its products
- Three ways of redis cluster
- UFS Clk Gate介绍
- 基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
- [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
- Lua and go mixed call debugging records support cross platform (implemented through C and luajit)
- Go Plus Security:一款Build Web3不可或缺的安全生态基础设施
- 1311_硬件设计_ICT概念、应用以及优缺点学习小结
猜你喜欢

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

Sentinel fusing and current limiting

2021 CIKM |GF-VAE: A Flow-based Variational Autoencoder for Molecule Generation

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

如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?

ZK snark: about private key, ring signature, zkksp

(翻译)按钮位置约定能强化用户使用习惯

zk-SNARK:关于私钥、环签名、ZKKSP

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

基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
随机推荐
Sentinel fusing and current limiting
Basic line chart: the most intuitive presentation of data trends and changes
Advanced content of MySQL -- three MySQL logs that must be understood binlog, redo log and undo log
(翻译)按钮位置约定能强化用户使用习惯
JS Base64 encoding and decoding
cpu和gpu已过时,npu和apu的时代开始
2021 CIKM |GF-VAE: A Flow-based Variational Autoencoder for Molecule Generation
[cloud native] talk about the understanding of the old message middleware ActiveMQ
容器跑不动?网络可不背锅
What is the problem of the time series database that has been developed for 5 years?
[digital ic/fpga] Hot unique code detection
php中可以使用取绝对值函数 abs() 将负数转成正数
[cloud native kubernetes] how to use configmap under kubernetes cluster
Pits encountered by sdl2 OpenGL
The convolution kernel is expanded to 51x51, and the new CNN architecture slak counterattacks the transformer
JS upload avatar (you can understand it after reading it, trust me)
某大厂开发和测试干了一架,还用鼠标线勒脖子...
想要做好软件测试,可以先了解AST、SCA和渗透测试
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.
General test case writing specification