package hackerrank;

import java.util.Scanner;

/**
 * https://www.hackerrank.com/challenges/even-tree
 */
public class EvenTree {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // TODO
    }
}

@@ -35,206 +35,2720 @@
 til.
-Scanner;%0A%0A/**%0A * https://www.hackerrank.com/challenges/even-tree%0A */%0Apublic class EvenTree %7B%0A    public static void main(String%5B%5D args) %7B%0A        Scanner in = new Scanner(System.in);%0A        // TODO
+ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Objects;%0Aimport java.util.Queue;%0Aimport java.util.Scanner;%0Aimport java.util.Set;%0A%0A/**%0A * https://www.hackerrank.com/challenges/even-tree%0A */%0Apublic class EvenTree %7B%0A    private static class Node %7B%0A        int value;%0A        int count;%0A%0A        private Node(int value) %7B%0A            this.value = value;%0A        %7D%0A%0A        @Override%0A        public boolean equals(Object o) %7B%0A            if (this == o) return true;%0A            if (o == null %7C%7C getClass() != o.getClass()) return false;%0A            Node node = (Node) o;%0A            return value == node.value;%0A        %7D%0A%0A        @Override%0A        public int hashCode() %7B%0A            return Objects.hash(value);%0A        %7D%0A    %7D%0A%0A    private static int evenTree(Node root, Map%3CInteger, Node%3E vertices,%0A                                Map%3CInteger, List%3CInteger%3E%3E adjList) %7B%0A        int count = 0;%0A        updateCounts(root, vertices, adjList);%0A        Queue%3CInteger%3E queue = new LinkedList%3C%3E();%0A        queue.add(root.value);%0A        while (!queue.isEmpty()) %7B%0A            int node = queue.remove();%0A            for (int adj : adjList.get(node)) %7B%0A                if (vertices.get(adj).count %25 2 == 0) %7B%0A                    count++;%0A                %7D%0A                queue.add(adj);%0A            %7D%0A        %7D%0A        return count;%0A    %7D%0A%0A    private static int updateCounts(Node root, Map%3CInteger, Node%3E vertices,%0A                                    Map%3CInteger, List%3CInteger%3E%3E adjList) %7B%0A        if (root == null) %7B%0A            return 0;%0A        %7D%0A        int count = 1;%0A        for (int adj : adjList.get(root.value)) %7B%0A            count += updateCounts(vertices.get(adj), vertices, adjList);%0A        %7D%0A        root.count = count;%0A        return root.count;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Scanner in = new Scanner(System.in);%0A        int n = in.nextInt();%0A        int m = in.nextInt();%0A        Map%3CInteger, Node%3E vertices = new HashMap%3C%3E();%0A        Map%3CInteger, List%3CInteger%3E%3E adjList = new HashMap%3C%3E();%0A        Set%3CInteger%3E nodes = new HashSet%3C%3E();%0A        for (int vertex = 1; vertex %3C= n; vertex++) %7B%0A            adjList.put(vertex, new ArrayList%3C%3E());%0A            vertices.put(vertex, new Node(vertex));%0A            nodes.add(vertex);%0A        %7D%0A        for (int edge = 0; edge %3C m; edge++) %7B%0A            int v1 = in.nextInt();%0A            int v2 = in.nextInt();%0A            nodes.remove(v1);%0A            adjList.get(v2).add(vertices.get(v1).value);%0A        %7D%0A        Node root = vertices.get(nodes.iterator().next());%0A        System.out.println(evenTree(root, vertices, adjList));
 %0A   

Solve Even Tree