Codeforces Round #245 (Div. 2)-C. Xor-tree
来源:互联网 发布:收购网络域名诈骗保护 编辑:程序博客网 时间:2024/06/02 06:42
原题链接
节点初始标志记在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 <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <map>#include <vector>#include <queue>#define maxn 100005#define MOD 1000000007#define INF 1e9using namespace std;typedef long long ll;vector<int> v[maxn], ans;int d1[maxn], d2[maxn];void dfs(int j, int f, int k1, int k2, int h){if(k1 && (h&1)){d1[j] ^= 1;}if(k2 && !(h&1)){d1[j] ^= 1;}if(d1[j] != d2[j]){if(h&1) k1 ^= 1;else k2 ^= 1;ans.push_back(j);}for(int i = 0; i < v[j].size(); i++){int e = v[j][i];if(e != f){dfs(e, j, k1, k2, h + 1);}}}int main(){//freopen("in.txt", "r", stdin);int n, a, b;scanf("%d", &n);for(int i = 0; i < n - 1; i++){ scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } for(int i = 1; i <= n; i++) scanf("%d", d1+i); for(int i = 1; i <= n; i++) scanf("%d", d2+i); dfs(1, -1, 0, 0, 0); printf("%d\n", ans.size()); for(int i = 0; i < ans.size(); i++) printf("%d\n", ans[i]); return 0;}
0 0
- Codeforces Round #245 (Div. 2)-C. Xor-tree
- Codeforces Round #245 (Div. 2) C. Xor-tree DFS
- Codeforces Round #245 (Div. 1) A /(Div.2 C) Xor-tree
- Codeforces #245 (Div. 2)C. Xor-tree(DFS&&贪心
- Codeforces Round #245 (Div. 1)——Xor-tree
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor(xor运算应用)
- Codeforces Round #247 (Div. 2) C. k-Tree
- Codeforces Round #247 (Div. 2) C.K-Tree
- Codeforces Round #247 (Div. 2) C k-Tree
- Codeforces Round #247(Div. 2) C. k-Tree DP
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree
- Codeforces Round #358 (Div. 2)C. Alyona and the Tree
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree
- Codeforces Round #369 (Div. 2) 711C Coloring Tree (DP)
- Codeforces Round #395 (Div. 2) C. Timofey and a tree
- Codeforces Round #395 (Div. 2) C. Timofey and a tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- c通过脚本实现操作
- shell 与 sqlplus 交互
- 深入浅出JMS(一)--JMS基本概念
- Google 的开源技术protobuf 简介与例子
- 安装Nexus
- Codeforces Round #245 (Div. 2)-C. Xor-tree
- hdu1002 大大大整数加法 A + B Problem II
- 程序员如何提高工作效率?我是这样做的
- Linux crontab 命令格式与详细例子
- 14.Laravel取出mysql数据全部被转成string类型问题
- 调用接口查询天气
- 理解jquery的$.extend()、$.fn和$.fn.extend()
- 最长公共子序列(LCS)
- 深入浅出JMS(二)--ActiveMQ简单介绍以及安装