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)的更多相关文章

  1. 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 ...

  2. Codeforces 1118F1 Tree Cutting (Easy Version) (简单树形DP)

    <题目链接> 题目大意: 给定一棵树,树上的点有0,1,2三中情况,0代表该点无色.现在需要你将这棵树割掉一些边,使得割掉每条边分割成的两部分均最多只含有一种颜色的点,即分割后的两部分不能 ...

  3. 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 ...

  4. 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 ...

  5. Codeforces Round #672 (Div. 2) C1. Pokémon Army (easy version) (DP)

    题意:给你一组数\(a\),构造一个它的子序列\(b\),然后再求\(b_1-b2+b3-b4...\),问构造后的结果最大是多少. 题解:线性DP.我们用\(dp1[i]\)来表示在\(i\)位置, ...

  6. Codeforces Round #653 (Div. 3) E1. Reading Books (easy version) (贪心,模拟)

    题意:有\(n\)本书,A和B都至少要从喜欢的书里面读\(k\)本书,如果一本书两人都喜欢的话,那么他们就可以一起读来节省时间,问最少多长时间两人都能够读完\(k\)本书. 题解:我们可以分\(3\) ...

  7. Codeforces Round #650 (Div. 3) F1. Flying Sort (Easy Version) (离散化,贪心)

    题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\( ...

  8. Codeforces Round #658 (Div. 2) C1. Prefix Flip (Easy Version) (构造)

    题意:给你两个长度为\(n\)的01串\(s\)和\(t\),可以选择\(s\)的前几位,取反然后反转,保证\(s\)总能通过不超过\(3n\)的操作得到\(t\),输出变换总数,和每次变换的位置. ...

  9. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

随机推荐

  1. 安装mysql时启动服务出错问题

    mysql安装最后一步 无法启动服务错误 博客分类: IDE问题解析     今天安装mysql程序时候,在安装到最后一步时候,在最后一步却发现无法启动服务,出现这样的提示“cannot create ...

  2. c#特性attribute:(二)

    日志 初始化 特性类里边构造函数里的属性,带参数不带参数的 ******特性是编译时是不加到il 中的,是加到metadata中 ,本身对程序运行没有影响,除非我们主动的读取和使用区供反射可以使用. ...

  3. Liunx rm

    rm是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比如在/(根目录)下执行rm * -rf,这是一个危险的动作,“ * ”代表任意个字符).所以,我们在执行rm之 ...

  4. PAT 1008 数组元素循环右移问题 (20)(代码)

    1008 数组元素循环右移问题 (20)(20 分) 一个数组A中存有N(N&gt0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A ...

  5. Centos7上卸载openJdk安装,安装自己的JDK1.8

    1.下载jdk-1.8,官网下载1.8 注意:不要使用wget命令去下载jdk,若能够下载下来,解压的时候也会报错的: 2.卸载openjdk (1)使用  rpm -qa | grep java  ...

  6. Thrift.0

    0. Thrift的特性 1. 安装Thrift编译器 [Todo] http://thrift.apache.org/docs/install/ http://thrift.apache.org/d ...

  7. Startup.国外新锐公司及其技术Blog

    国外技术公司Tech/Engineering Blog 1. vimeo https://coderwall.com/team/vimeo http://blog.assembly.com/ 2. l ...

  8. JDK 规范目录

    JDK 规范目录 1.1 Java 异常处理 2.1 JDK 之 NIO 2 WatchService.WatchKey(监控文件变化) https://mp.weixin.qq.com/s/NIn2 ...

  9. Centos7 开启vsftpd

    Centos 1.开启DNS 可yum install (操作后要关闭) # vi /etc/resolv.conf# (INSERT)nameserver 8.8.8.8# (INSERT)表示按I ...

  10. [VBS]检测计算机各硬件信息

    1)批处理脚本:Rhea_HardwareInfoCollector.bat 调用VBScript脚本Rhea_HardwareInfoCollector.vbs,并将结果打印到文件Rhea_Resu ...