当前位置: 代码迷 >> 综合 >> Codeforces Round #540 (Div. 3) F1 Tree Cutting (Easy Version) 1118F1 (裸的树上dfs,树形dp)
  详细解决方案

Codeforces Round #540 (Div. 3) F1 Tree Cutting (Easy Version) 1118F1 (裸的树上dfs,树形dp)

热度:47   发布时间:2023-12-12 17:38:08.0

传送门:https://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.

告诉你一棵树,分别有蓝色、红色,无色节点,让你求一条边切断以后,分为两棵树颜色不相同的边的个数(无色节点不考虑)

很容易想到结构体保存一下每个节点下子树的红色和蓝色节点个数,然后遍历一遍边集,注意我并没有在dfs中直接判断,有人是在dfs中直接判断的,其实也很简单,但是如果最后遍历边集的话,你要判断一下两个点的父子关系,要拿出子节点来取到子节点的子树,至于为什么可以自己考虑一下,两个节点,如果你取到父节点,就没办法判断了,map标记一下父子关系即可(比赛的时候,写了20分钟,没有判断父子关系,一直wa14,太水了,,)

代码如下:

#include <bits/stdc++.h>
#include <time.h>
using namespace std;typedef long long ll;
typedef pair<int,int>  P;
const int maxn = 2e6 + 5000;
bool vis[maxn];
const ll mod = 1e9 + 7;
int sum = 0;
ll c,n,k,t;
ll b,q;
int a[maxn];vector<vector<int > >v;
struct node {int b,r;
} g[maxn];map<P,int>m;
node dfs(int x,int fa) {node gg;gg.b = 0,gg.r = 0;for(auto d:v[x]) {if(d == fa) continue;m[make_pair(d,x)] = 1;m[make_pair(x,d)] = 2;if(a[d] == 1) {gg.r++;} else if(a[d] == 2) {gg.b++;}node ggg = dfs(d,x);g[x].b += g[d].b;g[x].r += g[d].r;}return g[x];
}
P p[maxn];
int main() {while(cin >> n ) {v.resize(n + 300);int r = 0,b = 0;for(int i = 1; i <= n; i++) {g[i].r = g[i].b = 0;cin >> a[i];if(a[i] == 1) g[i].r++,r++;else if(a[i] == 2) g[i].b++,b++;}for(int i = 1; i < n; i++) {int u,vv;cin >> u >>vv;p[i].first = u,p[i].second = vv;v[vv].push_back(u);v[u].push_back(vv);}dfs(1,0);int ans = 0;for(int i = 1; i < n; i++) {int u = p[i].first;int vv = p[i].second;if(m[make_pair(u,vv)] == 1){if((b - g[u].b == 0 && g[u].r == 0) ||(r - g[u].r == 0 && g[u].b == 0))ans++;}else{if((b - g[vv].b == 0 && g[vv].r == 0) ||(r - g[vv].r == 0 && g[vv].b == 0))ans++;}}cout << ans << endl;}return 0;
}
  相关解决方案