codeforces C. Xor-tree
http://codeforces.com/problemset/problem/430/C
题意:在一棵上有n个节点,有n-1条边,在每一个节点上有一个值0或1,然后给你一个目标树,让你选择节点,然后把节点的值翻转,它的孙子节点跟着翻转,依次类推。。。问经过最少次数可以使这棵树变成目标树。
思路:利用异或的性质,任何数和0异或为它本身,从根节点dfs就行。
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#define maxn 200100
using namespace std; int n,k,x;
int a[maxn];
int b[maxn];
int num[maxn];
bool vis[maxn];
vector<int>g[maxn];
int ans[maxn];
int cnt;
void dfs(int c,int x,int y)
{
if((a[c]^x)!=b[c])
{
ans[cnt++]=c;
x=!x;
}
for(int i=; i<(int)g[c].size(); i++)
{
int v=g[c][i];
if(!vis[v])
{
vis[v]=true;
dfs(v,y,x);
}
}
} int main()
{
scanf("%d",&n);
for(int i=; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
for(int i=; i<=n; i++)
{
scanf("%d",&b[i]);
}
vis[]=true;
dfs(,,);
printf("%d\n",cnt);
for(int i=; i<cnt; i++)
{
printf("%d\n",ans[i]);
}
return ;
}
codeforces C. 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 ...
随机推荐
- 利用NPOI开源的读写Excel、WORD等微软OLE2组件读写execl,控制样式或单元格
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- android中listview的一些样式设置
在Android中,ListView是最常用的一个控件,在做UI设计的时候,很多人希望能够改变一下它的背景,使他能够符合整体的UI设计,改变背景背很简单只需要准备一张图片然后指定属性 android: ...
- Linux下配置SSL (转)
没有安装apache的情况: 首先安装SSL,再编译安装Apache,再配置证书即可 1.下载apache和openssl 网址:http://www.apache.org http://www.op ...
- StopWatch的用法
在学习spring的时候,看到关于统计时间的类,比较好奇,就记录下来,以便以后用到可以直接使用 org.springframework.util.StopWatch StopWatch该类在统计时间的 ...
- linux 上查找pid,筛选出来
ps -ef | grep httpd find / -name "1000sql.txt" 查找命令
- jQuery日期联动插件
此版本为网上的日期联动插件修改版,加入了修改后事件 /* * jQuery Date Selector Plugin * 日期联动选择插件 * * Demo: $("#calendar&qu ...
- MVC3.0 提交表单的方法
在views中使用 @using (Html.BeginForm()) 来完成,其中放一个submit类型的提交按钮,如: @using (Html.BeginForm()) { @Html.Vali ...
- Missing artifact com.sun:tools:jar:1.5.0
http://java-suddy.iteye.com/blog/1821871(解决了我的问题)
- 更新xcode后插件失效问题——不针对特定版本的通用解决方法
一.Xcode更新后插件失效的原理 1.每次更新Xcode后插件都会失效,其实插件都还在这个目录好好的躺着呢: ~/Library/Application Support/Developer/Shar ...
- 【Ural1057】幂和的数量
[题目描述] 写一个程序来计算区间[X,Y]内满足如下条件的整数个数:它恰好等于K个互不相等的B的整数幂之和. 举个例子.令X=15,Y=20,K=2,B=2.在这个例子中,区间[15,20]内有3个 ...