Skip to content

Instantly share code, notes, and snippets.

@robbiemu
Created April 15, 2016 17:26
Show Gist options
  • Save robbiemu/48346b79dd15601381b3137dfa6fd437 to your computer and use it in GitHub Desktop.
Save robbiemu/48346b79dd15601381b3137dfa6fd437 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Solution
{
class Solution
{
static void Main(String[] args)
{
Tree tree = new Tree();
int[] TreeSpec = Console.ReadLine().Split(' ').Select(x => Convert.ToInt32(x)).ToArray();
Nullable<int> found0, found1;
for (int i = 0; i < TreeSpec[1]; i++)
{
String[] edge = Console.ReadLine().Split(' ').ToArray();
found0 = null;
found1 = null;
foreach(Tree.Node n in tree.Nodes)
{
if(n.Tag == edge[0]) { found0 = n.Index; }
if (n.Tag == edge[1]) { found1 = n.Index; }
}
if(found0 == null)
{
tree.Nodes.Add(new Tree.Node(tree, edge[0]));
found0 = tree.Nodes.Count - 1;
}
if (found1 == null)
{
tree.Nodes.Add(new Tree.Node(tree, edge[1]));
found1 = tree.Nodes.Count - 1;
}
tree.Nodes[(int)found0].AddParent((int)found1);
tree.Nodes[(int)found1].AddChild((int)found0);
} // tree is complete
int edgesRemoved = 0;
foreach (int rootIndex in tree.RootNodesDirectory)
{
edgesRemoved = TraverseDF(tree, edgesRemoved, rootIndex);
}
Console.WriteLine(edgesRemoved);
}
private static int TraverseDF(Tree tree, int edgesRemoved, int rootIndex)
{
int childIndex;
for (int ci = tree.Nodes[rootIndex].Children.Count - 1; ci>=0; ci--)
{
childIndex = tree.Nodes[rootIndex].Children[ci];
Console.Out.WriteLine("[from "+tree.Nodes[rootIndex].Tag+"] subtree count from " + tree.Nodes[childIndex].Tag + ": " + countDF(tree, childIndex));
if (countDF(tree, childIndex) % 2 == 0)
{
Console.Out.WriteLine("removing edge: " + tree.Nodes[rootIndex].Tag + " " + tree.Nodes[childIndex].Tag);
tree.Nodes[rootIndex].RemoveChild(childIndex);
edgesRemoved++;
} else
{
edgesRemoved += TraverseDF(tree, 0, childIndex);
}
}
return edgesRemoved;
}
private static int countDF(Tree tree, int rootIndex) //assumes simple graph
{
int cnt = 1;
foreach (int childIndex in tree.Nodes[rootIndex].Children)
{
cnt += countDF(tree, childIndex);
}
return cnt;
}
}
class Tree
{
public List<Node> Nodes;
public List<int> RootNodesDirectory;
public List<int> LeafNodesDirectory;
public Tree()
{
Nodes = new List<Node> { };
RootNodesDirectory = new List<int> { };
LeafNodesDirectory = new List<int> { };
}
public class Node
{
public String Tag;
public int Index;
public List<int> Parents;
public List<int> Children;
Tree mTree;
public Node(Tree tree, String tag, int[] parents=null, int[] children=null)
{
Tag = tag;
Index = tree.Nodes.Count;
mTree = tree;
if (parents == null)
{
Parents = new List<int> { };
mTree.RootNodesDirectory.Add(Index);
}
else
{
parents.ToList();
}
if (children == null)
{
Children = new List<int> { };
mTree.LeafNodesDirectory.Add(Index);
}
else
{
children.ToList();
}
}
public bool AddParent(int index)
{
if (Parents.Contains(index))
{
return false;
}
if ((Children.Count == 0) && (!mTree.LeafNodesDirectory.Contains(Index)))
{
mTree.LeafNodesDirectory.Add(Index);
}
mTree.LeafNodesDirectory = mTree.LeafNodesDirectory.Where(val => val != index).ToList();
Parents.Add(index);
return true;
}
public bool AddChild(int index)
{
if (Children.Contains(index))
{
return false;
}
if ((Parents.Count == 0) && (!mTree.RootNodesDirectory.Contains(Index)))
{
mTree.RootNodesDirectory.Add(Index);
}
mTree.RootNodesDirectory = mTree.RootNodesDirectory.Where(val => val != index).ToList();
Children.Add(index);
return true;
}
public bool RemoveParent(int index)
{
if (!Parents.Contains(index))
{
return false;
}
Parents.Remove(index);
if (Parents.Count == 0)
{
mTree.RootNodesDirectory.Add(Index);
}
return true;
}
public bool RemoveChild(int index)
{
if (!Children.Contains(index))
{
return false;
}
Children.Remove(index);
if (Children.Count == 0)
{
mTree.LeafNodesDirectory.Add(Index);
}
return true;
}
}
}
}
@robbiemu
Copy link
Author

this was my take at the hackerrank eventree challenge. It took me more than a couple of hours to even begin writing the code. My first attempt had an events system for the nodes and tree to maintain state, but that became cumbersome so I redid it. It allowed me to complete the challenge. this is the working object model for the challenge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment