CodeForces - 1118 F1 Tree Cutting (Easy Version)【DFS】

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.

 

跑一遍DFS,当节点u的子节点中(包括u)红色(蓝色)的点数量为0,且包含了所有蓝色(红色)的点,那么ans++。

F1到F2感觉跨度有点大 ,暂时没有什么想法。

#include "bits/stdc++.h"
using namespace std;
int color[300004];
int rednum[300004];
int bluenum[300004];
vector<int>t[300004];
int red,blue;
int ans;
void dfs(int u,int pre)
{
    if(color[u]==1)rednum[u]++;
    else if(color[u]==2)bluenum[u]++;
    for (int i = 0; i < t[u].size(); ++i) {
        int v=t[u][i];
        if(v==pre)continue;
        dfs(v,u);
        rednum[u]+=rednum[v];
        bluenum[u]+=bluenum[v];
    }
    if(rednum[u]==0&&bluenum[u]==blue)ans++;
    else if(rednum[u]==red&&bluenum[u]==0)ans++;
}
int main()
{
    memset(rednum,0, sizeof(rednum));
    memset(bluenum,0, sizeof(bluenum));
    int n;
    cin>>n;
    red=0,blue=0;
    for (int i = 1; i <= n; ++i) {
        scanf("%d",&color[i]);
        if(color[i]==1)red++;
        else if(color[i]==2)blue++;
    }
    int u,v;
    for (int i = 0; i < n-1; ++i) {
        scanf("%d%d",&u,&v);
        t[u].push_back(v);
        t[v].push_back(u);
    }
    ans=0;
    dfs(1,-1);
    printf("%d\n",ans);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值