33

I'm stuck on a code challenge, and I want a hint.

PROBLEM: You are given a tree data structure (without cycles) and are asked to remove as many "edges" (connections) as possible, creating smaller trees with even numbers of nodes. This problem is always solvable as there are an even number of nodes and connections.

Your task is to count the removed edges.

Input: The first line of input contains two integers N and M. N is the number of vertices and M is the number of edges. 2 <= N <= 100. Next M lines contains two integers ui and vi which specifies an edge of the tree. (1-based index)

Output: Print the number of edges removed.

Sample Input

10 9
2 1
3 1
4 3
5 2
6 1
7 2
8 6
9 8
10 8

Sample Output : 2

Explanation : On removing the edges (1, 3) and (1, 6), we can get the desired result.

Bob Jones
  • 37
  • 2
  • 11
g4ur4v
  • 3,210
  • 5
  • 32
  • 57

8 Answers8

23

I used BFS to travel through the nodes. First, maintain an array separately to store the total number of child nodes + 1. So, you can initially assign all the leaf nodes with value 1 in this array. Now start from the last node and count the number of children for each node. This will work in bottom to top manner and the array that stores the number of child nodes will help in runtime to optimize the code.

Once you get the array after getting the number of children nodes for all the nodes, just counting the nodes with even number of nodes gives the answer. Note: I did not include root node in counting in final step.

passy
  • 7,170
  • 5
  • 36
  • 38
g4ur4v
  • 3,210
  • 5
  • 32
  • 57
  • 6
    why is the answer is such that nodes with even number of successors except from root is right answer ? How to think through this at first place – Harshit Mar 28 '14 at 20:17
  • 1
    @user595169 The reason is to be able to split a tree into even components, the number of nodes in the tree must be even. And therefore, it follows the value is root would be an even number – grdvnl May 23 '14 at 12:40
  • 4
    Why do you use the words "child", "leaf" and "root"? A tree is an undirected graph (en.wikipedia.org/wiki/Tree_(graph_theory)). The images at hackerrank don't show directions nor use those words. Or did I misunderstood the problem? – Luís Soares Jan 29 '15 at 01:20
  • 1
    So pick any node and call it a root, you will get valid tree. – madabrowski Apr 26 '15 at 19:58
  • I thought only the root node could have more than two children. If this is true, then you couldn't pick another node to be root, IF any node has more than two children. – keyboardSmasher Jun 16 '15 at 00:10
  • Can you point out what is the intuition behind this strategy? For example sometimes you don't end removing an edge between the root and its children even if root has even count. – GeneralFailure Aug 31 '15 at 11:15
  • I am not sure if I am right. Tried doing the algorithm for the following tree, 8 7 1 2 1 3 1 4 2 5 3 6 4 7 5 8 However ended up counting the number of child nodes as 4 2 2 2 2 1 1 1 In this case the number of even elements are 4. However, the right answer is 3. Is this because of the flaw in the algorithm or am I doing it wrong? – Dhananjayan Santhanakrishnan Sep 29 '16 at 22:07
6

This is my solution. I didn't use bfs tree, just allocated another array for holding eachnode's and their children nodes total number.

import java.util.Scanner;
import java.util.Arrays;

public class Solution {
        public static void main(String[] args) {
                int tree[];
                int count[];

                Scanner scan = new Scanner(System.in);

                int N = scan.nextInt(); //points
                int M = scan.nextInt();

                tree = new int[N];
                count = new int[N];
                Arrays.fill(count, 1);

                for(int i=0;i<M;i++)
                {
                        int u1 = scan.nextInt();
                    int v1 = scan.nextInt();

                    tree[u1-1] = v1;

                    count[v1-1] += count[u1-1];

                    int root = tree[v1-1];

                    while(root!=0)
                    {
                        count[root-1] += count[u1-1];
                        root = tree[root-1];
                    }
                }

                System.out.println("");

                int counter = -1;
                for(int i=0;i<count.length;i++)
                {
                        if(count[i]%2==0)
                        {
                                counter++;
                        }

                }
                System.out.println(counter);

        }

}
Community
  • 1
  • 1
Eren Yagdiran
  • 338
  • 2
  • 12
  • 1
    this line : `tree[u1-1] = v1;` assumes something like the nodes are in order, sorry to say but this is flawed – Decebal Oct 05 '15 at 20:42
  • @decebal Exactly! However Eren's solution could pass all the cases on HackerRank - https://www.hackerrank.com/challenges/even-tree – Jianxin Gao Oct 22 '16 at 00:07
2

If you observe the input, you can see that it is quite easy to count the number of nodes under each node. Consider (a b) as the edge input, in every case, a is the child and b is the immediate parent. The input always has edges represented bottom-up.

So its essentially the number of nodes which have an even count(Excluding the root node). I submitted the below code on Hackerrank and all the tests passed. I guess all the cases in the input satisfy the rule.

def find_edges(count):
    root = max(count)

    count_even = 0

    for cnt in count:
        if cnt % 2 == 0:
            count_even += 1

    if root % 2 == 0:
        count_even -= 1

    return count_even

def count_nodes(edge_list, n, m):
    count = [1 for i in range(0, n)]

    for i in range(m-1,-1,-1):
        count[edge_list[i][1]-1] += count[edge_list[i][0]-1]

return find_edges(count)
doubleo
  • 4,389
  • 4
  • 16
  • 19
  • 1
    I beg to differ here , using the following test case and your mentioned strategy output should be 6 while output should be 4 : 20 19 2 1 3 1 4 3 5 2 6 5 7 1 8 1 9 2 10 7 11 10 12 3 13 7 14 8 15 12 16 6 17 6 18 10 19 1 20 8 Please explain strategy ! – Harshit Mar 28 '14 at 19:47
2

I know that this has already been answered here lots and lots of time. I still want to know reviews on my solution here. I tried to construct the child count as the edges were coming through the input and it passed all the test cases.

namespace Hackerrank
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    class Program
    {
        static void Main(string[] args)
        {
            var tempArray = Console.ReadLine().Split(' ').Select(x => Convert.ToInt32(x)).ToList();
            int verticeNumber = tempArray[0];
            int edgeNumber = tempArray[1];

            Dictionary<int, int> childCount = new Dictionary<int, int>();

            Dictionary<int, int> parentDict = new Dictionary<int, int>();

            for (int count = 0; count < edgeNumber; count++)
            {
                var nodes = Console.ReadLine().Split(' ').Select(x => Convert.ToInt32(x)).ToList();
                var node1 = nodes[0];
                var node2 = nodes[1];

                if (childCount.ContainsKey(node2))
                    childCount[node2]++;
                else childCount.Add(node2, 1);

                var parent = node2;
                while (parentDict.ContainsKey(parent))
                {
                    var par = parentDict[parent];
                    childCount[par]++;
                    parent = par;
                }

                parentDict[node1] = node2;
            }

            Console.WriteLine(childCount.Count(x => x.Value % 2 == 1) - 1);
        }
    }
}
Swagata Prateek
  • 1,076
  • 7
  • 15
1

My first inclination is to work up from the leaf nodes because you cannot cut their edges as that would leave single-vertex subtrees.

David Harkness
  • 35,992
  • 10
  • 112
  • 134
1

Here's the approach that I used to successfully pass all the test cases.

  1. Mark vertex 1 as the root
  2. Starting at the current root vertex, consider each child. If the sum total of the child and all of its children are even, then you can cut that edge
  3. Descend to the next vertex (child of root vertex) and let that be the new root vertex. Repeat step 2 until you have traversed all of the nodes (depth first search).
kldavis4
  • 2,177
  • 1
  • 22
  • 33
0

Here's the general outline of an alternative approach:

  1. Find all of the articulation points in the graph.
  2. Check each articulation point to see if edges can be removed there.
  3. Remove legal edges and look for more articulation points.
Community
  • 1
  • 1
seaotternerd
  • 6,298
  • 2
  • 47
  • 58
0

Solution - Traverse all the edges, and count the number of even edges

If we remove an edge from the tree and it results in two tree with even number of vertices, let's call that edge - even edge

If we remove an edge from the tree and it results in two trees with odd number of vertices, let's call that edge - odd edge

Here is my solution in Ruby

num_vertices, num_edges = gets.chomp.split(' ').map { |e| e.to_i }
graph = Graph.new
(1..num_vertices).to_a.each do |vertex|
  graph.add_node_by_val(vertex)
end

num_edges.times do |edge|
  first, second = gets.chomp.split(' ').map { |e| e.to_i }
  graph.add_edge_by_val(first, second, 0, false)
end

even_edges = 0
graph.edges.each do |edge|
  dup = graph.deep_dup
  first_tree = nil
  second_tree = nil
  subject_edge = nil
  dup.edges.each do |e|
    if e.first.value == edge.first.value && e.second.value == edge.second.value
      subject_edge = e
      first_tree = e.first
      second_tree = e.second
    end
  end
  dup.remove_edge(subject_edge)
  if first_tree.size.even? && second_tree.size.even?
    even_edges += 1
  end
end
puts even_edges

Note - Click Here to check out the code for Graph, Node and Edge classes

satnam
  • 1,457
  • 4
  • 23
  • 43