CodeForces - 580C C. Kefa and Park 树 数据结构 建树 dfs

Kefa decided to celebrate his first big salary by going to the restaurant.

He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.

The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats.

Your task is to help Kefa count the number of restaurants where he can go.

Input

The first line contains two integers, n and m (2 ≤ n ≤ 1051 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa.

The second line contains n integers a1, a2, ..., an, where each ai either equals to 0(then vertex i has no cat), or equals to 1 (then vertex i has a cat).

Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ nxi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge.

It is guaranteed that the given set of edges specifies a tree.

Output

A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.

Examples
Input
4 1
1 1 0 0
1 2
1 3
1 4
Output
2
Input
7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7
Output
2
Note

Let us remind you that a tree is a connected graph on n vertices and n - 1 edge. Arooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.

Note to the first sample test:  The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.

Note to the second sample test:  The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7. 

                              



题意:给你n个节点,n个节点上有些有猫,主角要从1走到叶子节点,如果在路上遇到连续的猫超过m个,那么这条路走不通,求可以走到叶子节点的路的条数; 注意: 输入一条边的二个节点,父子关系不确定;


思路:我们可以用一个结构体里放一个vector来存此节点连接的节点,还要用一个标记数组,我们从根节点dfs,并且用vis标记已经访问过的节点就保证了只会从父亲搜索到儿子,当与那个节点连接的只有一个节点并且那个节点已经搜索过了,这个节点就是叶子节点;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int len=1e5+3;
const int INF=0x3f3f3f3f;
#define ll long long
const ll mod=1e18;
struct Node{
	vector<int>ve;
	int f;
}node[len];
ll ans;
int n,m;
int vis[len];
void dfs(int r,int sum)
{
	if(sum>m)return ;
	if(node[r].ve.size()==1&&vis[node[r].ve[0]])
	{
		ans++;
		return ;
	}
	for(int i=0;i<node[r].ve.size();++i)
	{
		int t=node[r].ve[i];
		if(vis[t])continue;
		vis[t]=1;
		if(node[t].f==0)dfs(t,0);
		else dfs(t,sum+1);
	}
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;++i)
		scanf("%d",&node[i].f);
	int u,v;
	for(int i=0;i<n-1;++i)
	{
		scanf("%d%d",&u,&v);
		node[u].ve.push_back(v); 
		node[v].ve.push_back(u);
	}
	vis[1]=1; 
	dfs(1,node[1].f);
	cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/hutwuguangrong/article/details/80412268