Exemplo n.º 1
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")));
    }
Exemplo n.º 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 hld  = new HLDecomposition(comp.Components.Count);

        for (int a = 0; a < comp.Components.Count; a++)
        {
            foreach (int b in comp.Components[a].Edges)
            {
                if (a < b)
                {
                    hld.AddEdge(a, b);
                }
            }
        }
        hld.Build();
        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 || hld.Dist(a, b) + hld.Dist(b, c) == hld.Dist(a, c);
        }
        Console.WriteLine(string.Join("\n", ans.Select(b => b ? "OK" : "NG")));
    }
Exemplo n.º 3
0
    void Test()
    {
        int N  = (int)1e5;
        int M  = N - 1; // (int)2e5;
        var E  = new int[M][];
        int ei = 0;

        for (int i = 0; i < M; i++)
        {
            E[i] = new int[] { i + 1, i + 2 };
        }
        int NQ     = (int)1e5;
        var Q      = new int[NQ][];
        var random = new Random(0);

        for (int i = 0; i < NQ; i++)
        {
            Q[i] = new[] { random.Next(N) + 1, random.Next(N) + 1, random.Next(N) + 1 }
        }
        ;

        var sw   = Stopwatch.StartNew();
        var comp = new TwoEdgeConnectedComponent(N, E, 1);

        Console.WriteLine("TwoEdge: " + sw.ElapsedMilliseconds);

        sw.Restart();

        var hld = new HLDecomposition(comp.Count);

        for (int a = 0; a < comp.Count; a++)
        {
            foreach (int b in comp[a].Edges)
            {
                if (a < b)
                {
                    hld.AddEdge(a, b);
                }
            }
        }
        hld.Build();

        Console.WriteLine("HLD: " + sw.ElapsedMilliseconds);
        sw.Restart();

        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 || hld.Dist(a, b) + hld.Dist(b, c) == hld.Dist(a, c);
        }
        Console.WriteLine(sw.ElapsedMilliseconds);
        Console.ReadLine();
    }
}
Exemplo n.º 4
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);
    }
}