Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1
2 seconds
256 megabytes
standard input
standard output
You are given an undirected tree of nn vertices.
Some vertices are colored blue, some are colored red and some are uncolored. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.
You choose an edge and remove it from the tree. Tree falls apart into two connected components. Let's call an edge nice if neither of the resulting components contain vertices of both red and blue colors.
How many nice edges are there in the given tree?
The first line contains a single integer nn (2≤n≤3⋅1052≤n≤3⋅105) — the number of vertices in the tree.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤20≤ai≤2) — the colors of the vertices. ai=1ai=1 means that vertex ii is colored red, ai=2ai=2 means that vertex ii is colored blue and ai=0ai=0 means that vertex ii is uncolored.
The ii-th of the next n−1n−1 lines contains two integers vivi and uiui (1≤vi,ui≤n1≤vi,ui≤n, vi≠uivi≠ui) — the edges of the tree. It is guaranteed that the given edges form a tree. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.
Print a single integer — the number of nice edges in the given tree.
5
2 0 0 1 2
1 2
2 3
2 4
2 5
1
5
1 0 0 0 2
1 2
2 3
3 4
4 5
4
3
1 1 2
2 3
1 3
0
Here is the tree from the first example:

The only nice edge is edge (2,4)(2,4). Removing it makes the tree fall apart into components {4}{4} and {1,2,3,5}{1,2,3,5}. The first component only includes a red vertex and the second component includes blue vertices and uncolored vertices.
Here is the tree from the second example:

Every edge is nice in it.
Here is the tree from the third example:

Edge (1,3)(1,3) splits the into components {1}{1} and {3,2}{3,2}, the latter one includes both red and blue vertex, thus the edge isn't nice. Edge (2,3)(2,3) splits the into components {1,3}{1,3} and {2}{2}, the former one includes both red and blue vertex, thus the edge also isn't nice. So the answer is 0.
题意概括:
给出一棵无向树,每个结点有一种颜色(1红 2蓝 0无色),如果删掉某条边可以把这棵树分成两部分刚好各部分只具有一种颜色,则这种边为nice边,求最多有多少条。
解题思路:
哈哈哈,一开始试着BFS暴力了一波,果然水不过。
正解DFS,很明显先预处理出 每个结点为跟的子树所具有的两种颜色的结点个数 dd[ x ][ 1 ] && dd[ x ][ 2 ];
DFS一遍整棵树,判断当前边所分成的两部分是否满足条件,一部分就是预处理的子树部分,另一部分就是用树的根节点部分减去子树部分。
AC code:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 3e5+;
int color[MAXN];
int u[MAXN], v[MAXN];
int dd[MAXN][];
bool vis[MAXN];
int N, ans;
struct Edge
{
int v, nxt;
}edge[MAXN<<];
int head[MAXN], cnt; void init()
{
memset(head, -, sizeof(head));
cnt = ;
} void add(int from, int to)
{
edge[cnt].v = to;
edge[cnt].nxt = head[from];
head[from] = cnt++;
} void dfs1(int x, int fa)
{
int v;
if(color[x] == ) dd[x][]+=;
else if(color[x] == ) dd[x][]+=;
for(int i = head[x]; i != -; i = edge[i].nxt){
v = edge[i].v;
if(v == x || v == fa) continue;
dfs1(v, x);
dd[x][]+=dd[v][];
dd[x][]+=dd[v][];
}
} void dfs2(int x, int fa)
{
int v;
for(int i = head[x]; i != -; i = edge[i].nxt){
v = edge[i].v;
if(v == fa) continue;
if(dd[v][] == && (dd[][]-dd[v][]) == ){
// printf("u:%d v:%d\n", x, v);
ans++;
}
else if(dd[v][] == && (dd[][]-dd[v][]) == ){
// printf("u:%d v:%d\n", x, v);
ans++;
}
dfs2(v, x);
}
} int main()
{
init();
scanf("%d", &N);
for(int i = ; i <= N; i++){
scanf("%d", &color[i]);
}
for(int i = ; i < N; i++){
scanf("%d %d", &u[i], &v[i]);
add(u[i], v[i]);
add(v[i], u[i]);
} dfs1(, );
ans = ;
dfs2(, ); printf("%d\n", ans); return ;
}
Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】的更多相关文章
- Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)
https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...
- Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】
传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...
- Codeforces Round #650 (Div. 3) F1. Flying Sort (Easy Version) (离散化,贪心)
题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\( ...
- Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)【模拟】
一 题面 C2. Increasing Subsequence (hard version) 二 分析 需要思考清楚再写的一个题目,不能一看题目就上手,容易写错. 分以下几种情况: 1 左右两端数都小 ...
- Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array 【dp】
传送门:http://codeforces.com/contest/1105/problem/C C. Ayoub and Lost Array time limit per test 1 secon ...
- Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)【ABCD】
比赛链接:https://codeforces.com/contest/1445 A. Array Rearrangment 题意 给定两个大小均为 \(n\) 的升序数组 \(a\) 和 \(b\) ...
- 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 #263 (Div. 2) A. Appleman and Easy Task【地图型搜索/判断一个点四周‘o’的个数的奇偶】
A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 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 ...
随机推荐
- 【Hadoop系列】linux下 root用户免密码登录远程主机 ssh
SSH原理:[Hadoop系列]linux SSH原理解析 操作环境: CentOS 6.5 操作对象: 用户A主机和远程主机B 正文部分:斜体加粗代表linux指令. linux下 非root用户免 ...
- 静态代码块,构造代码块,main()
静态代码块 随Class 加载而加载,为Class 作初始化: 在main() 之前加载: 只执行一次: 构造代码块 随对象的创建而加载,为对象作初始化 public class day04 { pu ...
- 1.JDBC基础
JDBC全称Java Database Connectivity,即Java数据库连接.(以下以MySQL为例,使用MySQL语句) Sun公司提供了标准JDBC API接口,没有实现具体类.各个数据 ...
- Spring课程 Spring入门篇 5-6 introductions应用
1 解析 1.1 aop:declare-parents 标签简介 1.2 标签使用样式 2 代码演练 2.1 introductions标签应用 1 解析 1.1 aop:declare-paren ...
- Java线程面试题 Top 50 (个人总结)(转)
问答总结: 1. JDK1.5引入了哪些更高阶的并发工具 2. Java中CyclicBarrier 和 CountDownLatch有什么不同? CountDownLatch和CyclicBar ...
- CSS之after与before的content 和 attr 配合使用
content 和 attr 配合使用 如果你不想把content内容在CSS里写死,那你可以使用attr表达式来从页面元素中动态的获取内容: /* <div data-line="1 ...
- js权威指南学习笔记(一)类型、值和变量
1.数据类型:原始类型(primitive type) 和对象类型(object type) 原始类型包括数字.字符串和布尔值: 除数字.字符串.布尔值.null(空).undefined(未定义), ...
- PHP中empty、isset和is_null的使用区别
关于PHP中empty().isset() 和 is_null() 这三个函数的区别,之前记得专门总结过,上次又被问到,网上已经很多,就用几个例子来说明: 测试用例选取: <?php $a;$b ...
- Python爬虫教程-25-数据提取-BeautifulSoup4(三)
Python爬虫教程-25-数据提取-BeautifulSoup4(三) 本篇介绍 BeautifulSoup 中的 css 选择器 css 选择器 使用 soup.select 返回一个列表 通过标 ...
- 常用APDU指令错误码
状态码 性质 错误解释 9000 正常 成功执行 6200 警告 信息未提供 6281 警告 回送数据可能出错 6282 警告 文件长度小于Le 6283 警告 选中的文件无效 6284 警告 FCI ...