CodeForces - 429A Xor-tree
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).
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.
Examples
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
2
4
7 题意:给出一棵树,然后这个树,然后每个节点有个值,然后再给一个目标树,要求把树翻转成目标树,翻转规则是,翻转当前节点,他的儿子不变
,他的儿子的儿子要翻转,求翻转次数 思路:因为他每翻一个下面的节点就要发生变化,翻多了节点后就不好判断当前要不要翻转,我就分别记录,奇数层的翻转次数,
和偶数层的翻转次数还有就是判断当前层的奇偶,然后进行dfs
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> mp[100001];
int c1[100001],c2[100001];
int n,x,y;
int num[100001];
int vis[100001];
int cnt;
void dfs(int x,int j,int o,int z,int d)
{
if(z==0)//第一个不同的点
{
if(c1[x]!=c2[x])
{
num[cnt++]=x;
d=1;
z=1;
j=1;
}
}
else{//判断奇偶与层数
d++;
if(c1[x]==c2[x]&&d%2==1&&j%2==1)
{
num[cnt++]=x;
j++;
}
else if(c1[x]!=c2[x]&&d%2==1&&j%2==0)
{
num[cnt++]=x;
j++;
}
else if(c1[x]==c2[x]&&d%2==0&&o%2==1)
{
num[cnt++]=x;
o++;
}
else if(c1[x]!=c2[x]&&d%2==0&&o%2==0)
{
num[cnt++]=x;
o++;
} }
for(int i=0;i<mp[x].size();i++)//往下dfs
{
if(vis[mp[x][i]]==0)
{
vis[mp[x][i]]=1;
dfs(mp[x][i],j,o,z,d);
}
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n-1;i++)//领接表建图
{
scanf("%d%d",&x,&y);
mp[x].push_back(y);
mp[y].push_back(x);
}
for(int i=1;i<=n;i++)
scanf("%d",&c1[i]);
for(int i=1;i<=n;i++)
scanf("%d",&c2[i]);
vis[1]=1;
dfs(1,0,0,0,0);
printf("%d\n",cnt);
for(int i=0;i<cnt;i++)
printf("%d\n",num[i]);
}
CodeForces - 429A Xor-tree的更多相关文章
- Problem - D - Codeforces Fix a Tree
Problem - D - Codeforces Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...
- [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)
[多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...
- 「AGC035C」 Skolem XOR Tree
「AGC035C」 Skolem XOR Tree 感觉有那么一点点上道了? 首先对于一个 \(n\),若 \(n\equiv 3 \pmod 4\),我们很快能够构造出一个合法解如 \(n,n-1, ...
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- Codeforces 765 E. Tree Folding
题目链接:http://codeforces.com/problemset/problem/765/E $DFS子$树进行$DP$ 大概分以下几种情况: 1.为叶子,直接返回. 2.长度不同的路径长度 ...
- Codeforces 932.D Tree
D. Tree time limit per test 2 seconds memory limit per test 512 megabytes input standard input outpu ...
- 【思维题 状压dp】APC001F - XOR Tree
可能算是道中规中矩的套路题吧…… Time limit : 2sec / Memory limit : 256MB Problem Statement You are given a tree wit ...
- AtCoder - 3913 XOR Tree
Problem Statement You are given a tree with N vertices. The vertices are numbered 0 through N−1, and ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- CodeForces 383C Propagating tree
Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
随机推荐
- android -------- ConstraintLayout Group和goneMargin(五)
前面的文章 ConstraintLayout 介绍 (一) ConstraintLayout约束属性 (二) ConstraintLayout 宽高比和偏移量比(三) ConstraintLayout ...
- Fiddler抓取https数据包
Wireshark和Fiddler的优缺点: ①Wireshark是一种在网络层上工作的抓包工具,不仅自带大量的协议分析器,而且可以通过编写Wireshark插件来识别自定义的协议.虽然Wiresha ...
- react中创建组件
第1种 - 创建组件的方式 > 使用构造函数来创建组件,如果要接收外界传递的数据,需要在 构造函数的参数列表中使用`props`来接收:> 必须要向外return一个合法的JSX创建的虚拟 ...
- 77. Combinations (java 求C(n,k)的组合,排除重复元素)
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 解析:同求全 ...
- 浅谈cookie、session
揭秘Cookie: cookie说的直白点就是保存在用户浏览器端的一个键值对,举个例子,你现在登录了京东商城,你把浏览器关闭之后,你再打开京东,你还是可以对你的账户继续操作,已经购买的商品,订单都是可 ...
- 2018 Benelux Algorithm Programming Contest (BAPC 18)I In Case of an Invasion, Please. . .
题意:一副无向有权图,每个点有一些人,某些点是避难所(有容量),所有人要去避难所,问最小时间所有人都到达避难所, 题解:dij+二分+最大流check,注意到避难所最多10个,先挨个dij求到避难所的 ...
- P4238 【模板】多项式求逆 ntt
题意:求多项式的逆 题解:多项式最高次项叫度deg,假设我们对于多项式\(A(x)*B(x)\equiv 1\),已知A,求B 假设度为n-1,\(A(x)*B(x)\equiv 1(mod x^{\ ...
- mybatis调用存储过程的两种方式
先总结和说明一下注意点: 1.如果传入的某个参数可能为空,必须指定jdbcType 2.当传入map作为参数时,必须指定JavaType 3.如果做动态查询(参数为表名,sql关键词),可以使用${} ...
- EF - Database First 开发方式
概述 Database First 又叫数据库优先的开发方式,是一种比较旧的开发方式,现在越来越多的企业已经不再使用此种开发方式. 当然,对于一些旧项目进行升级,在已经有了数据库的情况下,使用此种方式 ...
- docker实战系列之快速删除docker中的容器
1.停用全部运行中的容器 docker stop $(docker ps -q) 2.删除全部容器 docker rm $(docker ps -aq) 3.停用并删除容器 docker stop $ ...