C. Xor-tree

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/430/problem/C

Description

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 node x. 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 ≤ n; ui ≠ 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 n integer numbers, the i-th number corresponds to goali (goali is either 0 or 1).

1000000000.

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 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

Sample Output

2
4
7

HINT

题意

给你一棵以1为根节点的树,然后每个点都是1或者0,

你可以使得一个点和他的儿子们全部都翻转
然后问你最少翻转多少次,且翻转的是哪些 点

题解:

很明显的一个dfs

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/* int buf[10];
inline void write(int i) {
int p = 0;if(i == 0) p++;
else while(i) {buf[p++] = i % 10;i /= 10;}
for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);
printf("\n");
}
*/
//**************************************************************************************
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge
{
int x,y,z;
};
vector<int> e[maxn];
void add_edge(int a,int b)
{
e[a].push_back(b);
}
int a[maxn];
int dp[maxn];
vector<int> ans;
int flag[maxn];
void dfs(int x,int c,int d)
{
if(flag[x])
return;
flag[x]=;
if(a[x]^c==dp[x])
{
for(int i=;i<e[x].size();i++)
{
int v=e[x][i];
dfs(v,d,c);
}
}
else
{
ans.push_back(x);
for(int i=;i<e[x].size();i++)
{
int v=e[x][i];
dfs(v,d,!c);
}
}
}
int main()
{
int n;
cin>>n;
for(int i=;i<n-;i++)
{
int u=read(),v=read();
add_edge(v,u);
add_edge(u,v);
}
for(int i=;i<=n;i++)
cin>>a[i];
for(int i=;i<=n;i++)
cin>>dp[i];
dfs(,,);
cout<<ans.size()<<endl;
for(int i=;i<ans.size();i++)
cout<<ans[i]<<endl;
}

Codeforces Round #245 (Div. 2) C. Xor-tree DFS的更多相关文章

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

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

  2. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  3. Codeforces Round #200 (Div. 1)D. Water Tree dfs序

    D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...

  4. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  5. Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树

    题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  6. 343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构

    D. Water Tree   Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each ...

  7. Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分

    D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...

  8. Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分

    D. Happy Tree Party     Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...

  9. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

    E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...

  10. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

随机推荐

  1. vscode中go插件配置

    # 转自:http://www.mamicode.com/info-detail-2436665.html # https://blog.csdn.net/bing2011/article/detai ...

  2. mysql 数据库备份及恢复

    1.备份 # 导出数据库:mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldump -–add-drop-table -uusername -ppassword da ...

  3. Codeforces 798D - Mike and distribution(二维贪心、(玄学)随机排列)

    题目链接:http://codeforces.com/problemset/problem/798/D 题目大意:从长度为n的序列A和序列B中分别选出k个下表相同的数要求,设这两个序列中k个数和分别为 ...

  4. PHP安全编程:register_globals的安全性

    如果你还能记起早期Web应用开发中使用C开发CGI程序的话,一定会对繁琐的表单处理深有体会.当PHP的register_globals配置选项打开时,复杂的原始表单处理不复存在,公用变量会自动建立.它 ...

  5. Linux文件系统的详解

    这里以 EXT2 文件系统为例 在Linux下,一个磁盘的最前面是MBR,大小为512Byte 在每一个分区下,第一部分是boot sector,接下来是super block,再接下来是inode, ...

  6. Python教程(一)Python简介

    Python就为我们提供了非常完善的基础代码库,覆盖了网络.文件.GUI.数据库.文本等大量内容,被形象地称作“内置电池(batteries included)”.用Python开发,许多功能不必从零 ...

  7. SaltStack的salt-ssh使用及LAMP状态设计部署(五)

    一.salt-ssh的使用 官方文档:https://docs.saltstack.com/en/2016.11/topics/ssh/index.html (1)安装salt-ssh [root@l ...

  8. poj1915 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=1915 题意 输入正方形棋盘的边长.起点和终点的位置,给定棋子的走法,输出最少经过多少步可以从起点走到终点. 思路 经典bfs题目. 代 ...

  9. linux 使用

    1. 请问怎样从文件里读内容到变量 var=(echo $(<aa)) ====>var=($(<aa)) $ var=($(<aa)) $ echo ${var[*]} /h ...

  10. 湖南大学ACM程序设计新生杯大赛(同步赛)J - Piglet treasure hunt Series 2

    题目描述 Once there was a pig, which was very fond of treasure hunting. One day, when it woke up, it fou ...