Codeforces 580D Kefa and dishes

Source: Internet
Author: User

C. Kefa and Park

Kefa decided to celebrate he 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 have already found out what is the vertices with cats in them.

The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he'll go, but unfortunately he's very afraid of cats, so there was no-he wil L go to the restaurant if the path from the restaurant to He house contains more than m consecutive vert Ices with cats.

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

Input

The first line contains integers, n and m (2≤ n ≤105, 1≤ m ≤ n)-the number of vertices of the tree and the maximum number of consecutive vertices with cats tha T is still ok for Kefa.

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

NextN-1 lines contains the edges of the tree in the format "x i y Sub class= "Lower-index" > i "(without the quotes) ( 1≤ x i , y i n , x i y i ), where x i and y i is 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.

Sample Test (s) 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 the tree is a connected graph on n vertices and n -1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any of the vertices connected by an edge, one vertex was a parent (the one closer to the root), and the Other one was a child. A vertex is called a leaf and if it has no children.

Note to the first sample test:the vertices containing cats is marked red. The restaurants is 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 is located at vertices 4, 5, 6, 7. Kefa can ' t go to restaurants 6, 7.

1#include <cstdio>2#include <vector>3#include <cstring>4#include <algorithm>5 using namespacestd;6typedefLong Longll;7 intans=0, M;8 int  out[100005];9vector<int>r[100005];Ten BOOLmark[100005],vis[100005]; One voidDfsintUintcat) A { -     if(Mark[u]) + +Cat; -     Elsecat=0; the     if(cat>m)return; -vis[u]=1; -     intCnt=0; -      for(intv=0; V<r[u].size (); v++) +         if(!Vis[r[u][v]]) -         { + DFS (R[U][V],CAT); Acnt++; at         } -     if(!cnt) ans++; - } - intMain () - { -     intN; inscanf"%d%d",&n,&m); -      for(intI=1; i<=n;i++) to     { +         inttmp; -scanf"%d",&tmp); themark[i]=tmp; *     } $      for(intI=0; i<n-1; i++)Panax Notoginseng     { -         intu,v; thescanf"%d%d",&u,&v); + r[v].push_back (u); A R[u].push_back (v); the     } +Dfs1,0); -printf"%d\n", ans); $     return 0; $}

Codeforces 580D Kefa and dishes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.