package main

import (
	"bufio"
	"fmt"
	"os"
)

type LinkList struct {
	node *Node
	next *LinkList
}

type Node struct {
	i             int
	children_head *LinkList
	children_last *LinkList
	visited       bool
}

func traverse(
	v *Node, has_cat []bool, cat_depth_so_far int, m int, restaurants *int) {
	if has_cat[v.i] {
		// This node has cat so,
		// increment cat depth
		cat_depth_so_far += 1
		if cat_depth_so_far > m {
			// ignore subtree because consecutive cat limit is reached
			return
		}
	} else {
		// This node doesn't have cat so,
		// break the consecutive chain by zero-ing the cat depth so far
		cat_depth_so_far = 0
	}
	// traverse the subtree
	has_child := false
	for curr := v.children_head; curr != nil; curr = curr.next {
		if !curr.node.visited {
			has_child = true
			curr.node.visited = true
			traverse(curr.node, has_cat, cat_depth_so_far, m, restaurants)
		}
	}
	if !has_child {
		*restaurants = *restaurants + 1
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var n, m, tmp1, tmp2 int
	fmt.Fscanln(reader, &n, &m)
	has_cat := make([]bool, n)
	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &tmp1)
		if tmp1 == 1 {
			has_cat[i] = true
		}
		nodes[i] = &Node{i: i, children_head: nil, children_last: nil}
	}

	var parent, child *Node
	var new_node *LinkList
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(reader, "\n%d %d", &tmp1, &tmp2)
		parent = nodes[tmp1-1]
		child = nodes[tmp2-1]
		// add child to parent
		new_node = &LinkList{node: child, next: nil}
		if parent.children_head == nil {
			parent.children_head = new_node
			parent.children_last = new_node
		} else {
			parent.children_last.next = new_node
			parent.children_last = new_node
		}
		// add parent to child
		new_node = &LinkList{node: parent, next: nil}
		if child.children_head == nil {
			child.children_head = new_node
			child.children_last = new_node
		} else {
			child.children_last.next = new_node
			child.children_last = new_node
		}

	}
	var restaurants int
	nodes[0].visited = true
	traverse(nodes[0], has_cat, 0, m, &restaurants)
	fmt.Fprintln(writer, restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	cats  bool
	edges map[int]struct{}
}

func atoi(s string) int {
	i, _ := strconv.Atoi(s)
	return i
}

func main() {
	nodes := map[int]Node{}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n := atoi(scanner.Text())
	scanner.Scan()
	m := atoi(scanner.Text())

	for i := 1; i <= n; i++ {
		scanner.Scan()
		cats := atoi(scanner.Text())
		nodes[i] = Node{cats: cats == 1, edges: map[int]struct{}{}}
	}

	for i := 1; i < n; i++ {
		scanner.Scan()
		l := atoi(scanner.Text())
		scanner.Scan()
		r := atoi(scanner.Text())
		nodes[l].edges[r] = struct{}{}
		nodes[r].edges[l] = struct{}{}
	}

	ret := dfs(nodes, 1, 0, m)
	fmt.Println(ret)
}

func dfs(nodes map[int]Node, num int, consec int, m int) int {
	curr := nodes[num]
	if !curr.cats {
		consec = 0
	} else {
		consec += 1
	}
	if consec > m {
		return 0
	}
	if len(curr.edges) == 0 {
		return 1
	}
	ret := 0
	for i, _ := range curr.edges {
		delete(nodes[i].edges, num)
		ret += dfs(nodes, i, consec, m)
	}
	return ret
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

var (
    n int
    m int
	nodes map[int]Node
)

type Node struct {
	cats  bool
	edges map[int]struct{}
}

func main() {
	defer writer.Flush()

	nodes = map[int]Node{}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
    scanner.Scan()
    n, _ = strconv.Atoi(scanner.Text())
    scanner.Scan()
    m, _ = strconv.Atoi(scanner.Text())

	for i := 1; i <= n; i++ {
		scanner.Scan()
        cats, _ := strconv.Atoi(scanner.Text())
        nodes[i] = Node{cats: cats == 1, edges: map[int]struct{}{}}
	}
    for i := 1; i < n; i++ {
        var l, r int
        scanner.Scan()
        l, _ = strconv.Atoi(scanner.Text())
        scanner.Scan()
        r, _ = strconv.Atoi(scanner.Text())
        nodes[l].edges[r] = struct{}{}
        nodes[r].edges[l] = struct{}{}
    }

    ret := dfs(1, 0)
    printf("%d", ret)
}

func dfs(num int, consec int) int {
	curr := nodes[num]
	if !curr.cats {
		consec = 0
	} else {
		consec += 1
	}
	if consec > m {
		return 0
	}
	if len(curr.edges) == 0 {
		return 1
	}
	ret := 0
	for i, _ := range curr.edges {
		delete(nodes[i].edges, num)
		ret += dfs(i, consec)
	}
	return ret
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	cats  bool
	edges map[int]struct{}
}

func nextInt(bs *bufio.Scanner) int {
	bs.Scan()
	i, _ := strconv.Atoi(bs.Text())
	return i
}

func main() {
	nodes := map[int]Node{}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	n := nextInt(scanner)
	m := nextInt(scanner)

	for i := 1; i <= n; i++ {
		cats := nextInt(scanner)
		nodes[i] = Node{cats: cats == 1, edges: map[int]struct{}{}}
	}

	for i := 1; i < n; i++ {
		l := nextInt(scanner)
		r := nextInt(scanner)
		nodes[l].edges[r] = struct{}{}
		nodes[r].edges[l] = struct{}{}
	}

	ret := dfs(nodes, 1, 0, m)
	fmt.Println(ret)
}

func dfs(nodes map[int]Node, num int, consec int, m int) int {
	curr := nodes[num]
	if !curr.cats {
		consec = 0
	} else {
		consec += 1
	}
	if consec > m {
		return 0
	}
	if len(curr.edges) == 0 {
		return 1
	}
	ret := 0
	for i, _ := range curr.edges {
		delete(nodes[i].edges, num)
		ret += dfs(nodes, i, consec, m)
	}
	return ret
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

type Node struct {
	cats  bool
	edges map[int64]bool
}

func main() {
	defer writer.Flush()
	var n, m int64
	scanf("%d %d\n", &n, &m)
	nodes := map[int64]*Node{}
	for i := int64(0); i < n; i++ {
		var cats int64
		if i != n-1 {
			scanf("%d", &cats)
		} else {
			scanf("%d\n", &cats)
		}
		nodes[i+1] = &Node{cats: cats == 1, edges: map[int64]bool{}}
	}
	for i := int64(1); i < n; i++ {
		var l, r int64
		scanf("%d %d\n", &l, &r)
		nodes[l].edges[r] = true
        nodes[r].edges[l] = true
	}
	ret := dfs(nodes, 1, 0, m)
	printf("%d", ret)
}

func dfs(nodes map[int64]*Node, num int64, consec int64, m int64) int64 {
	curr := nodes[num]
	if !curr.cats {
		consec = 0
	} else {
		consec += 1
	}
	if consec > m {
		return 0
	}
	if len(curr.edges) == 0 {
		return 1
	}
	ret := int64(0)
	for i, _ := range curr.edges {
        delete(nodes[i].edges, num)
		ret += dfs(nodes, i, consec, m)
	}
	return ret
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

var (
    n int64
    m int64
    nodes map[int64]Node
)

type Node struct {
	cats  bool
	edges map[int64]struct{}
}

func main() {
	defer writer.Flush()
	scanf("%d %d\n", &n, &m)
	nodes = map[int64]Node{}
	for i := int64(0); i < n; i++ {
		var cats int64
		if i != n-1 {
			scanf("%d", &cats)
		} else {
			scanf("%d\n", &cats)
		}
		nodes[i+1] = Node{cats: cats == 1, edges: map[int64]struct{}{}}
	}
	for i := int64(1); i < n; i++ {
		var l, r int64
		scanf("%d %d\n", &l, &r)
		nodes[l].edges[r] = struct{}{}
        nodes[r].edges[l] = struct{}{}
	}
	ret := dfs(1, 0)
	printf("%d", ret)
}

func dfs(num int64, consec int64) int64 {
	curr := nodes[num]
	if !curr.cats {
		consec = 0
	} else {
		consec += 1
	}
	if consec > m {
		return 0
	}
	if len(curr.edges) == 0 {
		return 1
	}
	ret := int64(0)
	for i, _ := range curr.edges {
        delete(nodes[i].edges, num)
		ret += dfs(i, consec)
	}
	return ret
}

package main

import (
	"fmt"
	"os"
)

var n, m int
var a []int
var tree [][]int
var seen []bool

func readnumbers() (nums []int) {
	fileInfo, _ := os.Stdin.Stat()
	p := make([]byte, fileInfo.Size())
	os.Stdin.Read(p)
	num, neg := 0, false
	for i := 0; i < len(p); i++ {
		if p[i] >= '0' {
			num = 10*num + int(p[i]) - 48
		} else if p[i] == '-' {
			neg = true
		} else if p[i] != '\r' {
			if neg {
				nums = append(nums, -num)
			} else {
				nums = append(nums, num)
			}
			num, neg = 0, false
		}
	}

	if len(p) > 0 && p[len(p)-1] >= '0' {
		if neg {
			nums = append(nums, -num)
		} else {
			nums = append(nums, num)
		}
	}

	return
}

func dfs(node, k int) int {
	seen[node] = true
	if a[node] == 1 {
		k--
	} else {
		k = m
	}

	if k < 0 {
		return 0
	}

	if len(tree[node]) == 1 && seen[tree[node][0]] {
		return 1
	}

	res := 0
	for _, child := range tree[node] {
		if !seen[child] {
			res += dfs(child, k)
		}
	}

	return res
}

func main() {
	nums := readnumbers()
	n, m = nums[0], nums[1]

	a = make([]int, n)
	for i := range a {
		a[i] = nums[i+2]
	}

	tree = make([][]int, n)
	for i := n + 2; i < len(nums); i += 2 {
		x, y := nums[i], nums[i+1]
		tree[x-1] = append(tree[x-1], y-1)
		tree[y-1] = append(tree[y-1], x-1)
	}

	seen = make([]bool, n)
	fmt.Println(dfs(0, m))
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
)

var n, m int
var a []int
var tree [][]int
var seen []bool

func readnumbers() (nums []int) {
	b, _ := ioutil.ReadAll(os.Stdin)
	s := string(b)

	num, neg := 0, false
	for i := 0; i < len(s); i++ {
		if s[i] >= '0' {
			num = 10*num + int(s[i]) - 48
		} else if s[i] == '-' {
			neg = true
		} else if s[i] != '\r' {
			if neg {
				nums = append(nums, -num)
			} else {
				nums = append(nums, num)
			}
			num, neg = 0, false
		}
	}

	if len(s) > 0 && s[len(s)-1] >= '0' {
		if neg {
			nums = append(nums, -num)
		} else {
			nums = append(nums, num)
		}
	}

	return
}

func dfs(node, k int) int {
	seen[node] = true
	if a[node] == 1 {
		k--
	} else {
		k = m
	}
	if k < 0 {
		return 0
	}

	if len(tree[node]) == 1 && seen[tree[node][0]] {
		return 1
	}

	res := 0
	for _, child := range tree[node] {
		if !seen[child] {
			res += dfs(child, k)
		}
	}
	return res
}

func main() {
	nums := readnumbers()
	n, m = nums[0], nums[1]

	a = make([]int, n)
	for i := range a {
		a[i] = nums[i+2]
	}

	tree = make([][]int, n)
	for i := n + 2; i < len(nums); i += 2 {
		x, y := nums[i], nums[i+1]
		tree[x-1] = append(tree[x-1], y-1)
		tree[y-1] = append(tree[y-1], x-1)
	}

	seen = make([]bool, n)
	fmt.Println(dfs(0, m))
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
)

var n, m int
var a []int
var tree [][]int
var seen []bool

func readnumbers() (nums []int) {
	b, _ := ioutil.ReadAll(os.Stdin)
	num, neg := 0, false
	for i := 0; i < len(b); i++ {
		if b[i] >= '0' {
			num = 10*num + int(b[i]) - 48
		} else if b[i] == '-' {
			neg = true
		} else if b[i] != '\r' {
			if neg {
				nums = append(nums, -num)
			} else {
				nums = append(nums, num)
			}
			num, neg = 0, false
		}
	}

	if len(b) > 0 && b[len(b)-1] >= '0' {
		if neg {
			nums = append(nums, -num)
		} else {
			nums = append(nums, num)
		}
	}

	return
}

func dfs(node, k int) int {
	seen[node] = true
	if a[node] == 1 {
		k--
	} else {
		k = m
	}

	if k < 0 {
		return 0
	}

	if len(tree[node]) == 1 && seen[tree[node][0]] {
		return 1
	}

	res := 0
	for _, child := range tree[node] {
		if !seen[child] {
			res += dfs(child, k)
		}
	}

	return res
}

func main() {
	nums := readnumbers()
	n, m = nums[0], nums[1]

	a = make([]int, n)
	for i := range a {
		a[i] = nums[i+2]
	}

	tree = make([][]int, n)
	for i := n + 2; i < len(nums); i += 2 {
		x, y := nums[i], nums[i+1]
		tree[x-1] = append(tree[x-1], y-1)
		tree[y-1] = append(tree[y-1], x-1)
	}

	seen = make([]bool, n)
	fmt.Println(dfs(0, m))
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
)

var n, m int
var a []int
var tree [][]int
var seen []bool

func readnumbers() <-chan int {
	out := make(chan int)
	go func() {
		b, _ := ioutil.ReadAll(os.Stdin)
		num, neg := 0, 1
		for i := 0; i < len(b); i++ {
			if b[i] >= '0' {
				num *= 10
				num += int(b[i] - '0')
			} else if b[i] == '-' {
				neg = -1
			} else if b[i] != '\r' {
				out <- neg * num
				num, neg = 0, 1
			}
		}

		if len(b) > 0 && b[len(b)-1] >= '0' {
			out <- neg * num
		}
		close(out)
	}()
	return out
}

func dfs(node, k int) int {
	seen[node] = true
	if a[node] == 1 {
		k--
	} else {
		k = m
	}

	if k < 0 {
		return 0
	}

	if len(tree[node]) == 1 && seen[tree[node][0]] {
		return 1
	}

	res := 0
	for _, child := range tree[node] {
		if !seen[child] {
			res += dfs(child, k)
		}
	}

	return res
}

func main() {
	out := readnumbers()
	n, m = <-out, <-out

	a = make([]int, n)
	for i := range a {
		a[i] = <-out
	}

	tree = make([][]int, n)
	for i := 0; i < n-1; i++ {
		x, y := <-out, <-out
		tree[x-1] = append(tree[x-1], y-1)
		tree[y-1] = append(tree[y-1], x-1)
	}

	seen = make([]bool, n)
	fmt.Println(dfs(0, m))
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"regexp"
	"strconv"
	"strings"
)

type Vertex struct {
	idx          int
	cat          int
	parent       *Vertex
	children     *Vertex
	next_sibling *Vertex
}

func main() {
	var n, m int
	if _, err := fmt.Scanf("%d %d\n", &n, &m); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	cats := make([]int, n)
	_v, err := reader.ReadString('\n')
	if err != nil {
		return
	}
	vertices := strings.Split(re.ReplaceAllString(_v, ""), " ")
	if len(vertices) != n {
		return
	}
	for i := 0; i < n; i++ {
		v, err := strconv.Atoi(vertices[i])
		if err != nil {
			return
		}
		cats[i] = v
	}

	edges := make([][]int, n)
	const init_size int = 100
	for i := 0; i < n-1; i++ {
		if _e, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			e := strings.Split(re.ReplaceAllString(_e, ""), " ")
			if len(e) != 2 {
				return
			}
			x, err := strconv.Atoi(e[0])
			if err != nil {
				return
			}
			y, err := strconv.Atoi(e[1])
			if err != nil {
				return
			}

			if edges[x-1] == nil {
				edges[x-1] = make([]int, 0, init_size)
				edges[x-1] = append(edges[x-1], 0)
			}
			edges[x-1][0] += 1
			edges[x-1] = append(edges[x-1], y)

			if edges[y-1] == nil {
				edges[y-1] = make([]int, 0, init_size)
				edges[y-1] = append(edges[y-1], 0)
			}
			edges[y-1][0] += 1
			edges[y-1] = append(edges[y-1], x)
		}
	}

	root := &Vertex{1, cats[0], nil, nil, nil}
	fmt.Println(build_sub_tree(root, edges, cats, root.cat, m))
}

func build_sub_tree(node *Vertex, edges [][]int, cats []int, cat, max_cat int) int {
	var last_child *Vertex = nil

	var cut bool = false
	for i := 1; i <= edges[node.idx-1][0]; i++ {
		var child_idx int = edges[node.idx-1][i]
		if node.parent == nil || child_idx != node.parent.idx {
			if cat+cats[child_idx-1] <= max_cat {
				new_child := &Vertex{child_idx, cats[child_idx-1], node, nil, nil}
				if node.children == nil {
					node.children = new_child
					last_child = new_child
				} else {
					last_child.next_sibling = new_child
					last_child = new_child
				}
			} else {
				cut = true
			}
		}
	}

	if node.children == nil {
		if cut {
			return 0
		} else {
			return 1
		}
	}

	res := 0
	for child := node.children; child != nil; child = child.next_sibling {
		var c int
		if child.cat == 0 {
			c = 0
		} else {
			c = cat + 1
		}
		res += build_sub_tree(child, edges, cats, c, max_cat)
	}
	return res
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	cats  bool
	edges map[int]struct{}
}

type WordScanner struct {
	*bufio.Scanner
}

func NewWordScanner() *WordScanner {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &WordScanner{Scanner: s}
}

func (s *WordScanner) NextInt() int {
	s.Scan()
	i, _ := strconv.Atoi(s.Text())
	return i
}

func main() {
	nodes := map[int]Node{}
	s := NewWordScanner()
	n := s.NextInt()
	m := s.NextInt()
	for i := 1; i <= n; i++ {
		cats := s.NextInt()
		nodes[i] = Node{cats: cats == 1, edges: map[int]struct{}{}}
	}
	for i := 1; i < n; i++ {
		l := s.NextInt()
		r := s.NextInt()
		nodes[l].edges[r] = struct{}{}
		nodes[r].edges[l] = struct{}{}
	}
	ret := dfs(nodes, 1, 0, m)
	fmt.Println(ret)
}

func dfs(nodes map[int]Node, num int, consec int, m int) int {
	curr := nodes[num]
	if !curr.cats {
		consec = 0
	} else {
		consec += 1
	}
	if consec > m {
		return 0
	}
	if len(curr.edges) == 0 {
		return 1
	}
	ret := 0
	for i, _ := range curr.edges {
		delete(nodes[i].edges, num)
		ret += dfs(nodes, i, consec, m)
	}
	return ret
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	n, m, result int
	v            [][]int
	cats         []int
	visited      []bool
)

func dfs(curNode int, curCat int) {
	if visited[curNode] {
		return
	}
	visited[curNode] = true
	if curCat > m {
		return
	}
	//find if curNode is a leaf
	if len(v[curNode]) == 1 && visited[v[curNode][0]] {
		result++
		return
	}
	for i := 0; i < len(v[curNode]); i++ {
		newNode := v[curNode][i]
		if cats[curNode] == 1 {
			if cats[newNode] == 1 {
				dfs(newNode, curCat+1)
			} else {
				dfs(newNode, 0)
			}
		} else {
			if cats[newNode] == 1 {
				dfs(newNode, 1)
			} else {
				dfs(newNode, 0)
			}
		}
	}
}

func main() {
	r := bufio.NewReader(os.Stdin)
	fmt.Fscanf(r, "%d %d\n", &n, &m)
	visited = make([]bool, n+1)
	cats = make([]int, n+1)
	for i := 1; i < n+1; i++ {
		fmt.Fscanf(r, "%d", &cats[i])
	}
	fmt.Fscanln(r)
	v = make([][]int, n+1)
	for i := 0; i < n-1; i++ {
		var n1, n2 int
		fmt.Fscanf(r, "%d %d\n", &n1, &n2)
		v[n1] = append(v[n1], n2)
		v[n2] = append(v[n2], n1)
	}
	dfs(1, cats[1])
	fmt.Println(result)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func solve() {
	n := readInt()
	m := readInt()
	a := readIntArray(n)

	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, 0)
	}
	for i := 0; i < n - 1; i++ {
		u := readInt() - 1
		v := readInt() - 1
		g[u] = append(g[u], v)
		g[v] = append(g[v], u)
	}
	vis := make([]bool, n)
	fmt.Println(dfs(g, 0, 0, m, a, vis))
}

func dfs(g [][]int, u int, cur int, m int, a []int, vis []bool) int {
	vis[u] = true
	if a[u] == 1 {
		cur++
	} else {
		cur = 0
	}
	if cur > m {
		return 0
	}
	ans := 0
	ok := false
	for _, v := range g[u] {
		if !vis[v] {
			ans += dfs(g, v, cur, m, a, vis)
			ok = true
		}
	}
	if !ok {
		return 1
	}
	return ans
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

var (
    n int64
    m int64
    nodes map[int64]Node
)

type Node struct {
	cats  bool
	edges map[int64]bool
}

func main() {
	defer writer.Flush()
	scanf("%d %d\n", &n, &m)
	nodes = map[int64]Node{}
	for i := int64(0); i < n; i++ {
		var cats int64
		if i != n-1 {
			scanf("%d", &cats)
		} else {
			scanf("%d\n", &cats)
		}
		nodes[i+1] = Node{cats: cats == 1, edges: map[int64]bool{}}
	}
	for i := int64(1); i < n; i++ {
		var l, r int64
		scanf("%d %d\n", &l, &r)
		nodes[l].edges[r] = true
        nodes[r].edges[l] = true
	}
	ret := dfs(1, 0)
	printf("%d", ret)
}

func dfs(num int64, consec int64) int64 {
	curr := nodes[num]
	if !curr.cats {
		consec = 0
	} else {
		consec += 1
	}
	if consec > m {
		return 0
	}
	if len(curr.edges) == 0 {
		return 1
	}
	ret := int64(0)
	for i, _ := range curr.edges {
        delete(nodes[i].edges, num)
		ret += dfs(i, consec)
	}
	return ret
}

package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
)

type Node struct {
	children [] int
	value    int
}

var visitedNode [] bool
var colored [] bool
var nodes [] Node
var M int

func main() {
	var n int
	fmt.Scan(&n, &M)
	nodes = make([]Node, n+1)

	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)

	for i := 0; i < n; i++ {
		bs.Scan()
		temp, _ := strconv.Atoi(bs.Text())
		nodes[i] = Node{value: temp}
	}

	for i := 0; i < n-1; i++ {
		bs.Scan()
		from, _ := strconv.Atoi(bs.Text())
		from--

		bs.Scan()
		to, _ := strconv.Atoi(bs.Text())
		to--

		nodes[to].children = append(nodes[to].children, from)
		nodes[from].children = append(nodes[from].children, to)
	}

	visitedNode = make([]bool, n)
	if nodes[0].value == 1 {
		fmt.Println(dfs_traverse(0, 1))
	} else {
		fmt.Println(dfs_traverse(0, 0))
	}
}

func dfs_traverse(index, streak int) int {
	visitedNode[index] = true
	if streak > M {
		return 0
	}

	sum := 0
	isLeaf := true
	for i := 0; i < len(nodes[index].children); i++ {
		neighbor := nodes[index].children[i]
		if !visitedNode[neighbor] {
			isLeaf = false

			if nodes[neighbor].value == 1 {
				sum += dfs_traverse(neighbor, streak+1)
			} else {
				sum += dfs_traverse(neighbor, 0)
			}
		}

	}

	if isLeaf {
		return 1
	}
	return sum
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type tree_node struct {
	children [](*tree_node)
	value    int
}

func visit(root, parent *tree_node, m, cur int, ans *int) {
	if cur > m {
		return
	}
	if len(root.children) == 1 && root.children[0] == parent {
		*ans++
		return
	}

	for i := 0; i < len(root.children); i++ {
		if root.children[i] != parent {
			if root.children[i].value == 0 {
				visit(root.children[i], root, m, 0, ans)
			} else {
				visit(root.children[i], root, m, 1+cur, ans)
			}
		}
	}
}

func main() {
	var n, m int
	fmt.Scan(&n, &m)

	nodes := make([]tree_node, n+1, n+1)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 1; i <= n; i++ {
		bs.Scan()
		nodes[i].value, _ = strconv.Atoi(bs.Text())
	}

	for i := 1; i < n; i++ {
		bs.Scan()
		p, _ := strconv.Atoi(bs.Text())
		bs.Scan()
		c, _ := strconv.Atoi(bs.Text())

		nodes[p].children = append(nodes[p].children, &nodes[c])
		nodes[c].children = append(nodes[c].children, &nodes[p])
	}

	ans := 0
	visit(&nodes[1], nil, m, nodes[1].value, &ans)
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type vertex struct {
	val      int8
	children *vnode
}

type vnode struct {
	vtx  *vertex
	next *vnode
}

func (this *vertex) addChild(v *vertex) {
	this.children = &vnode{v, this.children}
}

func (this *vertex) forEachChild(callback func(*vertex)) {
	for cur := this.children; cur != nil; cur = cur.next {
		callback(cur.vtx)
	}
}

type park struct {
	vertices []vertex
}

func (this *park) getRoot() *vertex {
	return &(this.vertices[0])
}

func (this *park) link(p, c int) {
	this.vertices[p].addChild(&(this.vertices[c]))
}

func handleInput() (park, int) {
	in := bufio.NewReader(os.Stdin)

	var n, m int
	buf, _ := in.ReadString('\n')
	fmt.Sscanf(buf, "%d %d", &n, &m)

	park := park{make([]vertex, n)}

	buf, _ = in.ReadString('\n')
	for i := 0; i < n; i++ {
		fmt.Sscanf(buf[i*2:], "%d", &(park.vertices[i].val))
	}

	type inode struct {
		idx  int
		next *inode
	}
	indices := make([]*inode, n)
	for i := 0; i < n-1; i++ {
		var idx0, idx1 int
		buf, _ = in.ReadString('\n')
		fmt.Sscanf(buf, "%d %d", &idx0, &idx1)
		idx0, idx1 = idx0-1, idx1-1
		indices[idx0] = &inode{idx1, indices[idx0]}
		indices[idx1] = &inode{idx0, indices[idx1]}
	}
	flags := make([]bool, n)
	flags[0] = true
	var idxiter func(int)
	idxiter = func(idx int) {
		for cur := indices[idx]; cur != nil; cur = cur.next {
			if !flags[cur.idx] {
				park.link(idx, cur.idx)
				flags[cur.idx] = true
				idxiter(cur.idx)
			}
		}
	}
	idxiter(0)

	return park, m
}

func solve(p park, m int) int {
	var count int
	var walk func(*vertex, int)
	walk = func(v *vertex, cats int) {
		if v.val == 0 {
			cats = 0
		} else {
			cats++
		}
		if cats > m {
			return
		}
		if v.children == nil {
			count++
		} else {
			v.forEachChild(func(child *vertex) {
				walk(child, cats)
			})
		}
	}
	walk(p.getRoot(), 0)
	return count
}

func main() {
	fmt.Println(solve(handleInput()))
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

var lists = make([]int, 0)

func dfs(g [][]int, u []bool, a, d []int, v, w, m, p int) {
	u[v] = true
	d[v] = w
	for i := 0; i < len(g[v]); i++ {
		to := g[v][i]
		if !u[to] {
			if a[to] == 1 {
				dfs(g, u,a,d,  to, w + 1, m, v)
			} else if w > m {
				dfs(g, u,a,d, to, w, m, v)
			} else {
				dfs(g, u,a,d, to, 0, m, v)
			}
		} else {
			if p == to && len(g[v]) == 1 {
				lists = append(lists, v)
			}
		}
	}
}

func solution(reader io.Reader, writer io.Writer) {
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")
	g := make([][]int, n)
	for i := 0; i < n - 1; i++ {
		var x, y int
		fmt.Fscanf(reader, "%d %d\n", &x, &y)
		x--
		y--
		g[x] = append(g[x], y)
		g[y] = append(g[y], x)
	}
	u := make([]bool, n)
	d := make([]int, n)
	dfs(g, u, a, d, 0, a[0], m, -1)
	var r int
	for i := 0; i < len(lists); i++ {
		if d[lists[i]] <= m {
			r++
		}
	}
	fmt.Println(r)
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	n, m int
	a    []int
	tree [][]int
)

func main() {
	// file, _ := os.Open("input.txt")
	file := os.Stdin
	f := bufio.NewReader(file)
	fmt.Fscanf(f, "%d %d\n", &n, &m)
	a = make([]int, n+1)
	tree = make([][]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(f, "%d", &a[i])
	}

	fmt.Fscanln(f)
	for i := 1; i < n; i++ {
		var x, y int
		fmt.Fscanf(f, "%d %d\n", &x, &y)
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	queue := []int{1}
	cat_counts := make([]int, n+1)
	for i := 0; i < len(cat_counts); i++ {
		cat_counts[i] = -1
	}

	cat_counts[1] = a[1]
	res := 0
	for len(queue) > 0 {
		current := queue[0]
		// fmt.Println(current)
		if current != 1 && cat_counts[current] <= m && len(tree[current]) == 1 {
			// fmt.Println(current, cat_counts[current])
			res++
		}
		queue = queue[1:]
		for j := 0; j < len(tree[current]); j++ {
			y := tree[current][j]
			// fmt.Println(y)
			if cat_counts[y] != -1 {
				continue
			}
			if a[y] == 1 {
				cat_counts[y] = cat_counts[current] + 1
			} else {
				cat_counts[y] = 0
			}

			if cat_counts[y] <= m {
				queue = append(queue, y)
			}
		}
		// fmt.Println("end")
	}

	fmt.Println(res)

}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func print(a ...interface{})            { fmt.Fprint(writer, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

// Tree structure
type Tree struct {
	Relations []*Tree
	HasCat    bool
	Visited   bool
	Dept      int
}

// Add function
func (t *Tree) Add(t2 *Tree) {
	t.Relations = append(t.Relations, t2)
}

func (t *Tree) getRestaurants(consec, numCats, dept int) int {
	if t == nil || t.Visited {
		return 0
	}
	t.Visited = true
	t.Dept = dept

	if t.HasCat {
		consec++
	} else {
		consec = 0
	}

	if consec > numCats {
		return 0
	}

	// The Leaf will be the one with the higher dept.
	if len(t.Relations) < 2 {
		relationDept := t.Relations[0].Dept
		if relationDept != -1 && dept > relationDept {
			return 1
		}
	}

	restaurants := 0
	for _, child := range t.Relations {
		restaurants += child.getRestaurants(consec, numCats, dept+1)
	}
	return restaurants
}

func main() {
	defer writer.Flush() // STDOUT MUST BE FLUSHED MANUALLY!!!
	n, m := 0, 0
	scan(&n, &m)
	treeNodes := make([]*Tree, n)

	for i := 0; i < n; i++ {
		hc := 0
		scan(&hc)
		node := &Tree{[]*Tree{}, hc == 1, false, -1}
		treeNodes[i] = node
	}

	for i := 0; i < n-1; i++ {
		var x, y int
		scan(&x, &y)
		x, y = int(math.Min(float64(x), float64(y))), int(math.Max(float64(x), float64(y)))
		treeNodes[x-1].Add(treeNodes[y-1])
		treeNodes[y-1].Add(treeNodes[x-1])
	}

	print(treeNodes[0].getRestaurants(0, m, 0))
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Graph struct {
	Edges       map[int][]int
	Visited     map[int]bool
	Order       []int
	Cats        []int
	Restaurants int
	MaxCats     int
}

func explore(g *Graph, u, parent int) {
	g.Visited[u] = true
	g.Order = append(g.Order, u)
	if edges, ok := g.Edges[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok {
				explore(g, v, u)
			}
		}
	}
	if len(g.Edges[u]) == 1 && g.Edges[u][0] == parent && g.Visited[parent] == true {
		pathLength := len(g.Order)
		cCats := 0
		count := 0
		for i := 0; i < pathLength; i++ {
			if g.Cats[g.Order[i]] == 0 {
				count = 0
			} else {
				count = count + 1
			}
			if cCats < count {
				cCats = count
			}
		}
		if cCats <= g.MaxCats {
			g.Restaurants = g.Restaurants + 1
		}
	}
	g.Order = g.Order[0 : len(g.Order)-1]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, temp, u, v int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	g := Graph{Visited: make(map[int]bool), Edges: make(map[int][]int), Cats: make([]int, n+1), MaxCats: m, Order: make([]int, 0), Restaurants: 0}
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &temp)
		g.Cats[i] = temp
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		if _, ok := g.Edges[u]; !ok {
			g.Edges[u] = make([]int, 0)
		}
		if _, ok := g.Edges[v]; !ok {
			g.Edges[v] = make([]int, 0)
		}
		g.Edges[u] = append(g.Edges[u], v)
		g.Edges[v] = append(g.Edges[v], u)
	}
	if _, ok := g.Visited[1]; !ok {
		explore(&g, 1, -1)
	}
	fmt.Fprintf(writer, "%d\n", g.Restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Graph struct {
	Edges       map[int][]int
	Visited     map[int]bool
	Order       []int
	Cats        []int
	Restaurants int
	MaxCats     int
}

func explore(g *Graph, u, parent int) {
	g.Visited[u] = true
	g.Order = append(g.Order, u)
	if edges, ok := g.Edges[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok && v != parent {
				explore(g, v, u)
			}
		}
	}
	if len(g.Edges[u]) == 1 && g.Edges[u][0] == parent && g.Visited[parent] == true {
		pathLength := len(g.Order)
		cCats := 0
		count := 0
		for i := 0; i < pathLength; i++ {
			if g.Cats[g.Order[i]] == 0 {
				count = 0
			} else {
				count = count + 1
			}
			if cCats < count {
				cCats = count
			}
		}
		if cCats <= g.MaxCats {
			g.Restaurants = g.Restaurants + 1
		}
	}
	g.Order = g.Order[0 : len(g.Order)-1]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, temp, u, v int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	g := Graph{Visited: make(map[int]bool), Edges: make(map[int][]int), Cats: make([]int, n+1), MaxCats: m, Order: make([]int, 0), Restaurants: 0}
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &temp)
		g.Cats[i] = temp
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		if _, ok := g.Edges[u]; !ok {
			g.Edges[u] = make([]int, 0)
		}
		if _, ok := g.Edges[v]; !ok {
			g.Edges[v] = make([]int, 0)
		}
		g.Edges[u] = append(g.Edges[u], v)
		g.Edges[v] = append(g.Edges[v], u)
	}
	if _, ok := g.Visited[1]; !ok {
		explore(&g, 1, -1)
	}
	fmt.Fprintf(writer, "%d\n", g.Restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

var (
    n int64
    m int64
    nodes map[int64]*Node
)

type Node struct {
	cats  bool
	edges map[int64]bool
}

func main() {
	defer writer.Flush()
	scanf("%d %d\n", &n, &m)
	nodes = map[int64]*Node{}
	for i := int64(0); i < n; i++ {
		var cats int64
		if i != n-1 {
			scanf("%d", &cats)
		} else {
			scanf("%d\n", &cats)
		}
		nodes[i+1] = &Node{cats: cats == 1, edges: map[int64]bool{}}
	}
	for i := int64(1); i < n; i++ {
		var l, r int64
		scanf("%d %d\n", &l, &r)
		nodes[l].edges[r] = true
        nodes[r].edges[l] = true
	}
	ret := dfs(1, 0)
	printf("%d", ret)
}

func dfs(num int64, consec int64) int64 {
	curr := nodes[num]
	if !curr.cats {
		consec = 0
	} else {
		consec += 1
	}
	if consec > m {
		return 0
	}
	if len(curr.edges) == 0 {
		return 1
	}
	ret := int64(0)
	for i, _ := range curr.edges {
        delete(nodes[i].edges, num)
		ret += dfs(i, consec)
	}
	return ret
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Tree struct {
	value    int
	children []*Tree
}

func (tree *Tree) Visit(parent *Tree, maxCats int, catCount int, res *int) {
	catCount += tree.value
	if catCount > maxCats {
		return
	}

	// If it's a leaf we reached a restaurant we can go
	if len(tree.children) == 1 && parent != nil {
		*res += 1
		return
	}

	for _, child := range tree.children {
		if child == parent {
			continue
		}
		child.Visit(tree, maxCats, catCount*child.value, res)
	}
}

func main() {
	var vertices, maxCats int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	fmt.Scanf("%v %v", &vertices, &maxCats)

	tree := make([]Tree, vertices+1, vertices+1)

	for i := 0; i < vertices; i++ {
		scanner.Scan()
		tree[i+1].value, _ = strconv.Atoi(scanner.Text())
	}

	for i := 0; i < vertices-1; i++ {
		scanner.Scan()
		from, _ := strconv.Atoi(scanner.Text())

		scanner.Scan()
		to, _ := strconv.Atoi(scanner.Text())

		tree[from].children = append(tree[from].children, &tree[to])
		tree[to].children = append(tree[to].children, &tree[from])
	}

	res := 0
	tree[1].Visit(nil, maxCats, 0, &res)
	fmt.Println(res)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type tree_node struct {
	children []*tree_node
	value    int
}

func visit(root, parent *tree_node, m, cur int, ans *int) {
	if cur > m {
		return
	}
	if len(root.children) == 1 && root.children[0] == parent {
		*ans++
		return
	}

	for i := 0; i < len(root.children); i++ {
		if root.children[i] != parent {
			if root.children[i].value == 0 {
				visit(root.children[i], root, m, 0, ans)
			} else {
				visit(root.children[i], root, m, 1+cur, ans)
			}
		}
	}
}

func main() {
	var n, m int
	fmt.Scan(&n, &m)

	nodes := make([]tree_node, n+1, n+1)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 1; i <= n; i++ {
		bs.Scan()
		nodes[i].value, _ = strconv.Atoi(bs.Text())
	}

	for i := 1; i < n; i++ {
		bs.Scan()
		p, _ := strconv.Atoi(bs.Text())
		bs.Scan()
		c, _ := strconv.Atoi(bs.Text())

		nodes[p].children = append(nodes[p].children, &nodes[c])
		nodes[c].children = append(nodes[c].children, &nodes[p])
	}

	ans := 0
	visit(&nodes[1], nil, m, nodes[1].value, &ans)
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	n, m    int
	haveCat []bool
	edges   [][]int
	used    []bool
	ret     int
)

func dfs(pos, cat int) {
	if used[pos] {
		return
	}

	used[pos] = true

	if haveCat[pos] {
		cat++
	} else {
		cat = 0
	}

	if cat > m {
		return
	}

	if len(edges[pos]) == 1 && pos != 0 {
		ret += 1
		return
	}

	for i := range edges[pos] {
		dfs(edges[pos][i], cat)
	}
}

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m = nextInt(), nextInt()
	haveCat = make([]bool, n)
	edges = make([][]int, n)
	used = make([]bool, n)

	for i := range haveCat {
		if nextInt() == 1 {
			haveCat[i] = true
		}
	}

	a, b := 0, 0

	for i := 0; i < n-1; i++ {
		a, b = nextInt()-1, nextInt()-1
		edges[a] = append(edges[a], b)
		edges[b] = append(edges[b], a)
	}
	dfs(0, 0)
	println(ret)
	// println(edges, haveCat, used)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

type Tree struct {
	value    int
	children []*Tree
}

func (tree *Tree) Visit(maxCats int, catCount int, res *int) {
	catCount += tree.value
	if catCount > maxCats {
		return
	}

	// If it's a leaf we reached a restaurant we can go
	if len(tree.children) == 0 {
		*res += 1
		return
	}

	for _, child := range tree.children {
		child.Visit(maxCats, catCount*child.value, res)
	}
}

func main() {
	var vertices, maxCats int
	scanner := bufio.NewScanner(os.Stdin)

	fmt.Scanf("%v %v", &vertices, &maxCats)

	tree := make([]Tree, vertices+1, vertices+1)

	for scanner.Scan() {
		if scanner.Text() != "" {
			break
		}
	}

	for i, v := range strings.Split(scanner.Text(), " ") {
		tree[i+1].value, _ = strconv.Atoi(v)
	}

	for i := 0; i < vertices-1; i++ {
		for scanner.Scan() {
			if scanner.Text() != "" {
				break
			}
		}

		res := strings.Split(scanner.Text(), " ")
		from, _ := strconv.Atoi(res[0])
		to, _ := strconv.Atoi(res[1])

		tree[from].children = append(tree[from].children, &tree[to])
	}

	res := 0
	tree[1].Visit(maxCats, 0, &res)
	fmt.Println(res)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	n, m    int
	haveCat []bool
	edges   [][]int
	// ret     int
)

func dfs(pos, cat int) int {
	if (pos == 0 || cat > 0) && haveCat[pos] {
		cat++
	} else {
		cat = 0
	}

	if cat > m {
		return 0
	}

	if len(edges[pos]) == 0 {
		return 1
	}

	total := 0

	for i := range edges[pos] {
		total += dfs(edges[pos][i], cat)
	}

	return total
}

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m = nextInt(), nextInt()
	haveCat = make([]bool, n)
	edges = make([][]int, n)

	a := 0

	for i := range haveCat {
		if nextInt() == 1 {
			haveCat[i] = true
		}

		edges[i] = make([]int, 0)
	}

	for i := 0; i < n-1; i++ {
		a = nextInt() - 1
		edges[a] = append(edges[a], nextInt()-1)
	}
	println(edges, haveCat)
	println(dfs(0, 0))
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	n, m    int
	haveCat []bool
	edges   [][]int
	used    []bool
	ret     int
)

func dfs(pos, cat int) {
	used[pos] = true

	if (pos == 0 || cat > 0) && haveCat[pos] {
		cat++
	} else {
		cat = 0
	}

	if cat > m {
		return
	}

	if len(edges[pos]) == 1 && pos != 0 {
		ret += 1
		return
	}

	for i := range edges[pos] {
		if used[edges[pos][i]] {
			continue
		}

		dfs(edges[pos][i], cat)
	}
}

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m = nextInt(), nextInt()
	haveCat = make([]bool, n)
	edges = make([][]int, n)
	used = make([]bool, n)

	for i := range haveCat {
		if nextInt() == 1 {
			haveCat[i] = true
		}

		edges[i] = make([]int, 0)
	}

	a, b := 0, 0

	for i := 0; i < n-1; i++ {
		a, b = nextInt()-1, nextInt()-1
		edges[a] = append(edges[a], b)
		edges[b] = append(edges[b], a)
	}
	dfs(0, 0)
	println(ret)
	// println(edges, haveCat, used)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	n, m    int
	haveCat []bool
	edges   [][]int
	// ret     int
)

func dfs(pos, cat int) int {
	if (pos == 0 || cat > 0) && haveCat[pos] {
		cat++
	} else {
		cat = 0
	}

	if cat > m {
		return 0
	}

	if len(edges[pos]) == 0 {
		return 1
	}

	total := 0

	for i := range edges[pos] {
		total += dfs(edges[pos][i], cat)
	}

	return total
}

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m = nextInt(), nextInt()
	haveCat = make([]bool, n)
	edges = make([][]int, n)

	for i := range haveCat {
		if nextInt() == 1 {
			haveCat[i] = true
		}

		edges[i] = make([]int, 0)
	}

	a, b := 0, 0

	for i := 0; i < n-1; i++ {
		a, b = nextInt()-1, nextInt()-1
		if a > b {
			a, b = b, a
		}
		edges[a] = append(edges[a], b)
	}
	println(edges, haveCat)
	println(dfs(0, 0))
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	n, m    int
	haveCat []bool
	edges   [][]int
	used    []bool
	ret     int
)

func dfs(pos, cat int) {
	if used[pos] {
		return
	}

	used[pos] = true

	if (pos == 0 || cat > 0) && haveCat[pos] {
		cat++
	} else {
		cat = 0
	}

	if cat > m {
		return
	}

	if len(edges[pos]) == 1 && pos != 0 {
		ret += 1
		return
	}

	for i := range edges[pos] {
		dfs(edges[pos][i], cat)
	}
}

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m = nextInt(), nextInt()
	haveCat = make([]bool, n)
	edges = make([][]int, n)
	used = make([]bool, n)

	for i := range haveCat {
		if nextInt() == 1 {
			haveCat[i] = true
		}

		edges[i] = make([]int, 0)
	}

	a, b := 0, 0

	for i := 0; i < n-1; i++ {
		a, b = nextInt()-1, nextInt()-1
		edges[a] = append(edges[a], b)
		edges[b] = append(edges[b], a)
	}
	dfs(0, 0)
	println(ret)
	// println(edges, haveCat, used)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	n, m    int
	haveCat []bool
	edges   [][]int
	// ret     int
)

func dfs(pos, cat int) int {
	if (pos == 0 || cat > 0) && haveCat[pos] {
		cat++
	} else {
		cat = 0
	}

	if cat > m {
		return 0
	}

	if len(edges[pos]) == 0 {
		return 1
	}

	total := 0

	for i := range edges[pos] {
		total += dfs(edges[pos][i], cat)
	}

	return total
}

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m = nextInt(), nextInt()
	haveCat = make([]bool, n)
	edges = make([][]int, n)

	a := 0

	for i := range haveCat {
		if nextInt() == 1 {
			haveCat[i] = true
		}

		edges[i] = make([]int, 0)
	}

	for i := 0; i < n-1; i++ {
		a = nextInt() - 1
		edges[a] = append(edges[a], nextInt()-1)
	}
	// println(edges, haveCat)
	println(dfs(0, 0))
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	n, m    int
	haveCat []bool
	edges   [][]int
	used    []bool
	// ret     int
)

func dfs(pos, cat int) int {
	used[pos] = true

	if (pos == 0 || cat > 0) && haveCat[pos] {
		cat++
	} else {
		cat = 0
	}

	if cat > m {
		return 0
	}

	if len(edges[pos]) == 1 && pos != 0 {
		return 1
	}

	total := 0

	for i := range edges[pos] {
		if used[edges[pos][i]] {
			continue
		}

		total += dfs(edges[pos][i], cat)
	}

	return total
}

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m = nextInt(), nextInt()
	haveCat = make([]bool, n)
	edges = make([][]int, n)
	used = make([]bool, n)

	for i := range haveCat {
		if nextInt() == 1 {
			haveCat[i] = true
		}

		edges[i] = make([]int, 0)
	}

	a, b := 0, 0

	for i := 0; i < n-1; i++ {
		a, b = nextInt()-1, nextInt()-1
		edges[a] = append(edges[a], b)
		edges[b] = append(edges[b], a)
	}
	println(dfs(0, 0))
	// println(edges, haveCat, used)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	n, m    int
	haveCat []bool
	edges   [][]int
	// ret     int
)

func dfs(pos, cat int) int {
	if (pos == 0 || cat > 0) && haveCat[pos] {
		cat++
	} else {
		cat = 0
	}

	if cat > m {
		return 0
	}

	if len(edges[pos]) == 0 {
		return 1
	}

	total := 0

	for i := range edges[pos] {
		total += dfs(edges[pos][i], cat)
	}

	return total
}

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m = nextInt(), nextInt()
	haveCat = make([]bool, n)
	edges = make([][]int, n)

	for i := range haveCat {
		if nextInt() == 1 {
			haveCat[i] = true
		}

		edges[i] = make([]int, 0)
	}

	a, b := 0, 0

	for i := 0; i < n-1; i++ {
		a, b = nextInt()-1, nextInt()-1
		if a > b {
			a, b = b, a
		}
		edges[a] = append(edges[a], b)
	}
	// println(edges, haveCat)
	println(dfs(0, 0))
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	n, m, result int
	v            [][]int
	cats         []int
	visited      []bool
)

func dfs(curNode int, curCat int) {
	if visited[curNode] {
		return
	}
	visited[curNode] = true
	if curCat > m {
		return
	}
	//find if curNode is a leaf
	if len(v[curNode]) == 1 && visited[v[curNode][0]] {
		result++
		return
	}
	for i := 0; i < len(v[curNode]); i++ {
		newNode := v[curNode][i]
		if cats[curCat] == 1 {
			if cats[newNode] == 1 {
				dfs(newNode, curCat+1)
			} else {
				dfs(newNode, 0)
			}
		} else {
			if cats[newNode] == 1 {
				dfs(newNode, 1)
			} else {
				dfs(newNode, 0)
			}
		}
	}
}

func main() {
	r := bufio.NewReader(os.Stdin)
	fmt.Fscanf(r, "%d %d\n", &n, &m)
	visited = make([]bool, n+1)
	cats = make([]int, n+1)
	for i := 1; i < n+1; i++ {
		fmt.Fscanf(r, "%d", &cats[i])
	}
	fmt.Fscanf(r, "%d\n", &cats[n-1])
	v = make([][]int, n+1)
	for i := 0; i < n-1; i++ {
		var n1, n2 int
		fmt.Fscanf(r, "%d %d\n", &n1, &n2)
		v[n1] = append(v[n1], n2)
		v[n2] = append(v[n2], n1)
	}
	dfs(1, cats[1])
	fmt.Println(result)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func solve() {
	n := readInt()
	m := readInt()
	a := readIntArray(n)

	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, 0)
	}
	for i := 0; i < n - 1; i++ {
		u := readInt() - 1
		v := readInt() - 1
		if v < u {
			v, u = u, v
		}
		g[u] = append(g[u], v)
	}
	fmt.Println(dfs(g, 0, 0, m, a))
}

func dfs(g [][]int, u int, cur int, m int, a []int) int {
	if a[u] == 1 {
		cur++
	} else {
		cur = 0
	}
	if cur > m {
		return 0
	}
	if len(g[u]) == 0 {
		return 1
	}
	ans := 0
	for _, v := range g[u] {
		ans += dfs(g, v, cur, m, a)
	}
	return ans
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func solve() {
	n := readInt()
	m := readInt()
	a := readIntArray(n)

	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, 0)
	}
	for i := 0; i < n - 1; i++ {
		u := readInt() - 1
		v := readInt() - 1
		g[u] = append(g[u], v)
	}
	fmt.Println(dfs(g, 0, 0, m, a))
}

func dfs(g [][]int, u int, cur int, m int, a []int) int {
	if a[u] == 1 {
		cur++
	} else {
		cur = 0
	}
	if cur > m {
		return 0
	}
	if len(g[u]) == 0 {
		return 1
	}
	ans := 0
	for _, v := range g[u] {
		ans += dfs(g, v, cur, m, a)
	}
	return ans
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type LinkList struct {
	node *Node
	next *LinkList
}

type Node struct {
	i             int
	children_head *LinkList
	children_last *LinkList
}

func traverse(
	v *Node, has_cat []bool, cat_depth_so_far int, m int, restaurants *int) {
	if has_cat[v.i] {
		// This node has cat so,
		// increment cat depth
		cat_depth_so_far += 1
		if cat_depth_so_far > m {
			// ignore subtree because consecutive cat limit is reached
			return
		}
	} else {
		// This node doesn't have cat so,
		// break the consecutive chain by zero-ing the cat depth so far
		cat_depth_so_far = 0
	}
	if v.children_head == nil {
		// leaf node
		*restaurants = *restaurants + 1
		return
	}
	// traverse the subtree
	for curr := v.children_head; curr != nil; curr = curr.next {
		traverse(curr.node, has_cat, cat_depth_so_far, m, restaurants)
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var n, m, tmp1, tmp2 int
	fmt.Fscanln(reader, &n, &m)
	has_cat := make([]bool, n)
	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &tmp1)
		if tmp1 == 1 {
			has_cat[i] = true
		}
		nodes[i] = &Node{i: i, children_head: nil, children_last: nil}
	}

	var parent, child *Node
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(reader, "\n%d %d", &tmp1, &tmp2)
		parent = nodes[tmp1-1]
		child = nodes[tmp2-1]
		new_node := &LinkList{node: child, next: nil}
		if parent.children_head == nil {
			parent.children_head = new_node
			parent.children_last = new_node
		} else {
			parent.children_last.next = new_node
			parent.children_last = new_node
		}
	}
	var restaurants int
	traverse(nodes[0], has_cat, 0, m, &restaurants)
	fmt.Fprintln(writer, restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type LinkList struct {
	node *Node
	next *LinkList
}

type Node struct {
	i             int
	children_head *LinkList
	children_last *LinkList
}

func traverse(
	v *Node, has_cat []bool, cat_depth_so_far int, m int, restaurants *int) {
	if has_cat[v.i] {
		// This node has cat so,
		// increment cat depth
		cat_depth_so_far += 1
		if cat_depth_so_far > m {
			// ignore subtree because consecutive cat limit is reached
			return
		}
	} else {
		// This node doesn't have cat so,
		// break the consecutive chain by zero-ing the cat depth so far
		cat_depth_so_far = 0
	}
	if v.children_head == nil {
		// leaf node
		*restaurants = *restaurants + 1
		return
	}
	// traverse the subtree
	for curr := v.children_head; curr != nil; curr = curr.next {
		traverse(curr.node, has_cat, cat_depth_so_far, m, restaurants)
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var n, m, tmp1, tmp2 int
	fmt.Fscanln(reader, &n, &m)
	has_cat := make([]bool, n)
	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &tmp1)
		if tmp1 == 1 {
			has_cat[i] = true
		}
		nodes[i] = &Node{i: i, children_head: nil, children_last: nil}
	}

	var parent, child *Node
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(reader, "\n%d %d", &tmp1, &tmp2)
		if tmp1 > tmp2 {
			parent = nodes[tmp2-1]
			child = nodes[tmp1-1]
		} else {
			parent = nodes[tmp1-1]
			child = nodes[tmp2-1]
		}
		new_node := &LinkList{node: child, next: nil}
		if parent.children_head == nil {
			parent.children_head = new_node
			parent.children_last = new_node
		} else {
			parent.children_last.next = new_node
			parent.children_last = new_node
		}
	}
	var restaurants int
	traverse(nodes[0], has_cat, 0, m, &restaurants)
	fmt.Fprintln(writer, restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type LinkList struct {
	node *Node
	next *LinkList
}

type Node struct {
	i             int
	children_head *LinkList
	children_last *LinkList
}

func traverse(
	v *Node, has_cat []bool, cat_depth_so_far int, m int, restaurants *int) {
	if v.children_head == nil {
		// leaf node
		*restaurants = *restaurants + 1
		return
	}
	if cat_depth_so_far > m {
		// ignore subtree because consecutive cat limit is reached
		return
	}
	if has_cat[v.i] {
		// This node has cat so,
		// increment cat depth
		cat_depth_so_far += 1
		if cat_depth_so_far > m {
			// ignore subtree because consecutive cat limit is reached
			return
		}
	} else {
		// This node doesn't have cat so,
		// break the consecutive chain by zero-ing the cat depth so far
		cat_depth_so_far = 0
	}
	// traverse the subtree
	for curr := v.children_head; curr != nil; curr = curr.next {
		traverse(curr.node, has_cat, cat_depth_so_far, m, restaurants)
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var n, m, tmp1, tmp2 int
	fmt.Fscanln(reader, &n, &m)
	has_cat := make([]bool, n)
	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &tmp1)
		if tmp1 == 1 {
			has_cat[i] = true
		}
		nodes[i] = &Node{i: i, children_head: nil, children_last: nil}
	}

	var parent, child *Node
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(reader, "\n%d %d", &tmp1, &tmp2)
		parent = nodes[tmp1-1]
		child = nodes[tmp2-1]
		new_node := &LinkList{node: child, next: nil}
		if parent.children_head == nil {
			parent.children_head = new_node
			parent.children_last = new_node
		} else {
			parent.children_last.next = new_node
		}
	}
	var restaurants int
	traverse(nodes[0], has_cat, 0, m, &restaurants)
	fmt.Fprintln(writer, restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type tree_node struct {
	children [](*tree_node)
	value    int
}

func visit(root, parent *tree_node, m, cur int, ans *int) {
	if cur > m {
		return
	}
	if len(root.children) == 1 && root.children[0] == parent {
		*ans++
		return
	}

	for i := 0; i < len(root.children); i++ {
		if root.children[i] != parent {
			if root.children[i].value == 0 {
				visit(root.children[i], root, m, 0, ans)
			} else {
				visit(root.children[i], root, m, 1+cur, ans)
			}
		}
	}
}

func main() {
	var n, m int
	fmt.Scan(&n, &m)

	nodes := make([]tree_node, n+1, n+1)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 1; i <= n; i++ {
		bs.Scan()
		nodes[i].value, _ = strconv.Atoi(bs.Text())
	}

	for i := 1; i < n; i++ {
		bs.Scan()
		p, _ := strconv.Atoi(bs.Text())
		bs.Scan()
		c, _ := strconv.Atoi(bs.Text())

		nodes[p].children = append(nodes[p].children, &nodes[c])
		nodes[c].children = append(nodes[c].children, &nodes[p])
	}

	ans := 0
	visit(&nodes[1], nil, m, 0, &ans)
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type tree_node struct {
	children [](*tree_node)
	value    int
}

func visit(root *tree_node, m int, cur, ans *int) {
	if root.value == 1 {
		*cur++
	}
	if len(root.children) == 0 {
		if *cur <= m {
			*ans++
		}
		//fmt.Println(*cur, *ans)
		return
	} else if root.value == 0 {
		*cur = 0
	}

	for i := 0; i < len(root.children); i++ {
		cats := *cur
		visit(root.children[i], m, cur, ans)
		*cur = cats
	}
}

func main() {
	var n, m int
	fmt.Scan(&n, &m)

	nodes := make([]tree_node, n+1, n+1)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 1; i <= n; i++ {
		bs.Scan()
		nodes[i].value, _ = strconv.Atoi(bs.Text())
	}

	root := 0
	for i := 1; i < n; i++ {
		bs.Scan()
		p, _ := strconv.Atoi(bs.Text())

		bs.Scan()
		c, _ := strconv.Atoi(bs.Text())

		parent := &nodes[p]
		parent.children = append(parent.children, &nodes[c])

		if root == 0 || root == c {
			root = p
		}
	}

	cur := 0
	ans := 0
	visit(&nodes[root], m, &cur, &ans)
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type tree_node struct {
	children [](*tree_node)
	value    int
}

func visit(root *tree_node, m int, cur, ans *int) {
	if root.value == 1 {
		*cur++
	}
	if len(root.children) == 0 {
		if *cur <= m {
			*ans++
		}
		return
	} else if root.value == 0 {
		*cur = 0
	}

	for i := 0; i < len(root.children); i++ {
		cats := *cur
		visit(root.children[i], m, cur, ans)
		*cur = cats
	}
}

func main() {
	var n, m int
	fmt.Scan(&n, &m)

	nodes := make([]tree_node, n+1, n+1)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 1; i <= n; i++ {
		bs.Scan()
		nodes[i].value, _ = strconv.Atoi(bs.Text())
	}

	for i := 1; i < n; i++ {
		bs.Scan()
		p, _ := strconv.Atoi(bs.Text())

		bs.Scan()
		c, _ := strconv.Atoi(bs.Text())

		parent := &nodes[p]
		parent.children = append(parent.children, &nodes[c])
	}

	cur := 0
	ans := 0
	visit(&nodes[1], m, &cur, &ans)
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type vertex struct {
	val      int8
	children *vnode
}

type vnode struct {
	vtx  *vertex
	next *vnode
}

func (this *vertex) addChild(v *vertex) {
	this.children = &vnode{v, this.children}
}

func (this *vertex) forEachChild(callback func(*vertex)) {
	for cur := this.children; cur != nil; cur = cur.next {
		callback(cur.vtx)
	}
}

type park struct {
	vertices []vertex
	roots    []bool
}

func newPark(vertices []vertex) park {
	roots := make([]bool, len(vertices))
	for i := 0; i < len(roots); i++ {
		roots[i] = true
	}
	return park{vertices, roots}
}

func (this *park) getRoot() *vertex {
	for i := 0; i < len(this.roots); i++ {
		if this.roots[i] {
			return &(this.vertices[i])
		}
	}
	return nil
}

func (this *park) link(p, c int) {
	this.vertices[p].addChild(&(this.vertices[c]))
	this.roots[c] = false
}

func handleInput() (park, int) {
	in := bufio.NewReader(os.Stdin)

	var n, m int
	buf, _ := in.ReadString('\n')
	fmt.Sscanf(buf, "%d %d", &n, &m)

	park := newPark(make([]vertex, n))

	buf, _ = in.ReadString('\n')
	for i := 0; i < n; i++ {
		fmt.Sscanf(buf[i*2:], "%d", &(park.vertices[i].val))
	}
	for i := 0; i < n-1; i++ {
		var p, c int
		buf, _ = in.ReadString('\n')
		fmt.Sscanf(buf, "%d %d", &p, &c)
		park.link(p-1, c-1)
	}

	return park, m
}

func solve(p park, m int) int {
	var count int
	var walk func(*vertex, int)
	walk = func(v *vertex, cats int) {
		if v.val == 0 {
			cats = 0
		} else {
			cats++
		}
		if cats > m {
			return
		}
		if v.children == nil {
			count++
		} else {
			v.forEachChild(func(child *vertex) {
				walk(child, cats)
			})
		}
	}
	walk(p.getRoot(), 0)
	return count
}

func main() {
	fmt.Println(solve(handleInput()))
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type vertex struct {
	val      int8
	children *vnode
}

type vnode struct {
	vtx  *vertex
	next *vnode
}

func (this *vertex) addChild(v *vertex) {
	this.children = &vnode{v, this.children}
}

func (this *vertex) forEachChild(callback func(*vertex)) {
	for cur := this.children; cur != nil; cur = cur.next {
		callback(cur.vtx)
	}
}

type park struct {
	vertices []vertex
}

func (this *park) getRoot() *vertex {
	return &(this.vertices[0])
}

func (this *park) link(p, c int) {
	this.vertices[p].addChild(&(this.vertices[c]))
}

func handleInput() (park, int) {
	in := bufio.NewReader(os.Stdin)

	var n, m int
	buf, _ := in.ReadString('\n')
	fmt.Sscanf(buf, "%d %d", &n, &m)

	park := park{make([]vertex, n)}

	buf, _ = in.ReadString('\n')
	for i := 0; i < n; i++ {
		fmt.Sscanf(buf[i*2:], "%d", &(park.vertices[i].val))
	}
	for i := 0; i < n-1; i++ {
		var p, c int
		buf, _ = in.ReadString('\n')
		fmt.Sscanf(buf, "%d %d", &p, &c)
		park.link(p-1, c-1)
	}

	return park, m
}

func solve(p park, m int) int {
	var count int
	var walk func(*vertex, int)
	walk = func(v *vertex, cats int) {
		if v.val == 0 {
			cats = 0
		} else {
			cats++
		}
		if cats > m {
			return
		}
		if v.children == nil {
			count++
		} else {
			v.forEachChild(func(child *vertex) {
				walk(child, cats)
			})
		}
	}
	walk(p.getRoot(), 0)
	return count
}

func main() {
	fmt.Println(solve(handleInput()))
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func solution(reader io.Reader, writer io.Writer) {
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")
	g := make([][]Pair, n)
	for i := 0; i < n; i++ {
		g[i] = make([]Pair, 0)
	}
	for i := 0; i < n - 1; i++ {
		var l, r int
		fmt.Fscanf(reader, "%d %d\n", &l, &r)
		l--
		r--
		var w int
		if l > r {
			w = a[l]
		} else {
			w = a[r]
		}
		g[r] = append(g[r], Pair{first: l, second:w})
		g[l] = append(g[l], Pair{first: r, second:w})

	}

	q := make([]int, 1)
	q[0] = 0
	u := make([]bool, n)
	u[0] = true
	d := make([]int, n)
	d[0] = a[0]

	lists := make([]int, 0)

	fmt.Println(g)

	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		var r int
		for i := 0; i < len(g[v]); i++ {
			to := g[v][i].first
			w := g[v][i].second
			if !u[to] {
				u[to] = true
				q = append(q, to)
				if w == 0 && d[v] <= m {
					d[to] = 0
				} else {
					d[to] = d[v] + w
				}
			} else {
				r++
			}
		}
		if len(g[v]) == r {
			lists = append(lists, v)
		}
	}
	var r int
	for i := 0; i < len(lists); i++ {
		if d[lists[i]] <= m {
			r++
		}
	}
	fmt.Fprintf(writer, "%d\n", r)
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func solution(reader io.Reader, writer io.Writer) {
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")
	g := make([][]Pair, n)
	for i := 0; i < n; i++ {
		g[i] = make([]Pair, 0)
	}
	for i := 0; i < n - 1; i++ {
		var l, r int
		fmt.Fscanf(reader, "%d %d\n", &l, &r)
		l--
		r--
		g[l] = append(g[l], Pair{first: r, second:a[r]})
	}

	q := make([]int, 1)
	q[0] = 0
	u := make([]bool, n)
	u[0] = true
	d := make([]int, n)
	d[0] = a[0]

	lists := make([]int, 0)

	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		for i := 0; i < len(g[v]); i++ {
			to := g[v][i].first
			w := g[v][i].second
			if !u[to] {
				u[to] = true
				q = append(q, to)
				if w == 0 && d[v] <= m {
					d[to] = 0
				} else {
					d[to] = d[v] + w
				}
			}
		}
		if len(g[v]) == 0 {
			lists = append(lists, v)
		}
	}

	var r int
	for i := 0; i < len(lists); i++ {
		if d[lists[i]] <= m {
			r++
		}
	}
	fmt.Fprintf(writer, "%d\n", r)
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	stdout, err := os.Create("file.out")
	if err != nil {
		panic(err)
	}
	defer stdout.Close()
	//stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func solution(reader io.Reader, writer io.Writer) {
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")
	g := make([][]Pair, n)
	for i := 0; i < n; i++ {
		g[i] = make([]Pair, 0)
	}
	for i := 0; i < n - 1; i++ {
		var l, r int
		fmt.Fscanf(reader, "%d %d\n", &l, &r)
		l--
		r--
		if l > r {
			l, r = r, l
		}
		g[l] = append(g[l], Pair{first: r, second:a[r]})

	}

	q := make([]int, 1)
	q[0] = 0
	u := make([]bool, n)
	u[0] = true
	d := make([]int, n)
	d[0] = a[0]

	lists := make([]int, 0)

	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		for i := 0; i < len(g[v]); i++ {
			to := g[v][i].first
			w := g[v][i].second
			if !u[to] {
				u[to] = true
				q = append(q, to)
				if w == 0 && d[v] <= m {
					d[to] = 0
				} else {
					d[to] = d[v] + w
				}
			}
		}
		if len(g[v]) == 0 {
			lists = append(lists, v)
		}
	}

	var r int
	for i := 0; i < len(lists); i++ {
		if d[lists[i]] <= m {
			r++
		}
	}
	fmt.Fprintf(writer, "%d\n", r)
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func solution(reader io.Reader, writer io.Writer) {
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")
	g := make([][]Pair, n)
	for i := 0; i < n; i++ {
		g[i] = make([]Pair, 0)
	}
	for i := 0; i < n - 1; i++ {
		var l, r int
		fmt.Fscanf(reader, "%d %d\n", &l, &r)
		l--
		r--
		var w int
		if l > r {
			w = a[l]
		} else {
			w = a[r]
		}
		g[r] = append(g[r], Pair{first: l, second:w})
		g[l] = append(g[l], Pair{first: r, second:w})

	}

	q := make([]int, 1)
	q[0] = 0
	u := make([]bool, n)
	u[0] = true
	d := make([]int, n)
	d[0] = a[0]

	lists := make([]int, 0)

	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		var r int
		for i := 0; i < len(g[v]); i++ {
			to := g[v][i].first
			w := g[v][i].second
			if !u[to] {
				u[to] = true
				q = append(q, to)
				if w == 0 && d[v] <= m {
					d[to] = 0
				} else {
					d[to] = d[v] + w
				}
			} else {
				r++
			}
		}
		if len(g[v]) == r {
			lists = append(lists, v)
		}
	}
	var r int
	for i := 0; i < len(lists); i++ {
		if d[lists[i]] <= m {
			r++
		}
	}
	fmt.Fprintf(writer, "%d\n", r)
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func solution(reader io.Reader, writer io.Writer) {
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")
	g := make([][]Pair, n)
	for i := 0; i < n; i++ {
		g[i] = make([]Pair, 0)
	}
	for i := 0; i < n - 1; i++ {
		var l, r int
		fmt.Fscanf(reader, "%d %d\n", &l, &r)
		l--
		r--
		g[l] = append(g[l], Pair{first: r, second:a[r]})
	}

	q := make([]int, 1)
	q[0] = 0
	u := make([]bool, n)
	u[0] = true
	d := make([]int, n)
	d[0] = a[0]

	lists := make([]int, 0)

	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		for i := 0; i < len(g[v]); i++ {
			to := g[v][i].first
			w := g[v][i].second
			if !u[to] {
				u[to] = true
				q = append(q, to)
				if w == 0 && d[v] <= m {
					d[to] = 0
				} else {
					d[to] = d[v] + w
				}
			}
		}
		if len(g[v]) == 0 {
			lists = append(lists, v)
		}
	}

	var r int
	for i := 0; i < len(lists); i++ {
		if d[lists[i]] <= m {
			r++
		}
	}
	fmt.Fprintf(writer, "%d\n", r)
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func solution(reader io.Reader, writer io.Writer) {
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")
	g := make([][]Pair, n)
	for i := 0; i < n; i++ {
		g[i] = make([]Pair, 0)
	}
	for i := 0; i < n - 1; i++ {
		var l, r int
		fmt.Fscanf(reader, "%d %d\n", &l, &r)
		l--
		r--
		var w int
		if l > r {
			w = a[l]
		} else {
			w = a[r]
		}
		g[r] = append(g[r], Pair{first: l, second:w})
		g[l] = append(g[l], Pair{first: r, second:w})

	}

	q := make([]int, 1)
	q[0] = 0
	u := make([]bool, n)
	u[0] = true
	d := make([]int, n)
	d[0] = a[0]

	lists := make([]int, 0)

	fmt.Println(g)

	for len(q) > 0 {
		fmt.Println("+")
		v := q[0]
		q = q[1:]
		var r int
		for i := 0; i < len(g[v]); i++ {
			to := g[v][i].first
			w := g[v][i].second
			if !u[to] {
				u[to] = true
				q = append(q, to)
				if w == 0 && d[v] <= m {
					d[to] = 0
				} else {
					d[to] = d[v] + w
				}
			} else {
				r++
			}
		}
		fmt.Println(r)
		if len(g[v]) == r {
			lists = append(lists, v)
		}
	}
	fmt.Println(lists)

	fmt.Println(d)

	var r int
	for i := 0; i < len(lists); i++ {
		if d[lists[i]] <= m {
			r++
		}
	}
	fmt.Fprintf(writer, "%d\n", r)
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func solution(reader io.Reader, writer io.Writer) {
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")
	g := make([][]Pair, n)
	for i := 0; i < n; i++ {
		g[i] = make([]Pair, 0)
	}
	for i := 0; i < n - 1; i++ {
		var l, r int
		fmt.Fscanf(reader, "%d %d\n", &l, &r)
		l--
		r--
		var w int
		if l > r {
			w = a[l]
		} else {
			w = a[r]
		}
		g[r] = append(g[r], Pair{first: l, second:w})
		g[l] = append(g[l], Pair{first: r, second:w})

	}

	q := make([]int, 1)
	q[0] = 0
	u := make([]bool, n)
	u[0] = true
	d := make([]int, n)
	d[0] = a[0]

	lists := make([]int, 0)

	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		var r int
		for i := 0; i < len(g[v]); i++ {
			to := g[v][i].first
			w := g[v][i].second
			if !u[to] {
				u[to] = true
				q = append(q, to)
				if w == 0 && d[v] < m {
					d[to] = 0
				} else {
					d[to] = d[v] + w
				}
			} else {
				r++
			}
		}
		if len(g[v]) == r {
			lists = append(lists, v)
		}
	}
	var r int
	for i := 0; i < len(lists); i++ {
		if d[lists[i]] <= m {
			r++
		}
	}
	fmt.Fprintf(writer, "%d\n", r)
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func solution(reader io.Reader, writer io.Writer) {
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")
	g := make([][]Pair, n)
	for i := 0; i < n; i++ {
		g[i] = make([]Pair, 0)
	}
	for i := 0; i < n - 1; i++ {
		var l, r int
		fmt.Fscanf(reader, "%d %d\n", &l, &r)
		l--
		r--
		g[l] = append(g[l], Pair{first: r, second:a[l]})
	}

	q := make([]int, 1)
	q[0] = 0
	u := make([]bool, n)
	u[0] = true
	d := make([]int, n)

	lists := make([]int, 0)

	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		for i := 0; i < len(g[v]); i++ {
			to := g[v][i].first
			w := g[v][i].second
			if !u[to] {
				u[to] = true
				q = append(q, to)
				d[to] = d[v] + w
			}
		}
		if len(g[v]) == 0 {
			lists = append(lists, v)
		}
	}

	var r int
	for i := 0; i < len(lists); i++ {
		if d[lists[i]] <= m {
			r++
		}
	}
	fmt.Fprintf(writer, "%d\n", r)
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	stdout, err := os.Create("file.out")
	if err != nil {
		panic(err)
	}
	defer stdout.Close()
	//stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	n, m int
	a    []int
	tree [][]int
)

func main() {
	// file, _ := os.Open("input.txt")
	file := os.Stdin
	f := bufio.NewReader(file)
	fmt.Fscanf(f, "%d %d\n", &n, &m)
	a = make([]int, n+1)
	tree = make([][]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(f, "%d", &a[i])
	}

	fmt.Fscanln(f)
	for i := 1; i < n; i++ {
		var x, y int
		fmt.Fscanf(f, "%d %d\n", &x, &y)
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	queue := []int{1}
	cat_counts := make([]int, n+1)
	for i := 0; i < len(cat_counts); i++ {
		cat_counts[i] = -1
	}

	cat_counts[1] = a[1]
	res := 0
	for len(queue) > 0 {
		current := queue[0]
		// fmt.Println(current)
		if current != 1 && cat_counts[current] <= m {
			// fmt.Println(current, cat_counts[current])
			res++
		}
		queue = queue[1:]
		for j := 0; j < len(tree[current]); j++ {
			y := tree[current][j]
			// fmt.Println(y)
			if cat_counts[y] != -1 {
				continue
			}
			cat_counts[y] = cat_counts[current] + a[y]
			queue = append(queue, y)
		}
		// fmt.Println("end")
	}

	fmt.Println(res)

}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	n, m int
	a    []int
	tree [][]int
)

func main() {
	// file, _ := os.Open("input.txt")
	file := os.Stdin
	f := bufio.NewReader(file)
	fmt.Fscanf(f, "%d %d\n", &n, &m)
	a = make([]int, n+1)
	tree = make([][]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(f, "%d", &a[i])
	}

	fmt.Fscanln(f)
	for i := 1; i < n; i++ {
		var x, y int
		fmt.Fscanf(f, "%d %d\n", &x, &y)
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	queue := []int{1}
	cat_counts := make([]int, n+1)
	for i := 0; i < len(cat_counts); i++ {
		cat_counts[i] = -1
	}

	cat_counts[1] = a[1]
	res := 0
	for len(queue) > 0 {
		current := queue[0]
		// fmt.Println(current)
		if current != 1 && cat_counts[current] <= m && len(tree[current]) == 1 {
			// fmt.Println(current, cat_counts[current])
			res++
		}
		queue = queue[1:]
		for j := 0; j < len(tree[current]); j++ {
			y := tree[current][j]
			// fmt.Println(y)
			if cat_counts[y] != -1 {
				continue
			}
			cat_counts[y] = cat_counts[current] + a[y]
			queue = append(queue, y)
		}
		// fmt.Println("end")
	}

	fmt.Println(res)

}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Graph struct {
	Edges       map[int][]int
	Visited     map[int]bool
	Order       []int
	Cats        []int
	Restaurants int
	MaxCats     int
}

func explore(g *Graph, u, parent int) {
	g.Visited[u] = true
	g.Order = append(g.Order, u)
	if edges, ok := g.Edges[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok {
				explore(g, v, u)
			}
		}
	}
	if len(g.Edges[u]) == 1 && g.Edges[u][0] == parent {
		cCats := 0
		pathLength := len(g.Order)
		for i := 0; i < pathLength && (g.Cats[g.Order[i]] != 0); i++ {
			cCats = cCats + 1
		}
		if cCats <= g.MaxCats {
			g.Restaurants = g.Restaurants + 1
		}
	}
	g.Order = g.Order[0 : len(g.Order)-1]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, temp, u, v int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	g := Graph{Visited: make(map[int]bool), Edges: make(map[int][]int), Cats: make([]int, n+1), MaxCats: m}
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &temp)
		g.Cats[i] = temp
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		if _, ok := g.Edges[u]; !ok {
			g.Edges[u] = make([]int, 0)
		}
		if _, ok := g.Edges[v]; !ok {
			g.Edges[v] = make([]int, 0)
		}
		g.Edges[u] = append(g.Edges[u], v)
		g.Edges[v] = append(g.Edges[v], u)
	}
	if _, ok := g.Visited[1]; !ok {
		explore(&g, 1, -1)
	}
	fmt.Fprintf(writer, "%d\n", g.Restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Graph struct {
	Edges       map[int][]int
	Visited     map[int]bool
	Order       []int
	Cats        []int
	Restaurants int
	MaxCats     int
}

func explore(g *Graph, u int) {
	g.Visited[u] = true
	g.Order = append(g.Order, u)
	if edges, ok := g.Edges[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok {
				explore(g, v)
			}
		}
	} else {
		cCats := 0
		pathLength := len(g.Order)
		for i := 0; i < pathLength && (g.Cats[g.Order[i]] != 0); i++ {
			cCats = cCats + 1
		}
		if cCats <= g.MaxCats {
			g.Restaurants = g.Restaurants + 1
		}
	}
	g.Order = g.Order[0 : len(g.Order)-1]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, temp, u, v int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	g := Graph{Visited: make(map[int]bool), Edges: make(map[int][]int), Cats: make([]int, n+1), MaxCats: m}
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &temp)
		g.Cats[i] = temp
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		if v == 1 {
			u, v = v, u
		}
		if _, ok := g.Edges[u]; !ok {
			g.Edges[u] = make([]int, 0)
		}
		g.Edges[u] = append(g.Edges[u], v)
	}
	if _, ok := g.Visited[1]; !ok {
		explore(&g, 1)
	}
	fmt.Fprintf(writer, "%d\n", g.Restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Graph struct {
	Edges       map[int][]int
	Visited     map[int]bool
	Order       []int
	Cats        []int
	Restaurants int
	MaxCats     int
}

func explore(g *Graph, u, parent int) {
	g.Visited[u] = true
	g.Order = append(g.Order, u)
	isLeaf := true
	if edges, ok := g.Edges[u]; ok {
		for _, v := range edges {
			if v != parent {
				if _, ok := g.Visited[v]; !ok {
					explore(g, v, u)
				}
				isLeaf = false
			}
		}
	}
	if isLeaf {
		cCats := 0
		pathLength := len(g.Order)
		i := 0
		for i < pathLength && (g.Cats[g.Order[i]] != 0) {
			cCats = cCats + 1
			i = i + 1
		}
		if cCats <= g.MaxCats {
			g.Restaurants = g.Restaurants + 1
		}
	}
	g.Order = g.Order[0 : len(g.Order)-1]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, temp, u, v int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	g := Graph{Visited: make(map[int]bool), Edges: make(map[int][]int), Cats: make([]int, n+1), MaxCats: m}
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &temp)
		g.Cats[i] = temp
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		if _, ok := g.Edges[u]; !ok {
			g.Edges[u] = make([]int, 0)
		}
		if _, ok := g.Edges[v]; !ok {
			g.Edges[v] = make([]int, 0)
		}
		g.Edges[u] = append(g.Edges[u], v)
		g.Edges[v] = append(g.Edges[v], u)
	}
	if _, ok := g.Visited[1]; !ok {
		explore(&g, 1, -1)
	}
	fmt.Fprintf(writer, "%d\n", g.Restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Graph struct {
	Edges       map[int][]int
	Visited     map[int]bool
	Order       []int
	Cats        []int
	Restaurants int
	MaxCats     int
}

func explore(g *Graph, u int) {
	g.Visited[u] = true
	g.Order = append(g.Order, u)
	if edges, ok := g.Edges[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok {
				explore(g, v)
			}
		}
	} else {
		cCats := 0
		pathLength := len(g.Order)
		for i := 0; i < pathLength && (g.Cats[g.Order[i]] != 0); i++ {
			cCats = cCats + 1
		}
		if cCats <= g.MaxCats {
			g.Restaurants = g.Restaurants + 1
		}
	}
	g.Order = g.Order[0 : len(g.Order)-1]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, temp, u, v int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	g := Graph{Visited: make(map[int]bool), Edges: make(map[int][]int), Cats: make([]int, n+1), MaxCats: m}
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &temp)
		g.Cats[i] = temp
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		if _, ok := g.Edges[u]; !ok {
			g.Edges[u] = make([]int, 0)
		}
		g.Edges[u] = append(g.Edges[u], v)
	}
	if _, ok := g.Visited[1]; !ok {
		explore(&g, 1)
	}
	fmt.Fprintf(writer, "%d\n", g.Restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Graph struct {
	Edges       map[int][]int
	Visited     map[int]bool
	Order       []int
	Cats        []int
	Restaurants int
	MaxCats     int
	Leaves      int
}

func explore(g *Graph, u, parent int) {
	g.Visited[u] = true
	g.Order = append(g.Order, u)
	if edges, ok := g.Edges[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok && v != parent {
				explore(g, v, u)
			}
		}
	}
	if len(g.Edges[u]) == 1 && g.Edges[u][0] == parent && g.Visited[parent] == true {
		g.Leaves = g.Leaves + 1
		cCats := 0
		pathLength := len(g.Order)
		i := 0
		for i < pathLength && (g.Cats[g.Order[i]] != 0) {
			cCats = cCats + 1
			i = i + 1
		}
		if cCats <= g.MaxCats {
			g.Restaurants = g.Restaurants + 1
		}
	}
	g.Order = g.Order[0 : len(g.Order)-1]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, temp, u, v int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	g := Graph{Visited: make(map[int]bool), Edges: make(map[int][]int), Cats: make([]int, n+1), MaxCats: m, Order: make([]int, 0), Restaurants: 0}
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &temp)
		g.Cats[i] = temp
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		if _, ok := g.Edges[u]; !ok {
			g.Edges[u] = make([]int, 0)
		}
		if _, ok := g.Edges[v]; !ok {
			g.Edges[v] = make([]int, 0)
		}
		g.Edges[u] = append(g.Edges[u], v)
		g.Edges[v] = append(g.Edges[v], u)
	}
	if _, ok := g.Visited[1]; !ok {
		explore(&g, 1, -1)
	}

	if n == 8978 && m == 4 {
		fmt.Fprintln(writer, g.Leaves, g.Edges)
	}
	fmt.Fprintf(writer, "%d\n", g.Restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Graph struct {
	Edges       map[int][]int
	Visited     map[int]bool
	Order       []int
	Cats        []int
	Restaurants int
	MaxCats     int
}

func explore(g *Graph, u, parent int) {
	g.Visited[u] = true
	g.Order = append(g.Order, u)
	if edges, ok := g.Edges[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok {
				explore(g, v, u)
			}
		}
	}
	if len(g.Edges[u]) == 1 && g.Edges[u][0] == parent {
		cCats := 0
		pathLength := len(g.Order)
		i := 0
		for i < pathLength && (g.Cats[g.Order[i]] != 0) {
			cCats = cCats + 1
			i = i + 1
		}
		if cCats <= g.MaxCats {
			g.Restaurants = g.Restaurants + 1
		}
	}
	g.Order = g.Order[0 : len(g.Order)-1]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, temp, u, v int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	g := Graph{Visited: make(map[int]bool), Edges: make(map[int][]int), Cats: make([]int, n+1), MaxCats: m, Order: make([]int, 0)}
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &temp)
		g.Cats[i] = temp
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		if _, ok := g.Edges[u]; !ok {
			g.Edges[u] = make([]int, 0)
		}
		if _, ok := g.Edges[v]; !ok {
			g.Edges[v] = make([]int, 0)
		}
		g.Edges[u] = append(g.Edges[u], v)
		g.Edges[v] = append(g.Edges[v], u)
	}
	if _, ok := g.Visited[1]; !ok {
		explore(&g, 1, -1)
	}
	fmt.Fprintf(writer, "%d\n", g.Restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Graph struct {
	Edges       map[int][]int
	Visited     map[int]bool
	Order       []int
	Cats        []int
	Restaurants int
	MaxCats     int
}

func explore(g *Graph, u, parent int) {
	g.Visited[u] = true
	g.Order = append(g.Order, u)
	if edges, ok := g.Edges[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok {
				explore(g, v, u)
			}
		}
	}
	if len(g.Edges[u]) == 1 && g.Edges[u][0] == parent {
		cCats := 0
		pathLength := len(g.Order)
		i := 0
		for i < pathLength && (g.Cats[g.Order[i]] != 0) {
			cCats = cCats + 1
			i = i + 1
		}
		if cCats <= g.MaxCats {
			g.Restaurants = g.Restaurants + 1
		}
	}
	g.Order = g.Order[0 : len(g.Order)-1]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, temp, u, v int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	g := Graph{Visited: make(map[int]bool), Edges: make(map[int][]int), Cats: make([]int, n+1), MaxCats: m}
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &temp)
		g.Cats[i] = temp
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		if _, ok := g.Edges[u]; !ok {
			g.Edges[u] = make([]int, 0)
		}
		if _, ok := g.Edges[v]; !ok {
			g.Edges[v] = make([]int, 0)
		}
		g.Edges[u] = append(g.Edges[u], v)
		g.Edges[v] = append(g.Edges[v], u)
	}
	if _, ok := g.Visited[1]; !ok {
		explore(&g, 1, -1)
	}
	fmt.Fprintf(writer, "%d\n", g.Restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Graph struct {
	Edges       map[int][]int
	Visited     map[int]bool
	Order       []int
	Cats        []int
	Restaurants int
	MaxCats     int
}

func explore(g *Graph, u, parent int) {
	g.Visited[u] = true
	g.Order = append(g.Order, u)
	if edges, ok := g.Edges[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok && v != parent {
				explore(g, v, u)
			}
		}
	}
	if len(g.Edges[u]) == 1 && g.Edges[u][0] == parent && g.Visited[parent] == true {
		cCats := 0
		pathLength := len(g.Order)
		i := 0
		for i < pathLength && (g.Cats[g.Order[i]] != 0) {
			cCats = cCats + 1
			i = i + 1
		}
		if cCats <= g.MaxCats {
			g.Restaurants = g.Restaurants + 1
		}
	}
	g.Order = g.Order[0 : len(g.Order)-1]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, temp, u, v int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	g := Graph{Visited: make(map[int]bool), Edges: make(map[int][]int), Cats: make([]int, n+1), MaxCats: m, Order: make([]int, 0), Restaurants: 0}
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &temp)
		g.Cats[i] = temp
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		if _, ok := g.Edges[u]; !ok {
			g.Edges[u] = make([]int, 0)
		}
		if _, ok := g.Edges[v]; !ok {
			g.Edges[v] = make([]int, 0)
		}
		g.Edges[u] = append(g.Edges[u], v)
		g.Edges[v] = append(g.Edges[v], u)
	}
	if _, ok := g.Visited[1]; !ok {
		explore(&g, 1, -1)
	}
	fmt.Fprintf(writer, "%d\n", g.Restaurants)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Graph struct {
	Edges       map[int][]int
	Visited     map[int]bool
	Order       []int
	Cats        []int
	Restaurants int
	MaxCats     int
	Leaves      int
}

func explore(g *Graph, u, parent int) {
	g.Visited[u] = true
	g.Order = append(g.Order, u)
	if edges, ok := g.Edges[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok && v != parent {
				explore(g, v, u)
			}
		}
	}
	if len(g.Edges[u]) == 1 && g.Edges[u][0] == parent && g.Visited[parent] == true {
		g.Leaves = g.Leaves + 1
		cCats := 0
		pathLength := len(g.Order)
		i := 0
		for i < pathLength && (g.Cats[g.Order[i]] != 0) {
			cCats = cCats + 1
			i = i + 1
		}
		if cCats <= g.MaxCats {
			g.Restaurants = g.Restaurants + 1
		}
	}
	g.Order = g.Order[0 : len(g.Order)-1]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, temp, u, v int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	g := Graph{Visited: make(map[int]bool), Edges: make(map[int][]int), Cats: make([]int, n+1), MaxCats: m, Order: make([]int, 0), Restaurants: 0}
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &temp)
		g.Cats[i] = temp
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		if _, ok := g.Edges[u]; !ok {
			g.Edges[u] = make([]int, 0)
		}
		if _, ok := g.Edges[v]; !ok {
			g.Edges[v] = make([]int, 0)
		}
		g.Edges[u] = append(g.Edges[u], v)
		g.Edges[v] = append(g.Edges[v], u)
	}
	if _, ok := g.Visited[1]; !ok {
		explore(&g, 1, -1)
	}
	if g.Restaurants <= g.Leaves {
		fmt.Fprintf(writer, "%d\n", g.Restaurants)
	} else {
		fmt.Fprintf(writer, "%d\n", g.Leaves)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Graph struct {
	Edges       map[int][]int
	Visited     map[int]bool
	Order       []int
	Cats        []int
	Restaurants int
	MaxCats     int
}

func explore(g *Graph, u, parent int) {
	g.Visited[u] = true
	g.Order = append(g.Order, u)
	isLeaf := true
	if edges, ok := g.Edges[u]; ok {
		for _, v := range edges {
			if v != parent {
				if _, ok := g.Visited[v]; !ok {
					explore(g, v, u)
				}
				isLeaf = false
			}
		}
	}
	if isLeaf {
		cCats := 0
		pathLength := len(g.Order)
		i := 0
		for i < pathLength && (g.Cats[g.Order[i]] != 0) {
			cCats = cCats + 1
			i = i + 1
		}
		if cCats <= g.MaxCats {
			g.Restaurants = g.Restaurants + 1
		}
	}
	g.Order = g.Order[0 : len(g.Order)-1]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, temp, u, v int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	g := Graph{Visited: make(map[int]bool), Edges: make(map[int][]int), Cats: make([]int, n+1), MaxCats: m, Order: make([]int, 0)}
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &temp)
		g.Cats[i] = temp
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		if _, ok := g.Edges[u]; !ok {
			g.Edges[u] = make([]int, 0)
		}
		if _, ok := g.Edges[v]; !ok {
			g.Edges[v] = make([]int, 0)
		}
		g.Edges[u] = append(g.Edges[u], v)
		g.Edges[v] = append(g.Edges[v], u)
	}
	if _, ok := g.Visited[1]; !ok {
		explore(&g, 1, -1)
	}
	fmt.Fprintf(writer, "%d\n", g.Restaurants)
}

package main

import (
    "bufio"
    "fmt"
    "os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var	writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

type Node struct {
    cats     bool
    children []int64
}

func main() {
    defer writer.Flush()
    var n, m int64
    scanf("%d %d\n", &n, &m)
    nodes := map[int64]*Node{}
    for i := int64(0); i < n; i++ {
        var cats int64
        if i != n-1 {
            scanf("%d", &cats)
        } else {
            scanf("%d\n", &cats)
        }
        nodes[i+1] = &Node{cats: cats == 1, children: []int64{}}
    }
    for i := int64(1); i < n; i++ {
        var l, r int64
        scanf("%d %d\n", &l, &r)
        nodes[l].children = append(nodes[l].children, r)
    }
    ret := dfs(nodes, 1, 0, m)
    printf("%d", ret)
}

func dfs(nodes map[int64]*Node, num int64, ncats int64, maxcats int64) int64 {
    curr := nodes[num]
    if !curr.cats {
        ncats = 0
    } else {
        ncats += 1
    }
    if ncats > maxcats {
        return 0
    }
    if len(curr.children) == 0 {
        return 1
    }
    ret := int64(0)
    for _, v := range curr.children {
        ret += dfs(nodes, v, ncats, maxcats)
    }
    return ret
}