Codeforces Round #245 (Div. 2) C. Xor-tree DFS
来源:互联网 发布:程序员出差多久 编辑:程序博客网 时间:2024/05/18 09:41
题目链接:
http://codeforces.com/contest/430/problem/C
题意:
一棵以1为根节点的树,每个点都是1或者0,
选定一个节点x,当前值取反,x的孙子,孙子的孙子。。。均取反
然后问你最少翻转多少次可以到达目标位置,且翻转的是哪些 点
题解一:
深度浅的点一定是受影响最小的(根节点只受自己的影响),所以从根依次向下递推处理
代码一:
#include <bits/stdc++.h>using namespace std;typedef long long ll;#define MS(a) memset(a,0,sizeof(a))#define MP make_pair#define PB push_backconst int INF = 0x3f3f3f3f;const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;inline ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}//////////////////////////////////////////////////////////////////////////const int maxn = 1e5+10;std::vector<int> G[maxn],ans;int n;int s[maxn],e[maxn];void dfs(int x,int f,int s1, int s2){ // s1表示当前点是否翻转,s2表示当前点的所有儿子们是否翻转 if(s[x]^s1 != e[x]){ ans.PB(x); s1 ^= 1; // 如果不行, 必转 } for(auto v : G[x]){ if(v!=f) dfs(v,x,s2,s1); // 走到下一层, 因为当前层的上一层的翻转状态和下一层的一样 }}int main(){ n = read(); for(int i=1; i<n; i++){ int u=read(),v=read(); G[u].PB(v); G[v].PB(u); } for(int i=1; i<=n; i++) s[i] = read(); for(int i=1; i<=n; i++) e[i] = read(); dfs(1,-1,0,0); cout << ans.size() << endl; for(auto x : ans) cout << x << endl; return 0;}
题解二:
http://blog.csdn.net/qq_26122039/article/details/52914241
节点初始标志记在d1[]中,节点目标标志记在d2[]中,k1为1表示奇数节点标志改变,k2为1表示偶数节点标志改变.h表示节点深度
从根节点开始深搜,遇到节点j先通过k1, k2改变d1[j]的值,改变后若d1[j] != d2[j]则d1[j] ^= 1, 同时若该节点深度h为奇数,则该节点孩子中奇数深度的节点都要改变,所以k1 ^= 1,同理若h为偶数,则该节点孩子中偶数深度的节点都要改变,所以k2 ^= 1;
代码二:
#include <bits/stdc++.h>using namespace std;typedef long long ll;#define MS(a) memset(a,0,sizeof(a))#define MP make_pair#define PB push_backconst int INF = 0x3f3f3f3f;const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;inline ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}//////////////////////////////////////////////////////////////////////////const int maxn = 1e5+10;std::vector<int> G[maxn],ans;int n;int s[maxn],e[maxn];void dfs(int x,int f,int k1,int k2,int d){ if(k1 && (d&1)){ s[x] ^= 1; } if(k2 && !(d&1)){ s[x] ^= 1; } if(s[x] != e[x]){ ans.PB(x); if(d&1) k1 ^= 1; else k2 ^= 1; s[x] ^= 1; } for(auto v : G[x]){ if(v!=f) dfs(v,x,k1,k2,d+1); }}int main(){ n = read(); for(int i=1; i<n; i++){ int u=read(),v=read(); G[u].PB(v); G[v].PB(u); } for(int i=1; i<=n; i++) s[i] = read(); for(int i=1; i<=n; i++) e[i] = read(); dfs(1,-1,0,0,0); cout << ans.size() << endl; for(auto i : ans) cout << i << endl; return 0;}
今天打了BC#92 看题解才知道auto的用法= =
0 0
- Codeforces Round #245 (Div. 2) C. Xor-tree DFS
- Codeforces #245 (Div. 2)C. Xor-tree(DFS&&贪心
- Codeforces Round #245 (Div. 2)-C. Xor-tree
- Codeforces Round #245 (Div. 1) A /(Div.2 C) Xor-tree
- Codeforces Round 245 div1A&div2C Xor-tree dfs暴搜
- Codeforces Round #245 (Div. 1)——Xor-tree
- Codeforces Round #301 C (Div. 2) 【dfs】
- Codeforces Round #359 (Div. 2) C DFS
- Codeforces Round #321 (Div. 2) C DFS
- Codeforces Round #376 (Div. 2)C(DFS)
- CodeForces 430C Xor-tree(DFS)
- Codeforces 430C Xor-tree【Dfs+思维】
- Codeforces 430C Xor-Tree DFS,奇偶性
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree (DFS)
- Codeforces Round #358 (Div. 2) -- C. Alyona and the Tree (树上的DFS)
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree (二叉树+DFS)
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree(DFS)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs)
- 通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
- MINA、Netty、Twisted一起学(八):HTTP服务器
- zookeeper伪分布式集群搭建
- 编程学习小结(2)编程需要哪些基础知识
- SVN更新时,校验和不匹配
- Codeforces Round #245 (Div. 2) C. Xor-tree DFS
- elasticsearch的_all和_source
- MINA、Netty、Twisted一起学(九):异步IO和回调函数
- VMware虚拟网络配置及原理
- java b2b2c商城系统
- php socket编程实现客服机器人原理
- MINA、Netty、Twisted一起学(十):线程模型
- 最近get到的一些新的点
- Codeforces 268B Two Sets