@@ -0,0 +1,2717 @@
+// Author: Param Singh %3Cparamsingh258@gmail.com%3E%0A// Kefa and Park: http://codeforces.com/contest/580/problem/C%0A%0Aimport java.io.*;%0Aimport java.util.*;%0Apublic class Kefa %7B%0A    static class Node %7B%0A        List%3CNode%3E children;%0A        int id;%0A        int num;%0A        boolean cat;%0A        int left, right;%0A        Node() %7B%0A            left = right = 0;%0A            children = new ArrayList%3CNode%3E();%0A            num = 0;%0A            cat = false;%0A        %7D%0A%0A        void add_child(Node x) %7B%0A            num++;%0A            children.add(x);%0A        %7D%0A%0A        void add_cat() %7B%0A            cat = true;%0A        %7D%0A    %7D%0A%0A    public static int num_leafs(Node vertex, Node prev, int consecutive, int m) %7B%0A        int ans = 0;%0A        if (vertex.num == 1 && prev != null) %7B%0A            if (vertex.cat)%0A                consecutive++;%0A            if (m %3E= consecutive)%0A                ans = 1;%0A            else%0A                ans = 0;%0A        %7D%0A        else %7B%0A            if (vertex.cat) %7B%0A                if (m == consecutive)%0A                    ans = 0;%0A                else %7B%0A                    for (Node x: vertex.children) %7B%0A                        if (prev != null && prev.id == x.id)%0A                            continue;%0A                        ans += num_leafs(x, vertex, consecutive+1, m);%0A                    %7D%0A                %7D%0A            %7D%0A            else %7B%0A                for (Node x: vertex.children) %7B%0A                    if (prev != null && prev.id == x.id)%0A                        continue;%0A                    ans += num_leafs(x, vertex, 0, m);%0A                %7D%0A                return ans;%0A            %7D%0A        %7D%0A        return ans;%0A    %7D%0A%0A    public static void main(String%5B%5D args) throws Exception %7B%0A        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));%0A        String s%5B%5D = in.readLine().split(%22 %22);%0A        int n = Integer.parseInt(s%5B0%5D);%0A        int m = Integer.parseInt(s%5B1%5D);%0A%0A        s = in.readLine().split(%22 %22);%0A        Node tree%5B%5D = new Node%5Bn%5D;%0A%0A        for (int i = 0; i %3C n; i++) %7B%0A            tree%5Bi%5D = new Node();%0A            tree%5Bi%5D.id = i;%0A            if (s%5Bi%5D.equals(%221%22))%0A                tree%5Bi%5D.add_cat();%0A        %7D%0A%0A        if (n == 1) %7B%0A            if (tree%5B0%5D.cat && m != 0)%0A                System.out.println(0);%0A            else%0A                System.out.println(1);%0A            System.exit(0);%0A        %7D%0A%0A        int a, b;%0A        for (int i = 1; i %3C n; i++) %7B%0A            s = in.readLine().split(%22 %22);%0A            a = Integer.parseInt(s%5B0%5D);%0A            b = Integer.parseInt(s%5B1%5D);%0A            tree%5Ba-1%5D.add_child(tree%5Bb-1%5D);%0A            tree%5Bb-1%5D.add_child(tree%5Ba-1%5D);%0A        %7D%0A        System.out.println(num_leafs(tree%5B0%5D, null, 0, m));%0A    %7D%0A%7D%0A%0A

Solve Kefa and Park