Esempio n. 1
0
        public int RightSiblingsCount(NaryTreeNode node)
        {
            if (node.parent != null)
            {
                var c = node.parent.children;
                var i = c.IndexOf(node);
                var l = (c.Count - 1) - i;
                return(l);
            }

            return(0);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var reader = new StreamReader("input.txt");
            // int tests = int.Parse(Console.ReadLine());
            int tests = int.Parse(reader.ReadLine());

            for (int test = 0; test < tests; test++)
            {
                // int soldiers = int.Parse(Console.ReadLine());
                int soldiers = int.Parse(reader.ReadLine());

                // string[] parents = Console.ReadLine().Split(' ');
                string[] parents = reader.ReadLine().Trim().Split(' ');

                // Total Handshakes
                int hs = 0;

                // Total BumpFists
                int bf = 0;

                // Construct a tree given its parents array.
                NaryTree tree = new NaryTree();
                tree.CreateTreeFromParents(parents);

                // For each node starting from root:
                Queue <NaryTreeNode> queue = new Queue <NaryTreeNode>();
                queue.Enqueue(tree.root);

                while (queue.Count > 0)
                {
                    NaryTreeNode currentNode = queue.Dequeue();

                    // bf = bf + total siblings on the right
                    bf = bf + tree.RightSiblingsCount(currentNode);

                    // Enqueue all the children of current node.
                    foreach (var child in currentNode.children)
                    {
                        queue.Enqueue(child);
                    }
                }

                // total handshakes
                hs = tree.TotalChildren();

                Console.WriteLine(hs + " " + bf);
            }

            Console.ReadLine();
        }
Esempio n. 3
0
        public void CreateTreeFromParents(string[] parents)
        {
            int length = parents.Length;

            created = new NaryTreeNode[length + 1];
            int j = 0;

            for (int i = 1; i <= length; i++)
            {
                int parent = int.Parse(parents[j++]);

                if (created[i] == null)
                {
                    created[i] = new NaryTreeNode(i);
                }

                if (parent == 0)
                {
                    root = created[i];
                    continue;
                }

                if (created[parent] == null)
                {
                    created[parent] = new NaryTreeNode(parent);
                }

                created[i].parent = created[parent];
                created[i].parent.descendantsCount += 1 + created[i].descendantsCount;

                created[parent].children.Insert(created[parent].children.Count, created[i]);
            }

            // Update the descendants


            created = null;
        }