static void Main() { var n = int.Parse(Console.ReadLine()); var map = Array.ConvertAll(new int[n], _ => Read().Skip(1).ToList()); var qc = int.Parse(Console.ReadLine()); var qs = Array.ConvertAll(new int[qc], _ => Read()); var lca = new Lca(n, 0, map); Console.WriteLine(string.Join("\n", qs.Select(q => lca.GetLca(q[0], q[1])))); }
static void Main() { var h = Read(); int n = h[0], qc = h[1]; var p = Read(); var qs = Array.ConvertAll(new bool[qc], _ => Read()); var d = p.Select((v, i) => (p: v, c: i + 1)).ToLookup(x => x.p, x => x.c); var map = Enumerable.Range(0, n).Select(i => d[i].ToList()).ToArray(); var lca = new Lca(n, 0, map); Console.WriteLine(string.Join("\n", qs.Select(q => lca.GetLca(q[0], q[1])))); }