下来的第一次相遇是在不翻盖的同一节点,递归可以是....

A. Xor-tree
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Iahub is very proud of his recent discovery, propagating trees. Right now, he invented a new tree, called xor-tree. After this new revolutionary discovery, he invented a game for kids which uses xor-trees.

The game is played on a tree having n nodes, numbered from 1 to n.
Each node i has an initial value initi,
which is either 0 or 1. The root of the tree is node 1.

One can perform several (possibly, zero) operations on the tree during the game. The only available type of operation is to pick a nodex. Right after someone
has picked node x, the value of node x flips, the
values of sons of x remain the same, the values of sons of sons of x flips,
the values of sons of sons of sons of x remain the same and so on.

The goal of the game is to get each node i to have value goali,
which can also be only 0 or 1. You need to reach the goal of the game by using minimum number of operations.

Input

The first line contains an integer n (1 ≤ n ≤ 105).
Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi)
meaning there is an edge between nodes ui and vi.

The next line contains n integer numbers, the i-th
of them corresponds to initi (initi is
either 0 or 1). The following line also contains ninteger numbers, the i-th
number corresponds to goali (goali is
either 0 or 1).

Output

In the first line output an integer number cnt, representing the minimal number of operations you perform. Each of the next cnt lines
should contain an integer xi,
representing that you pick a node xi.

Sample test(s)
input
10
2 1
3 1
4 2
5 1
6 2
7 5
8 6
9 8
10 5
1 0 1 1 0 1 0 1 0 1
1 0 1 0 0 1 1 1 0 1
output
2
4
7

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; int n,init[110000],goal[110000];
vector<int> g[110000],ans; void dfs(int u,int fa,int c1,int c2)
{
if(c1) init[u]^=1;
if(init[u]!=goal[u])
{
c1^=1; ans.push_back(u);
}
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(v==fa) continue;
dfs(v,u,c2,c1);
}
} int main()
{
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
g[0].push_back(1);
for(int i=1;i<=n;i++)
scanf("%d",init+i);
for(int i=1;i<=n;i++)
scanf("%d",goal+i);
dfs(0,0,0,0);
printf("%d\n",(int)ans.size());
for(int i=0;i<ans.size();i++)
printf("%d\n",ans[i]);
return 0;
}

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

Codeforces 429 A. Xor-tree的更多相关文章

  1. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  2. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

  3. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

  4. Codeforces 429 B. Working out-dp( Codeforces Round #245 (Div. 1))

    B. Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  5. Codeforces A. Game on Tree(期望dfs)

    题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)

    [多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...

  7. 「AGC035C」 Skolem XOR Tree

    「AGC035C」 Skolem XOR Tree 感觉有那么一点点上道了? 首先对于一个 \(n\),若 \(n\equiv 3 \pmod 4\),我们很快能够构造出一个合法解如 \(n,n-1, ...

  8. Codeforces Round #781(C. Tree Infection)

    Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...

  9. Codeforces 242E:XOR on Segment(位上的线段树)

    http://codeforces.com/problemset/problem/242/E 题意:给出初始n个数,还有m个操作,操作一种是区间求和,一种是区间xor x. 思路:昨天比赛出的一道类似 ...

  10. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

随机推荐

  1. Nginx + unicorn 运行多个Rails应用程序

    PS:第一次写的很详细,可惜发布失败,然后全没了,这是第二次,表示只贴代码,剩下的自己领悟好了,这就是所谓的一鼓作气再而衰吧,希望没有第三次. 版本: ruby 2.1.0 rails 4.0.2 n ...

  2. Eclipse+Maven创webapp工程

    1.开启eclipse,右键new-->other,例如以下图找到maven project. 选择maven project,点击next 2.选择maven project后.显示创建mav ...

  3. OpenGL路(四)自制的图形功能(立方体、汽缸、圆锥)

    #include <gl/glut.h> #include <gl/GLU.h> #include <gl/GL.h> #pragma comment(lib, & ...

  4. uva10067 Playing with Wheels 【建图+最短路】

    题目:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1008">uva10067 Play ...

  5. 采用xshell链路本地虚拟机Linux

    昨天想安装在自己的机器看Linux.而使用xshell通路.但是这花了很长的时间,于xshell结束所有的提示"Could not connect to '192.168.54.100' ( ...

  6. Entity Framework 6.1

    Entity Framework 6.1-Code First Code First-代码优先,先创建好领域模型.根据... 2014-04-21 14:56 阅读(6858) 评论(0)   Ent ...

  7. C语言优化实例:为了消除嵌套switch-case聪明的做法

    我们有可能会写出或者遇到类似这种代码: C/C++ switch (expr1) { case label11: switch (expr2) { case label21: // do someth ...

  8. java 不寻常的问题 No bean named &#39;sessionFactory&#39; is defined 和 initialize a collection of role

    左右java的"No bean named 'sessionFactory' is defined "  现在经常出去SHH或在其框架内Sping+JPA使用底部HIbernate ...

  9. win7通过配置怎么样ODBC数据源

    我的系统是win7最终.找了半天也没有找到控制面板上的卷管理工具--数据源.后来在网上搜索后发现win7事实并非如此开放ODBC数据源管理,打开ODBC数据源管理方法是cmd输入命令框odbcad32 ...

  10. Ambari-部署文档

    Ambari-server搭建过程 部署环境要求 操作系统:centos 5 或 centos 6 能够使用yum jdk版本号 1.7 官网安装教程 https://cwiki.apache.org ...