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:

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.


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

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

做的我WA的一声就哭了

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

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#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为根的树的颜色个数 
vector<int>vt[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<vt[x].size();i++){
		int t=vt[x][i];//找到与他相连的点 
		if(t==pre)continue;//emm... 一根枝 不要忘了continue 
		dfs(t,x);
		red[x]+=red[t];//子树合并 
		blue[x]+=blue[t];
		if(red[t]==red[0]&&blue[t]==0||blue[t]==blue[0]&&red[t]==0)//子树里包括了所有的某颜色且没有另一种颜色 
			res++;
	}
}

int main()
{
	cin>>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<n;i++){
		cin>>x>>y;
		vt[x].push_back(y);
		vt[y].push_back(x);
	}
	dfs(1,-1);
	cout<<res<<endl;
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问。在这个问中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问: CodeForces - 982C 树形DP是什么问?如何解决? 回答: CodeForces - 982C是一个树形动态规划问。在这个问中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值