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, 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,布布扣,bubuko.com

时间: 2024-12-26 08:31:28

Codeforces Round #245 (Div. 1)——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 #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. 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

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 test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given an undirected tree of nn vertices. Some vert

Codeforces Round #629 (Div. 3) E. Tree Queries(lca题)

https://codeforces.com/contest/1328/problem/E E. Tree Queries You are given a rooted tree consisting of nn vertices numbered from 11 to nn. The root of the tree is a vertex number 11. A tree is a connected undirected graph with n−1n−1 edges. You are

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