using System.Collections.Generic;
public Node(int val, Node mother, Node father)
Children = new List<Node>();
public void HaveABaby(Node n, Node father)
public bool IsRelated(Node n)
return IsRelated(n, new Dictionary<Node, int>());
public bool IsRelated(Node n, Dictionary<Node, int> map)
if(n == null || map.ContainsKey(this)) return false;
if(n.Value == Value) return true;
if(Children.Contains(n)) return true;
foreach(Node current in Children)
if(current.IsRelated(n, map)) return true;
return (Mother != null && Mother.IsRelated(n, map)) || (Father != null && Father.IsRelated(n, map));
public static void Main()
List<Node> nodes = new List<Node>();
for(int i = 0; i < 10; i++) nodes.Add(new Node(i, null, null));
nodes[0].HaveABaby(nodes[1], null);
nodes[0].HaveABaby(nodes[2], null);
nodes[1].HaveABaby(nodes[3], null);
nodes[1].HaveABaby(nodes[4], null);
nodes[2].HaveABaby(nodes[5], null);
nodes[6].HaveABaby(nodes[7], null);
nodes[6].HaveABaby(nodes[8], null);
for(int j = 0; j <=5; j++)
for(int k = 0; k <= 5; k++)
Console.WriteLine(nodes[j].IsRelated(nodes[k]) ? "Pass" : "Fail");
for(int j = 0; j <=5; j++)
for(int k = 6; k <= 8; k++)
Console.WriteLine(nodes[j].IsRelated(nodes[k]) ? "Fail" : "Pass");
for(int j = 6; j <=8; j++)
for(int k = 6; k <= 8; k++)
Console.WriteLine(nodes[j].IsRelated(nodes[k]) ? "Pass" : "Fail");