Codeforces Round #245 (Div. 1)A. Xor-tree(深搜)

传送门

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 xflips, 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. cells into walls so that all the remaining cells still formed a connected area. Help him.

Sample Input

102 13 14 25 16 27 58 69 810 51 0 1 1 0 1 0 1 0 11 0 1 0 0 1 1 1 0 1

Sample Output

247

思路

题意:

给出一棵树,有n个节点,根为1,每个节点的值不是0就是1,每次可以翻转一个节点,翻转后,当前节点值翻转,儿子节点值不变,儿子的儿子翻转,依次类推,求最少翻转次数,使得每个节点的值与目标结果相同。

题解:

由于翻转一个节点,其子节点有的会改变,因此贪心的从根开始搜索,如果当前节点与目标不同,那么一定要翻转,因为只影响子节点,因此其父节点已经到达目标结果将不受影响。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int init[maxn],goal[maxn];
vector<int>ans,itv[maxn];

void dfs(int u,int fa,int odd,int even)
{
	if (odd)	init[u] ^= 1;
	if (init[u] != goal[u])
	{
		odd ^= 1;
		ans.push_back(u);
	}
	int size = itv[u].size();
	for (int i = 0;i < size;i++)
	{
		int v = itv[u][i];
		if (fa == v)	continue;  //树是双向的,因此此处保证从根单向搜索
		dfs(v,u,even,odd);
	}
}

int main()
{
	int n,u,v;
	scanf("%d",&n);
	for (int i = 1;i < n;i++)
	{
		scanf("%d%d",&u,&v);
		itv[u].push_back(v);
		itv[v].push_back(u);
	}
	for (int i = 1;i <= n;i++)	scanf("%d",&init[i]);
	for (int i = 1;i <= n;i++)	scanf("%d",&goal[i]);
	dfs(1,1,0,0);
	int size = ans.size();
	printf("%d\n",size);
	for (int i = 0;i < size;i++)	printf("%d\n",ans[i]);
	return 0;
}

  

时间: 2024-08-02 20:36:18

Codeforces Round #245 (Div. 1)A. Xor-tree(深搜)的相关文章

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

本文出自:http://blog.csdn.net/svitter 实验环境:Myeclipse10 + tomcat7.0 有时间会写windows和linux下的tomcat配置,现在时间有限,暂且不写了..有些东西也是没有理解透彻. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <%@ page language="java" contentType="

Codeforces Round #222 (Div. 1)A. Maze(深搜)

传送门 Description Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side. Pavel drew a grid maze with all emp

Codeforces Round #321 (Div. 2) Kefa and Park 深搜

原题链接: 题意: 给你一棵有根树,某些节点的权值是1,其他的是0,问你从根到叶子节点的权值和不超过m的路径有多少条. 题解: 直接dfs一下就好了. 代码: #include<iostream> #include<cstring> #include<vector> #include<algorithm> #define MAX_N 100005 using namespace std; vector<int> G[MAX_N]; int n,m

Codeforces Round #245 (Div. 2)

A Points and Segments (easy) 智商题,(智商捉急~) /*********************************************************** *分析:只要按Xi从小到大染成1010101010... , *1.0间隔的的序列就能保证对于任意区间[l, r]中1的个数和0的个数之差小于等于1. *注意:由于输入的Xi可能是无序的,所有要两次排序处理. *******************************************

Codeforces Round #245 (Div. 1)——Tricky Function

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

Codeforces Round #245 (Div. 1)——Working out

题目链接 题意: 一个n*m的矩阵,每个方格有一个非负数,现在选择两条线路:一个左上到右下,一个左下到右上,且只能有一个公共点.求两个线路上数的最大值(公共点不算) 分析: 只有两种情况,dp即可.记两个线路为1和2,考虑一个公共点,1为左进右出,2为下进上出:1上进下出,2为左进右出 const int MAXN = 1005; int lu[MAXN][MAXN], ld[MAXN][MAXN]; int ru[MAXN][MAXN], rd[MAXN][MAXN]; int ipt[MAX

Codeforces Round #245 (Div. 1)——Xor-tree

题目链接 题意: 给一棵树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

Codeforces Round #245 (Div. 2) A - Points and Segments (easy)

水到家了 #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Point{ int index, pos; Point(int index_ = 0, int pos_ = 0){ index = index_; pos = pos_; } bool operator < (const Point& a) const{ return p

Codeforces Round #245 (Div. 2) B - Balls Game

暴利搜索即可 #include <iostream> #include <vector> #include <iostream> using namespace std; int main(){ int n,k,x; cin >> n >> k >> x; vector<int> c(n); for(int i = 0 ; i < n; ++ i) cin >> c[i]; int ans = 0; fo