Codeforces 1118F1-Tree Cutting (Easy Version) (树形dp入门题)

我们一起去砍树 咔咔咔

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

5
2 0 0 1 2
1 2
2 3
2 4
2 5

Output

1

Input

5
1 0 0 0 2
1 2
2 3
3 4
4 5

Output

4

Input

3
1 1 2
2 3
1 3

Output

0

Note

Here is the tree from the first example:

Codeforces 1118F1-Tree Cutting (Easy Version) (树形dp入门题)_第1张图片

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:

Codeforces 1118F1-Tree Cutting (Easy Version) (树形dp入门题)_第2张图片

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.


不得不说,这道题的题干是真的长,但是解释的很详细,就是说给你一颗树,可不是长在地上的树啊,树上的节点有的没有颜色,标记为0,有的染成了红色,标记为1,还有的染成了蓝色,标记为2;有一种树枝叫好树枝,你把它砍掉之后分成的两部分,每一部分只能有一种颜色,问你能找到多少根这样的好树枝?

又是一道树形dp的题,但比较独特,就是需要两个dp数组,这里我们使用red[i]和blue[i]来表示以i为树根的子树的颜色数量,
还需要用vector来存节点之间的联通关系
dfs的时候要注意,因为是无向图在一条树枝上的计算一次就行,不然就挂里面出不来了
这里可以判断一下 后继节点是否与前驱节点相同即可 
而子树的合并问题,被我给忘掉了。。。还好网上大神比较多
当一棵子树上包含了所有的颜色并且没有另一种颜色之后,那么分离他的边就是好边  Nice

做的我WA的一声就哭了

奉上我辣鸡又卑微的代码  Orz

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int mm=3e5+5;

int n;
int res=0;
int color[mm];//点的编号上的颜色 
int x,y;
int red[mm],blue[mm];//以i为根的树的颜色个数 
vectorvt[mm];//链接关系 

void dfs(int x,int pre){//枚举,x为当前节点  pre是前驱结点 
	if(color[x]==1)
		red[x]++;
	if(color[x]==2)
		blue[x]++;
	
	for(int i=0;i>n;
	for(int i=1;i<=n;i++){
		cin>>color[i];
		if(color[i]==1)
			red[0]++;
		if(color[i]==2)
			blue[0]++;//树上总的颜色数  
	}
		
	for(int i=1;i>x>>y;
		vt[x].push_back(y);
		vt[y].push_back(x);
	}
	dfs(1,-1);
	cout<

 

你可能感兴趣的:(搜索,树形dp,动态规划)