当前位置:网站首页>Codeforces Round #245 (Div. 1) A (dfs)
Codeforces Round #245 (Div. 1) A (dfs)
2022-07-29 23:17:00 【Ask for a guide】
题目
题意: 给定一棵树,权值均为0或1.Now to perform as few operations as possible,Make the weight of each point in the initial tree become the corresponding weight.操作: XOR the weights of a node,and his grandson、Grandchildren's grandchildren all perform XOR operations.
思路: dfs,The number of layers of the current node and the number of flips of nodes at odd layers are additionally maintained、Even number of flips,If the current layer is odd and the odd layer node flips an odd number of times,He also needs to flip.O(1)计算值,Otherwise violence will get stuckO(n^2)
时间复杂度: O(n)
代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int n;
int a[N];
int b[N];
vector<int> va[N];
vector<int> res;
void dfs(int cur,int fa,int level,int l,int r) //奇数、偶数
{
if(level&1)
{
if(l&1) a[cur] ^= 1;
}
else
{
if(r&1) a[cur] ^= 1;
}
int ll = l,rr = r;
if(a[cur]!=b[cur])
{
if(level&1) ll++;
else rr++;
res.push_back(cur);
}
for(int i=0;i<va[cur].size();++i)
{
int j = va[cur][i];
if(j==fa) continue;
dfs(j,cur,level+1,ll,rr);
}
}
void solve()
{
cin>>n;
for(int i=0;i<n-1;++i)
{
int x,y; cin>>x>>y;
va[x].push_back(y),va[y].push_back(x);
}
for(int i=1;i<=n;++i) cin>>a[i];
for(int i=1;i<=n;++i) cin>>b[i];
dfs(1,0,0,0,0);
cout<<res.size()<<"\n";
for(int i=0;i<res.size();++i)
{
if(i) cout<<'\n';
cout<<res[i];
}
}
signed main(void)
{
solve();
return 0;
}
边栏推荐
- NC4 判断链表中是否有环
- 单片机ds1302时钟程序(51单片机液晶显示程序)
- 互联网基石:TCP/IP四层模型,由浅入深直击原理!
- JZ18 删除链表的节点
- 将文件流转成file文件后使用luckysheet回显数据
- The first round of the real offer harvester~ How does the big factory inspect the candidates?(with detailed answer)
- MQTT over QUIC:下一代物联网标准协议为消息传输场景注入新动力
- 云计算1+X之openstack篇
- 通信岗秋招准备
- 嵌入式系统驱动初级【1】——内核模块上_编译方法
猜你喜欢
随机推荐
【企业架构框架】是什么让 TOGAF 10 成为有价值的贡献
DNA修饰碳纳米管|DNA修饰单层二硫化钼|DNA修饰二硫化钨(注意事项)
【移动应用开发】2022/2023 年 8 大移动应用程序开发趋势
BGP Federal Comprehensive Experiment
cmd md命令
leetcode122. Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II(简单)
8万字带你入门Rust
真offer收割机 第二弹~大厂如何考察候选人?(附答案详解)
NC4 判断链表中是否有环
50. Leetcode 】 【 Pow (x, n) (medium) (power) quickly
MQTT over QUIC: The Next-Generation IoT Standard Protocol Brings New Impetus to Messaging Scenarios
Spark读取多目录
线性表之顺序表(干货满满的分享来啦~内含顺序表全部函数代码~
Codeforces Round #245 (Div. 1) A (dfs)
三、HikariCP源码分析之获取连接流程三
推荐 7 个学习 Web3 的开源资源
DNA修饰的上转换纳米材料|聚胞苷酸Poly-C DNA修饰的氧化石墨烯|解析说明
[leetcode] 82. Delete duplicate elements in sorted linked list II (medium)
新版微信小程序发布指南
微信小程序滑动导航栏(网页浮动窗口怎么设置)









