private static void TryPath(Node<int> startNode, ulong currentPathValue, ref ulong currnetMaxPath)
        {
            while (true)
            {
                if (UsedNodes.Contains(startNode))
                {
                    return;
                }

                UsedNodes.Add(startNode);
                currentPathValue = (ulong)startNode.Value + currentPathValue;

                if (startNode.HasParent && !passedRoot)
                {
                    TryPath(startNode.Parent, currentPathValue, ref currnetMaxPath);
                    continue;
                }

                passedRoot = true;

                foreach (var child in startNode.Offspring)
                {
                    TryPath(child, currentPathValue, ref currnetMaxPath);
                }

                if (currnetMaxPath < currentPathValue)
                {
                    currnetMaxPath = currentPathValue;
                }
            }
        }
Пример #2
0
        static void Main()
        {
            int nodesNumber = int.Parse(Console.ReadLine());
            Node[] nodes = new Node[nodesNumber];

            for (int n = 0; n < nodesNumber; n++)
			{
			    nodes[n] = new Node(n);
			}

            ConnectNodesInTree(nodes);

            //a) - Find the root node
            Node root = FindTreeRoot(nodes);
            Console.WriteLine(root.Value);

            //b) - Find all leaf nodes
            List<int> leafNodes = FindLeafNodes(nodes);
            Console.WriteLine(string.Join(" ", leafNodes));

            //c) - Find all middle nodes
            List<int> middleNodes = FindMiddleNodes(nodes);
            Console.WriteLine(string.Join(" ", middleNodes));

            //d) - Find the longest path in the tree
            int maxHeight = FindMaxPath(root);

            //e) - all paths in the tree with given sum S of their nodes
            List<List<Node>> listOfPath = new List<List<Node>>();
            List<Node> currentPath = new List<Node>();
            FindPathWithSum(root, listOfPath, currentPath, 0, 8);
        }
Пример #3
0
        private static void ConnectNodesInTree(Node[] nodes)
        {
            for (int i = 0; i < nodes.Length - 1; i++)
            {
                string line = Console.ReadLine();

                string[] tokens = line.Split(' ');
                int currentParent = int.Parse(tokens[0]);
                int currentChild = int.Parse(tokens[1]);

                nodes[currentParent].AddChild(nodes[currentChild]);
                nodes[currentChild].HasParent = true;
            }
        }
Пример #4
0
        private static int FindMaxPath(Node node)
        {
            if (node.Children.Count == 0)
            {
                return 0;
            }

            int maxHeight = 0;
            foreach (var child in node.Children)
            {
                maxHeight = Math.Max(maxHeight, FindMaxPath(child));
            }

            maxHeight++;
            return maxHeight;
        }
Пример #5
0
        private static void DFS(Node node, long sum)
        {
            if (!visitedIds.Contains(node.Id))
            {
                visitedIds.Add(node.Id);
                sum += node.Id;

                foreach (var children in node.Childrens)
                {
                    DFS(children, sum);
                }

                if (node.Childrens.Count == 1 && sum > maxSum)
                {
                    maxSum = sum;
                }
            }
        }
Пример #6
0
        private static void FindPathWithSum(Node root, List<List<Node>> listOfPaths, List<Node> pathSoFar, int currentSum, int targetSum)
        {
            if (currentSum == targetSum)
            {
                Node[] nodePath = new Node[pathSoFar.Count];
                pathSoFar.CopyTo(nodePath);
                listOfPaths.Add(new List<Node>(nodePath));
                pathSoFar.RemoveRange(1, pathSoFar.Count - 1);
            }

            foreach (var item in root.Children)
            {
                pathSoFar.Add(item);
                FindPathWithSum(item, listOfPaths, pathSoFar, currentSum + item.Value, targetSum);
                if (pathSoFar.Count > 1)
                {
                    pathSoFar.RemoveAt(pathSoFar.Count - 1);
                }
            }
        }
Пример #7
0
        static void Main()
        {
            #if DEBUG
            Console.SetIn(new System.IO.StreamReader("../../input.txt"));
            #endif

            Dictionary<int, Node> nodes = new Dictionary<int, Node>();
            int n = int.Parse(Console.ReadLine());
            for (int i = 0; i < n - 1; i++)
            {
                string[] input = Console.ReadLine().Split(new Char[] { '(', ')', '<', '-' }, StringSplitOptions.RemoveEmptyEntries);

                int parentId = int.Parse(input[0]);
                int childrenId = int.Parse(input[1]);

                Node parentNode = new Node(parentId);
                nodes.AddSafe(parentId, parentNode);
                parentNode = nodes[parentId];

                Node childrenNode = new Node(childrenId);
                nodes.AddSafe(childrenId, childrenNode);
                childrenNode = nodes[childrenId];

                parentNode.Childrens.Add(childrenNode);
                childrenNode.Childrens.Add(parentNode);
            }

            foreach (var value in nodes.Values)
            {
                if (value.Childrens.Count == 1)
                {
                    visitedIds.Clear();
                    DFS(value, 0);
                }
            }

            Console.WriteLine(maxSum);
        }
Пример #8
0
 public void AddChild(Node child)
 {
     children.Add(child);
 }
Пример #9
0
        private static Node FindTreeRoot(Node[] nodes)
        {
            for (int i = 0; i < nodes.Length; i++)
            {
                if (nodes[i].HasParent == false)
                {
                    return nodes[i];
                }
            }

            throw new ArgumentException("The tree has no root!");
        }
Пример #10
0
        private static List<int> FindMiddleNodes(Node[] nodes)
        {
            List<int> middleNodes = new List<int>();

            for (int i = 0; i < nodes.Length; i++)
            {
                if (nodes[i].HasParent && nodes[i].Children.Count > 0)
                {
                    middleNodes.Add(nodes[i].Value);
                }
            }

            return middleNodes;
        }
Пример #11
0
        private static List<int> FindLeafNodes(Node[] nodes)
        {
            List<int> leafNodes = new List<int>();

            for (int i = 0; i < nodes.Length; i++)
            {
                if (nodes[i].Children.Count == 0)
                {
                    leafNodes.Add(nodes[i].Value);
                }
            }

            return leafNodes;
        }