任意门:http://codeforces.com/contest/1118/problem/F1

F1. Tree Cutting (Easy Version)
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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?

Input

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.

Output

Print a single integer — the number of nice edges in the given tree.

Examples
input

Copy
5
2 0 0 1 2
1 2
2 3
2 4
2 5
output

Copy
1
input

Copy
5
1 0 0 0 2
1 2
2 3
3 4
4 5
output

Copy
4
input

Copy
3
1 1 2
2 3
1 3
output

Copy
0
Note

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

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

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

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

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

  4. Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)【模拟】

    一 题面 C2. Increasing Subsequence (hard version) 二 分析 需要思考清楚再写的一个题目,不能一看题目就上手,容易写错. 分以下几种情况: 1 左右两端数都小 ...

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

  6. Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)【ABCD】

    比赛链接:https://codeforces.com/contest/1445 A. Array Rearrangment 题意 给定两个大小均为 \(n\) 的升序数组 \(a\) 和 \(b\) ...

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

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

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

随机推荐

  1. Java之Poi导出Excel文档

    一.Poi简介 在后台管理系统中,我们经常要做的导出操作,通常导出为Excel文档的形式,而Poi则提供了这种需要的支持. 二.Workbook/HSSFWorkbook/XSSFWorkbook 1 ...

  2. 会话技术Cookie

    1.会话技术 1>什么是会话技术: 从打开一个浏览器访问某个站点,到关闭这个浏览器的整个过程,成为一次会话. 2>作用: 会话技术就是记录这次会话中客户端的状态与数据的. Cookie:数 ...

  3. 第4章 css文字text与字体font-face

    text-overflow 与 word-wrap text-overflow:用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法: 但是text-overflow只是用来说明文字溢出 ...

  4. Python 爬虫的集中简单方式

  5. Live2D 博客页面添加板娘

    偶然看到了live2d,神奇的二次元呀 在页脚Html代码中添加如下代码即可: <link rel="stylesheet" type="text/css" ...

  6. springmvc封装list个数限制问题

    提交一颗树,三级区域个数大于1000个导致提交失败!!! org.springframework.beans.InvalidPropertyException: Invalid property 'd ...

  7. IE8下的怪异模式

    使用DWZ框架,老是出现点击button后在winxp IE8下出现新页面,经过检查后发现IE8下submit后,return false就不行了,必须使用window.event.returnVal ...

  8. 第三次scrum作业!

    1.小组成员 舒 溢 许嘉荣 唐 浩 黄欣欣 廖帅元 刘洋江 薛思汝 2.个人在小组第三次冲刺任务及其完成情况描述 根据小组讨论所分配任务,积极辅助组长以及各个成员,理清思路,编写代码,尽量在规定时间 ...

  9. 精准控制PWM脉冲的频率和数量

    在一些项目中,我们经常要控制PWM脉冲的频率和数量,比如步进电机的控制等,下面分享一个程序是关于这方面的,程序的思想就是通过STM32的定时器来输出PWM波,并开启定时器中断,在中断里面计数脉冲的数量 ...

  10. Java Collections Framework知识结构目录

    The core collection interfaces are the foundation of the Java Collections Framework. The Java Collec ...