Example #1
0
    void solve()
    {
        int N = cin.nextint;
        var D = new HLTree(N);

        for (int i = 0; i < N - 1; i++)
        {
            D.AddEdge(cin.nextint - 1, cin.nextint - 1);
        }
        D.Build();

        int Q = cin.nextint;

        for (int i = 0; i < Q; i++)
        {
            int u = cin.nextint - 1, v = cin.nextint - 1;
            WriteLine(D[u] + D[v] - D[D.GetLCA(u, v)] * 2 + 1);
        }
    }
Example #2
0
        public void Solve()
        {
            var n = ri;
            var G = new HLTree(n);

            for (int i = 0; i < n - 1; i++)
            {
                G.AddEdge(ri - 1, ri - 1);
            }
            G.Build();
            var q = ri;

            for (int i = 0; i < q; i++)
            {
                var u   = ri - 1;
                var v   = ri - 1;
                var lca = G.GetLCA(u, v);
                Debug.WriteLine(lca);
                Console.WriteLine(G.d[u] + G.d[v] - 2 * G.d[lca] + 1);
            }
        }