private static LowestCommonAncestor InitializeLca(int root = 0)
        {
            var lca = new LowestCommonAncestor(6, root);

            lca.AddEdge(0, 1);
            lca.AddEdge(0, 2);
            lca.AddEdge(1, 3);
            lca.AddEdge(1, 4);
            lca.AddEdge(2, 5);

            return(lca);
        }
Example #2
0
    public void Solve()
    {
        int N = Reader.Int(), M = Reader.Int();
        var E    = Reader.IntTable(M);
        int NQ   = Reader.Int();
        var Q    = Reader.IntTable(NQ);
        var comp = new TwoEdgeConnectedComponent(N, E, 1);
        var lca  = new LowestCommonAncestor(comp.Components.Count);

        for (int a = 0; a < comp.Components.Count; a++)
        {
            foreach (int b in comp.Components[a].Edges)
            {
                if (a < b)
                {
                    lca.AddEdge(a, b);
                }
            }
        }
        lca.Init(0);
        var ans = new bool[NQ];

        for (int i = 0; i < NQ; i++)
        {
            int a = comp.VtoComponentId(Q[i][0] - 1);
            int b = comp.VtoComponentId(Q[i][1] - 1);
            int c = comp.VtoComponentId(Q[i][2] - 1);
            ans[i] = a == b && a == c || lca.Dist(a, b) + lca.Dist(b, c) == lca.Dist(a, c);
        }
        Console.WriteLine(string.Join("\n", ans.Select(b => b ? "OK" : "NG")));
    }
Example #3
0
        public static void Solve()
        {
            var(N, X) = Scanner.Scan <int, int>();
            var tree = new LowestCommonAncestor(N);

            for (var i = 0; i < N - 1; i++)
            {
                var(a, b, c) = Scanner.Scan <int, int, long>();
                a--; b--;
                tree.AddEdge(a, b, c);
            }

            for (var i = 0; i < N; i++)
            {
                for (var j = 0; j < N; j++)
                {
                    if (tree.GetCost(i, j) == X)
                    {
                        Console.WriteLine("Yes");
                        return;
                    }
                }
            }

            Console.WriteLine("No");
        }
        public void GetDistanceAndCostTest(int u, int v, int c)
        {
            const int n = 6;

            for (var i = 0; i < n; i++)
            {
                var sut = new LowestCommonAncestor(6, i);
                sut.AddEdge(0, 1);
                sut.AddEdge(0, 2);
                sut.AddEdge(1, 3);
                sut.AddEdge(1, 4);
                sut.AddEdge(2, 5);

                Assert.That(sut.GetDistance(u, v), Is.EqualTo(c));
                Assert.That(sut.GetDistance(v, u), Is.EqualTo(c));
                Assert.That(sut.GetCost(u, v), Is.EqualTo(c));
                Assert.That(sut.GetCost(v, u), Is.EqualTo(c));
            }
        }
        public void GetCostTest(int u, int v, int c)
        {
            const int n = 6;
            const int m = 7;

            for (var i = 0; i < n; i++)
            {
                var sut = new LowestCommonAncestor(n, i);
                sut.AddEdge(0, 1, 1);
                sut.AddEdge(0, 2, 3);
                sut.AddEdge(2, 3, 5);
                sut.AddEdge(2, 4, 2);
                sut.AddEdge(3, 5, 7);

                Assert.That(sut.GetCost(u, v), Is.EqualTo(c));
                Assert.That(sut.GetCost(u, v, m), Is.EqualTo(c % m));
                Assert.That(sut.GetCost(v, u), Is.EqualTo(c));
            }
        }
        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));
        }
Example #7
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");
            }
        }
Example #8
0
    public void Solve()
    {
        int N = NextInt(), M = NextInt();
        var E = new int[M][];

        for (int i = 0; i < M; i++)
        {
            E[i] = new[] { NextInt(), NextInt() }
        }
        ;
        int NQ = NextInt();
        var Q  = new int[NQ][];

        for (int i = 0; i < NQ; i++)
        {
            Q[i] = new[] { NextInt(), NextInt(), NextInt() }
        }
        ;
        var comp = new TwoEdgeConnectedComponent(N, E, 1);
        var lca  = new LowestCommonAncestor(comp.Components.Count);

        for (int a = 0; a < comp.Components.Count; a++)
        {
            foreach (int b in comp.Components[a].Edges)
            {
                if (a < b)
                {
                    lca.AddEdge(a, b);
                }
            }
        }
        lca.Init(0);
        var ans = new bool[NQ];

        for (int i = 0; i < NQ; i++)
        {
            int a = comp.VtoComponentId(Q[i][0] - 1);
            int b = comp.VtoComponentId(Q[i][1] - 1);
            int c = comp.VtoComponentId(Q[i][2] - 1);
            ans[i] = a == b && a == c || lca.Dist(a, b) + lca.Dist(b, c) == lca.Dist(a, c);
        }
        Console.WriteLine(string.Join("\n", ans.Select(b => b ? "OK" : "NG")));
    }

    TextReader _reader = Console.In;
    int NextInt()
    {
        int c;

        while ((c = _reader.Read()) < '0' || c > '9')
        {
        }
        int res = c - '0';

        while ((c = _reader.Read()) >= '0' && c <= '9')
        {
            res = res * 10 + c - '0';
        }
        return(res);
    }
}