示例#1
0
 static int DFS_Find_Biggest_Sum_For_Children(Node currentNode)
 {
     int sum = 0;
     foreach (Node child in currentNode.Children)
     {
         int childSum = DFS_Find_Biggest_Sum_For_Children(child);
         if (childSum > sum)
         {
             sum = childSum;
         }
     }
     sum += currentNode.Value;
     return sum;
 }
示例#2
0
        static Dictionary<int, Node> ReadGraph(int edges)
        {
            Dictionary<int, Node> dict = new Dictionary<int, Node>();

            for (int i = 0; i < edges; i++)
            {
                int[] splitted = Console.ReadLine().Split().Select(int.Parse).ToArray();
                if (!splitted.Any())
                {
                    i--;
                }
                foreach (var j in splitted)
                {
                    if (!dict.ContainsKey(j))
                    {
                        dict[j] = new Node(j);
                    }
                }
                dict[splitted[0]].Children.Add(dict[splitted[1]]);
                dict[splitted[1]].Children.Add(dict[splitted[0]]);
            }
            return dict;
        }
示例#3
0
        static long DFS_Biggest_Sum_of_Children(Node currentNode)
        {
            List<long> sums = new List<long>();
            sums.Add(0);
            sums.Add(0);
            foreach (Node child in currentNode.Children)
            {
                bool hasSorted = false;
                int sumOfChild = DFS_Find_Biggest_Sum_For_Children(child);
                if (sums.Count < 2)
                {
                    sums.Add(sumOfChild);
                }
                else
                {
                    if (!hasSorted)
                    {
                        sums.Sort();
                        hasSorted = true;
                    }
                    if (sums[1] < sumOfChild)
                    {
                        if (sums[0] < sumOfChild)
                        {
                            sums[0] = sumOfChild;
                        }
                        else
                        {
                            sums[1] = sumOfChild;
                        }
                    }
                }

            }
            return sums[0] + sums[1] + currentNode.Value;
        }
示例#4
0
 static int DFS_Find_Deepest_Hole(Node node, int currentLenght)
 {
     int longestPath = currentLenght;
     hasVisited[node.Value] = true;
     if (node.Children.Any())
     {
         foreach (var child in node.Children)
         {
             if (!hasVisited.ContainsKey(child.Value))
             {
                 hasVisited[child.Value] = false;
             }
             if (!hasVisited[child.Value])
             {
                 int newLenght = DFS_Find_Deepest_Hole(child, currentLenght+1);
                 if (longestPath < newLenght)
                 {
                     longestPath = newLenght;
                 }
             }
         }
     }
     return longestPath;
 }
示例#5
0
        static Dictionary<int, Node> ReadTree(int nodes, int edges)
        {
            Dictionary<int, Node> newTree = new Dictionary<int, Node>();

            for (int i = 0; i < edges; i++)
            {
                int[] splitted = Console.ReadLine().Split().Select(int.Parse).ToArray();

                int nodeNumber = splitted[0];

                if (!newTree.ContainsKey(nodeNumber))
                {
                    newTree[nodeNumber] = new Node(nodeNumber);
                }
                foreach (int j in splitted.Skip(1))
                {
                    if (!newTree.ContainsKey(j))
                    {
                        newTree[j] = new Node(j);
                    }
                    newTree[nodeNumber].Children.Add(newTree[j]);
                    newTree[j].Parent = newTree[nodeNumber];
                }
            }
            return newTree;
        }