Obtain forest out of tree with even number of nodes

后端 未结 8 495
别跟我提以往
别跟我提以往 2021-02-03 20:31

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 remo

相关标签:
8条回答
  • 2021-02-03 21:12

    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)
    
    0 讨论(0)
  • 2021-02-03 21:12

    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).
    0 讨论(0)
  • 2021-02-03 21:15

    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

    0 讨论(0)
  • 2021-02-03 21:18

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-03 21:18

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

    0 讨论(0)
  • 2021-02-03 21:21

    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.

    0 讨论(0)
提交回复
热议问题