private static RootedTree[] GetRandomTrees(int count, int vertexCount, int minChildCount, int maxChildCount) { var randomTrees = new RootedTree[count]; for (int i = 0; i < count; ++i) { randomTrees[i] = InputGenerator.GenerateRandomRootedTree(vertexCount, minChildCount, maxChildCount); } return(randomTrees); }
private void ValidatesATree3(RootedTree tree) { Assert.AreEqual(3, tree.VertexCount); Assert.AreEqual(1, tree.Vertices[0].Children.Count); Assert.AreEqual(1, tree.Vertices[0].Children[0].ID); Assert.AreEqual(1, tree.Vertices[1].Children.Count); Assert.AreEqual(2, tree.Vertices[1].Children[0].ID); Assert.AreEqual(0, tree.Vertices[2].Children.Count); Assert.AreEqual(0, tree.Vertices[0].Depth); Assert.AreEqual(1, tree.Vertices[1].Depth); Assert.AreEqual(2, tree.Vertices[2].Depth); Assert.AreEqual(3, tree.Vertices[0].SubtreeSize); Assert.AreEqual(2, tree.Vertices[1].SubtreeSize); Assert.AreEqual(1, tree.Vertices[2].SubtreeSize); }
private void ValidatesATree1(RootedTree tree) { Assert.AreEqual(13, tree.VertexCount); CollectionAssert.AreEquivalent( new[] { 1, 2, 3 }, tree.Vertices[0].Children.Select(c => c.ID).ToArray()); Assert.AreEqual(0, tree.Vertices[1].Children.Count); CollectionAssert.AreEquivalent( new[] { 4, 5, 6 }, tree.Vertices[2].Children.Select(c => c.ID).ToArray()); Assert.AreEqual(0, tree.Vertices[3].Children.Count); Assert.AreEqual(0, tree.Vertices[4].Children.Count); CollectionAssert.AreEquivalent( new[] { 7, 8 }, tree.Vertices[5].Children.Select(c => c.ID).ToArray()); CollectionAssert.AreEquivalent( new[] { 9, 10 }, tree.Vertices[6].Children.Select(c => c.ID).ToArray()); Assert.AreEqual(0, tree.Vertices[7].Children.Count); Assert.AreEqual(0, tree.Vertices[8].Children.Count); CollectionAssert.AreEquivalent( new[] { 11, 12 }, tree.Vertices[9].Children.Select(c => c.ID).ToArray()); Assert.AreEqual(0, tree.Vertices[10].Children.Count); Assert.AreEqual(0, tree.Vertices[11].Children.Count); Assert.AreEqual(0, tree.Vertices[12].Children.Count); Assert.AreEqual(0, tree.Vertices[0].Depth); Assert.AreEqual(1, tree.Vertices[1].Depth); Assert.AreEqual(1, tree.Vertices[2].Depth); Assert.AreEqual(1, tree.Vertices[3].Depth); Assert.AreEqual(2, tree.Vertices[4].Depth); Assert.AreEqual(2, tree.Vertices[5].Depth); Assert.AreEqual(2, tree.Vertices[6].Depth); Assert.AreEqual(3, tree.Vertices[7].Depth); Assert.AreEqual(3, tree.Vertices[8].Depth); Assert.AreEqual(3, tree.Vertices[9].Depth); Assert.AreEqual(3, tree.Vertices[10].Depth); Assert.AreEqual(4, tree.Vertices[11].Depth); Assert.AreEqual(4, tree.Vertices[12].Depth); Assert.AreEqual(13, tree.Vertices[0].SubtreeSize); Assert.AreEqual(1, tree.Vertices[1].SubtreeSize); Assert.AreEqual(10, tree.Vertices[2].SubtreeSize); Assert.AreEqual(1, tree.Vertices[3].SubtreeSize); Assert.AreEqual(1, tree.Vertices[4].SubtreeSize); Assert.AreEqual(3, tree.Vertices[5].SubtreeSize); Assert.AreEqual(5, tree.Vertices[6].SubtreeSize); Assert.AreEqual(1, tree.Vertices[7].SubtreeSize); Assert.AreEqual(1, tree.Vertices[8].SubtreeSize); Assert.AreEqual(3, tree.Vertices[9].SubtreeSize); Assert.AreEqual(1, tree.Vertices[10].SubtreeSize); Assert.AreEqual(1, tree.Vertices[11].SubtreeSize); Assert.AreEqual(1, tree.Vertices[12].SubtreeSize); }
public RootedTreeNode(RootedTree tree, Node node, RootedTreeNode parent) { Tree = tree; Node = node; Parent = parent; Level = parent == null ? 0 : (parent.Level + 1); var children = new List <RootedTreeNode>(node.Degree); foreach (Node child in Node.GetAdjacentNodes().Where(x => Parent == null || x != Parent.Node)) { children.Add(new RootedTreeNode(tree, child, this)); } Children = children.ToArray(); tree.SetNode(this); }
public void ValidatesATree1() { // This is the tree pictured here (but zero-based): https://www.spoj.com/problems/LCA/. var tree = RootedTree.CreateFromChildren(13, 0, new[] { new List <int> { 1, 2, 3 }, null, new List <int> { 4, 5, 6 }, null, null, new List <int> { 7, 8 }, new List <int> { 9, 10 }, null, null, new List <int> { 11, 12 }, null, null, null }); tree.InitializeDepthsAndSubtreeSizes(); ValidatesATree1(tree); tree = RootedTree.CreateFromEdges(13, 0, new[, ] { { 0, 1 }, { 0, 2 }, { 0, 3 }, { 2, 4 }, { 2, 5 }, { 2, 6 }, { 9, 6 }, { 10, 6 }, { 5, 7 }, { 5, 8 }, { 9, 11 }, { 9, 12 } }); ValidatesATree1(tree); }
static void Main() { int n = int.Parse(Console.ReadLine()); RootedTree rtree = new RootedTree(); for (int i = 0; i < n; i++) { List <int> inputs = Console.ReadLine().Split(' ') .Select(val => int.Parse(val)) .ToList(); int p = inputs[0]; inputs.RemoveRange(0, 2); rtree.Add(p, inputs); } for (int i = 0; i < n; i++) { rtree[i].Display(); } }
// E.g. if verticesChildren[1] = (3, 4, 6), vertices w/ ID 3, 4, 6 are children of vertex w/ ID 1. public static RootedTree CreateFromChildren(int vertexCount, int rootID, List <int>[] verticesChildren) { var tree = new RootedTree(vertexCount, rootID); for (int id = 0; id < vertexCount; ++id) { if (!verticesChildren[id]?.Any() ?? true) { continue; } var parent = tree.Vertices[id]; foreach (int childID in verticesChildren[id]) { tree.Vertices[childID].SetParent(parent); } } return(tree); }
private void GetEulerTour3(bool useStack) { var tree = RootedTree.CreateFromChildren(3, 0, new[] { new List <int> { 1 }, new List <int> { 2 }, null }); var eulerTour = useStack ? tree.GetEulerTourUsingStack() : tree.GetEulerTourUsingRecursion(); CollectionAssert.AreEqual(new[] { 0, 1, 2, 1, 0 }, eulerTour.Select(v => v.ID).ToArray()); Assert.AreEqual(0, tree.Vertices[0].Depth); Assert.AreEqual(1, tree.Vertices[1].Depth); Assert.AreEqual(2, tree.Vertices[2].Depth); }
public void ValidatesATree3() { var tree = RootedTree.CreateFromChildren(3, 0, new[] { new List <int> { 1 }, new List <int> { 2 }, null }); tree.InitializeDepthsAndSubtreeSizes(); ValidatesATree3(tree); tree = RootedTree.CreateFromEdges(3, 0, new[, ] { { 2, 1 }, { 1, 0 } }); ValidatesATree3(tree); }
public static RootedTree GenerateRandomRootedTree(int vertexCount, int minChildCount, int maxChildCount) { if (minChildCount < 1 || maxChildCount < 1 || minChildCount > maxChildCount) { throw new ArgumentException(); } var verticesChildren = new List <int> [vertexCount]; // The number of children a vertex has is random, but IDs aren't random. The root is always 0 // and if ID1 < ID2, ID1's depth <= ID2's depth. Once an ID is added as a child, it gets in // line to become a parent. The tree isn't going to be very deep for any maxChildCount except // 1, as parents have (minChildCount + maxChildCount) / 2 children on average. var availableParentIDs = new Queue <int>(); availableParentIDs.Enqueue(0); var availableChildIDs = new Queue <int>(Enumerable.Range(1, vertexCount - 1)); while (true) { int parentID = availableParentIDs.Dequeue(); var parentsChildren = verticesChildren[parentID] = new List <int>(); int childCount = Rand.Next(minChildCount, maxChildCount + 1); for (int i = 0; i < childCount; ++i) { if (availableChildIDs.Count == 0) { return(RootedTree.CreateFromChildren(vertexCount, 0, verticesChildren)); } int childID = availableChildIDs.Dequeue(); parentsChildren.Add(childID); availableParentIDs.Enqueue(childID); } } }
public LCA(int vertexCount, List <int>[] verticesChildren) { _tree = RootedTree.CreateFromChildren(vertexCount, 0, verticesChildren); _eulerTour = _tree.GetEulerTour(); _segmentTree = new ArrayBasedSegmentTree(_eulerTour); }
internal Vertex(RootedTree tree, int ID) { _tree = tree; this.ID = ID; }