Tree Cutting (Easy Version) (树形dp水题)

本文探讨了一道算法题目,目标是在一棵树上找到最多的“好边”。好边定义为,当这条边被删除后,树分裂成的两部分各不包含红色和蓝色节点。文章详细介绍了使用深度优先搜索(DFS)算法来解决这一问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://cn.vjudge.net/contest/312853#problem/I

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三中情况,0代表该点无色。现在需要你将这棵树割掉一些边,使得割掉每条边分割成的两部分均最多只含有一种颜色的点,即分割后的两部分不能1,2点夹杂(0的点数可以任意),问你最多能有几条这样的边。

设一条边的两端点是u,v,那么只需要知道以v为根的子树1颜色和2颜色的个数,就能判断该边是不是好边

//木鲲上树,永无bug 
#include<bits/stdc++.h>
//#include<cstdio>
//#include<algorithm>
//#include<cstring>
//#include<map>
//#include<iostream>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
const int N=3e5+5;
int n;
struct node{
	int v,next;
}e[N<<1];
int head[N],tot=0;
void init(int n){
	tot=0;
	for(int i=0;i<=n;i++) head[i]=-1;
}
void add(int u,int v){
	e[tot].v=v;
	e[tot].next=head[u];
	head[u]=tot++;
	e[tot].v=u;
	e[tot].next=head[v];
	head[v]=tot++;
}
int sum1=0,sum2=0;
int num1[N],num2[N];
int c[N];
int ans=0;
void dfs(int u,int fa){
	if(c[u]==1) num1[u]=1;
	else if(c[u]==2) num2[u]=1;
	for(int i=head[u];~i;i=e[i].next){
		if(e[i].v==fa) continue;
		dfs(e[i].v,u);
		if(num1[e[i].v]==sum1&&num2[e[i].v]==0) ans++;
		else if(num2[e[i].v]==sum2&&num1[e[i].v]==0) ans++;
		num1[u]+=num1[e[i].v];
		num2[u]+=num2[e[i].v];
	}
}
int main(){
	ios::sync_with_stdio(false);
    cout.tie(NULL);
    scanf("%d",&n);
    init(n);
    ans=0;
    for(int i=1;i<=n;i++){
    	scanf("%d",&c[i]);
    	if(c[i]==1) sum1++;
    	else if(c[i]==2) sum2++;
	}
    for(int i=1;i<n;i++){
    	int u,v;
    	scanf("%d%d",&u,&v);
    	add(u,v);
	}
	dfs(1,0);
	printf("%d\n",ans);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值