public void ArgumentOutOfRangeInMethodTest(int u, int v) { const int length = 6; var sut = new LowestCommonAncestor(length); Assert.Throws <ArgumentOutOfRangeException>(() => sut.AddEdge(u, v)); Assert.Throws <ArgumentOutOfRangeException>(() => _ = sut.Find(u, v)); Assert.Throws <ArgumentOutOfRangeException>(() => _ = sut.GetDistance(u, v)); Assert.Throws <ArgumentOutOfRangeException>(() => _ = sut.GetCost(u, v)); Assert.Throws <ArgumentOutOfRangeException>(() => _ = sut.GetCost(u, v, 7)); if (v < 0 || length <= v) { return; } Assert.Throws <ArgumentOutOfRangeException>(() => _ = sut.GetAncestor(u, 0)); }
public static void Solve() { var N = Scanner.Scan <int>(); var lca = new LowestCommonAncestor(N + 1); for (var i = 1; i <= N; i++) { var p = Scanner.Scan <int>(); if (p == -1) { p = 0; } lca.AddEdge(p, i); } var Q = Scanner.Scan <int>(); while (Q-- > 0) { var(a, b) = Scanner.Scan <int, int>(); var p = lca.Find(a, b); Console.WriteLine(p == b ? "Yes" : "No"); } }