版权声明:本文为博主原创文章,未经博主同意不得转载。

https://blog.csdn.net/u012476429/article/details/25607945

题目链接

  • 题意:
    给一棵树n个节点,1为根节点。操作为,选定一个节点x。当前值取反,x的孙子,孙子的孙子。。

    均取反
    如今告诉初始时每一个点的值和最后每一个点的目标值。求操作次数最少时须要选择那些节点
    (1 ≤ n ≤ 105)

  • 分析:
    深度浅的点一定是受影响最小的(根节点仅仅受自己的影响)。所以从根依次向下递推处理就可以
const int MAXN = 110000;

VI G[MAXN], ans;
int now[MAXN], goal[MAXN]; void dfs(int u, int fa, int a, int b)
{
int rev = ((now[u] ^ a) != goal[u]);
if (rev)
{
ans.push_back(u);
a ^= 1;
}
REP(i, G[u].size())
{
int v = G[u][i];
if (v != fa)
dfs(v, u, b, a);
}
} int main()
{
// freopen("in.txt", "r", stdin);
int n, a, b;
while (~RI(n))
{
FE(i, 0, n) G[i].clear();
ans.clear(); REP(i, n - 1)
{
RII(a, b);
G[a].push_back(b);
G[b].push_back(a);
}
FE(i, 1, n) RI(now[i]);
FE(i, 1, n) RI(goal[i]);
dfs(1, 0, 0, 0);
WI(ans.size());
REP(i, ans.size())
WI(ans[i]);
}
return 0;
}

Codeforces Round #245 (Div. 1)——Xor-tree的更多相关文章

  1. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  2. Codeforces Round #245 (Div. 1)——Guess the Tree

    题目链接 题意: n个节点,给定每一个节点的子树(包含自己)的节点个数.每一个节点假设有子节点必定大于等于2.求这种数是否存在 n (1 ≤ n ≤ 24). 分析: 用类似DP的思路,从已知開始.这 ...

  3. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  4. Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)

    https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...

  5. Codeforces Round #353 (Div. 2) D. Tree Construction 模拟

    D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...

  6. Codeforces Round #245 (Div. 2) C. Xor-tree DFS

    C. Xor-tree Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...

  7. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

  8. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

  9. 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction

    Tree Construction Problem's Link ------------------------------------------------------------------- ...

  10. Codeforces Round #316 (Div. 2) D. Tree Requests dfs序

    D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

随机推荐

  1. golang https server分析

    https: HTTPS是http安全版本的实现,在http与tcp之间加了一层ssl/tls安全传输协议 为了防止请求被监听.篡改.冒充,在tls实现过程中引入了数字证书机制,数字证书由第三方权威机 ...

  2. SQLite学习手册(转)

    原文网址:http://www.cnblogs.com/stephen-liu74/archive/2012/01/22/2328757.html 在实际的应用中,SQLite作为目前最为流行的开源嵌 ...

  3. C++ 4种强制类型转换

    C++的四种强制类型转换为:static_cast.const_cast.reinterpret_cast和dynamic_cast 类型转换的一般形式:cast-name(expression); ...

  4. ctrl +z

    #bg 1 [1]+ /root/bin/rsync.sh & 用 jobs 命令查看正在运行的任务: #jobs [1]+ Running /root/bin/rsync.sh & ...

  5. 【Android Studio】之构建项目报错

    问题1: 报错: Could not download fastutil.jar (it.unimi.dsi:fastutil:7.2.0): No cached version available ...

  6. Oracle Tuning ( instance 级别 ) 01

    Shared Pool Tuning 目标是提高命中率, 以减少 I/O 操作 shared pool : 是由 library cache, data dictionary cache 两部分组成. ...

  7. XMLRPC 学习笔记(一)- Python 实现

    参考文章: http://baike.baidu.com/view/643379.htm http://docs.python.org/2/library/xmlrpclib.html http:// ...

  8. gibhub上搭建个人静态网站介绍

    之前学习过git的基本命令.今天介绍一下github上搭建个人网站的步骤. 在window系统上搭建gibhub个人网站(只能执行html.css和js文件),这就是纯静态页面. 步骤一:注册gith ...

  9. WPF实用知识点

    1.一个基本的WPF程序, 需要引入的程序集WindowsBase, PresentationCore, PresentationFramework using System; using Syste ...

  10. C# 各版本的新特性

    http://www.cnblogs.com/JeffreySun/archive/2012/11/14/2770211.html