Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)
https://codeforces.com/contest/1118/problem/F1
#include<bits/stdc++.h>
using namespace std;
int n;
vector<int> color;
vector<vector<int> > tree;
int red=,blue=;
int ans=; pair<int,int> dfs(int v,int p=-){
int r=(color[v]==);
int b=(color[v]==);
for(int i=;i<tree[v].size();i++){
int u=tree[v][i];
if(u!=p){//避免回溯到上一个经过的点
pair<int,int> tmp=dfs(u,v);
ans+=(tmp.first==red&&tmp.second==);//只有之前经过的点中包含了全部的红或者蓝点才可以
ans+=(tmp.first==&&tmp.second==blue);
r+=tmp.first;//加上之前点中的红和蓝点
b+=tmp.second;
}
}
return make_pair(r,b);
} int main(){
cin>>n;
color.resize(n);
for(int i=;i<n;i++){
cin>>color[i];
if(color[i]==)
red+=;
else if(color[i]==)
blue+=;
}
tree.resize(n);
for(int i=;i<n-;i++){
int a,b;
cin>>a>>b;
a--;b--;
tree[a].push_back(b);
tree[b].push_back(a);
}
dfs();
cout<<ans<<endl;
return ;
}
Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)的更多相关文章
- 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 tes ...
- Codeforces 1118F1 Tree Cutting (Easy Version) (简单树形DP)
<题目链接> 题目大意: 给定一棵树,树上的点有0,1,2三中情况,0代表该点无色.现在需要你将这棵树割掉一些边,使得割掉每条边分割成的两部分均最多只含有一种颜色的点,即分割后的两部分不能 ...
- Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version) 水题
B1. Character Swap (Easy Version) This problem is different from the hard version. In this version U ...
- Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version)
This problem is different from the hard version. In this version Ujan makes exactly one exchange. Yo ...
- Codeforces Round #672 (Div. 2) C1. Pokémon Army (easy version) (DP)
题意:给你一组数\(a\),构造一个它的子序列\(b\),然后再求\(b_1-b2+b3-b4...\),问构造后的结果最大是多少. 题解:线性DP.我们用\(dp1[i]\)来表示在\(i\)位置, ...
- Codeforces Round #653 (Div. 3) E1. Reading Books (easy version) (贪心,模拟)
题意:有\(n\)本书,A和B都至少要从喜欢的书里面读\(k\)本书,如果一本书两人都喜欢的话,那么他们就可以一起读来节省时间,问最少多长时间两人都能够读完\(k\)本书. 题解:我们可以分\(3\) ...
- Codeforces Round #650 (Div. 3) F1. Flying Sort (Easy Version) (离散化,贪心)
题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\( ...
- Codeforces Round #658 (Div. 2) C1. Prefix Flip (Easy Version) (构造)
题意:给你两个长度为\(n\)的01串\(s\)和\(t\),可以选择\(s\)的前几位,取反然后反转,保证\(s\)总能通过不超过\(3n\)的操作得到\(t\),输出变换总数,和每次变换的位置. ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
随机推荐
- laravel中类似于thinkPHP中trace功能
答案来自https://segmentfault.com/q/1010000007716945 一楼: 到 https://packagist.org 上搜索你想要的关键词,比如查debugbar 列 ...
- Java并发集合(三)-ConcurrentHashMap分析和使用
1 http://ifeve.com/hashmap-concurrenthashmap-%E7%9B%B8%E4%BF%A1%E7%9C%8B%E5%AE%8C%E8%BF%99%E7%AF%87% ...
- Vs2015 c# 诊断工具查看程序的占用情况
windbg用着还不熟悉,dottrace 还要版权,着急查看程序的cpu 的使用情况,因为程序开启之后占用处理器资源较大,问题在哪里呢,于是点开了vs2015自带的诊断工具,以前偶尔打开过,没发现 ...
- yum 安装 php7 ,lamp ,以及安装swoole
1 更新yum yum update 2 安装依赖 yum -y install pcre* openssl* yum -y install gcc make gd-devel libjpeg-dev ...
- 探索未知种族之osg类生物---呼吸分解之事件循环一
事件循环和更新循环 终于到了我们嘴里经常念叨的事件循环.更新循环以及渲染循环了.首先我们来区分一下事件循环和渲染循环,他们两个首先是两个不同顺序执行的过程,我们有时候会用到任意node的updateC ...
- Linux入门命令1
查询及帮助 man查看命令帮助,命令的词典,显示Unix联机参考手册的页面 info从Info参考系统中显示文件 help查看Linux内置命令的帮助,比如cd命令. whatis 为指定命令显示一行 ...
- 高负载PHP调优
高负载PHP调优 针对PHP的Linux调优 调整文件描述符限制 # ulimit -n 1000000 # vi /etc/security/limits.conf # Setting Shell ...
- linux 虚拟机设置
1.可以用网桥方式 2.可以用一个回环虚拟网卡即可.
- 【Redis】Redis-benchmark测试Redis性能
Redis-benchmark是官方自带的Redis性能测试工具,可以有效的测试Redis服务的性能. 使用说明如下: Usage: redis-benchmark [-h <host>] ...
- oracle undo表空间
查询undo表空间状态 "Bytes(M)" FROM dba_undo_extents GROUP BY tablespace_name, status; Undo表空间的状态( ...