結果

問題 No.416 旅行会社
ユーザー eitahoeitaho
提出日時 2016-12-31 01:09:20
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 859 ms / 4,000 ms
コード長 6,304 bytes
コンパイル時間 1,115 ms
コンパイル使用メモリ 111,616 KB
実行使用メモリ 63,648 KB
最終ジャッジ日時 2024-05-08 15:27:53
合計ジャッジ時間 10,276 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 347 ms
51,076 KB
testcase_01 AC 33 ms
19,456 KB
testcase_02 AC 31 ms
19,328 KB
testcase_03 AC 33 ms
19,584 KB
testcase_04 AC 31 ms
19,584 KB
testcase_05 AC 34 ms
19,456 KB
testcase_06 AC 31 ms
19,328 KB
testcase_07 AC 35 ms
19,968 KB
testcase_08 AC 40 ms
21,020 KB
testcase_09 AC 73 ms
26,240 KB
testcase_10 AC 392 ms
51,084 KB
testcase_11 AC 370 ms
51,088 KB
testcase_12 AC 365 ms
51,212 KB
testcase_13 AC 340 ms
51,088 KB
testcase_14 AC 841 ms
63,136 KB
testcase_15 AC 852 ms
63,384 KB
testcase_16 AC 859 ms
63,004 KB
testcase_17 AC 822 ms
63,140 KB
testcase_18 AC 853 ms
63,648 KB
testcase_19 AC 498 ms
51,168 KB
testcase_20 AC 523 ms
50,784 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        int N = Reader.Int(), M = Reader.Int(), R = Reader.Int();
        var E = Reader.IntTable(M);
        var removedE = Reader.IntTable(R);

        var dic = new Dictionary<long, int>();
        for (int i = 0; i < M; i++)
            dic[(long)--E[i][0] * N + --E[i][1]] = i;

        var index = removedE.Select(r => dic[(long)--r[0] * N + --r[1]]).ToArray();
        var isRemoved = new bool[M];
        Array.ForEach(index, i => isRemoved[i] = true);
        UnionFind initial = new UnionFind(N), uf = null;
        for (int i = 0; i < M; i++) if (!isRemoved[i]) initial.Unite(E[i][0], E[i][1]);
        E = index.Select(i => E[i]).Reverse().ToArray();

        Action init = () => uf = initial.Clone();
        Action<int> act = i => uf.Unite(E[i][0], E[i][1]);
        Predicate<int> ok = i => !uf.Same(i, 0);
        var ans = BinarySearchMax(N, 0, R, init, act, ok);
        
        Console.WriteLine(string.Join("\n",
            ans.Skip(1).Select(x => x == int.MinValue ? -1 : x == int.MaxValue ? 0 : R - x + 1)));
    }

    /* [lb, ub] */
    int[] BinarySearchMax(int NQ, int lb, int ub, Action Init, Action<int> Act, Predicate<int> OK)
    {
        var res = new int[NQ];
        int[] q = new int[(ub - lb + 1) * 4], nq = new int[(ub - lb + 1) * 4];
        int[] A = new int[NQ * 2], B = new int[NQ * 2], tmp;
        for (int i = 0; i < NQ; i++) A[i] = i;
        q[0] = lb; q[1] = ub; q[2] = 0; q[3] = NQ;

        for (int qend = 4; qend > 0; )
        {
            Init();
            int nqend = 0;
            for (int qi = 0, bi = 0; qi != qend; qi += 4)
            {
                int L = q[qi], R = q[qi + 1], start = q[qi + 2], num = q[qi + 3];
                int mid = L + R + 1 >> 1, end = start + num;
                if (R - L <= 1)
                {
                    for (int i = start; i < end; i++) res[A[i]] = (L == lb && !OK(A[i])) ? int.MinValue : R;
                    if (L < R) Act(L);
                    if (R == ub) for (int i = start; i < end; i++) if (OK(A[i])) res[A[i]] = int.MaxValue;
                    continue;
                }
                for (int i = L; i < mid; i++) Act(i);
                int numL = 0, numR = 0;
                for (int i = start; i < end; i++)
                    if (!OK(A[i])) B[bi + numL++] = A[i];
                    else B[bi + num + numR++] = A[i];
                Array.Copy(B, bi + num, B, bi + numL, numR);
                if (R != ub) for (int i = mid; i < R; i++) Act(i);
                nq[nqend + 0] = L; nq[nqend + 1] = mid; nq[nqend + 2] = bi; nq[nqend + 3] = numL;
                nq[nqend + 4] = mid; nq[nqend + 5] = R; nq[nqend + 6] = bi + numL; nq[nqend + 7] = num - numL;
                nqend += 8;
                bi += num;
            }
            qend = nqend;
            tmp = q; q = nq; nq = tmp;
            tmp = A; A = B; B = tmp;
        }

        return res;
    }

}

class UnionFind
{
    private int[] parent;
    private byte[] rank;

    public UnionFind(int N)
    {
        parent = new int[N];
        rank = new byte[N];
        for (int i = 0; i < N; i++) parent[i] = -1;
    }
    public int Root(int x)
    {
        if (parent[x] < 0) return x;
        return parent[x] = Root(parent[x]);
    }
    public bool Same(int x, int y)
    {
        return Root(x) == Root(y);
    }
    public int Count(int x)
    {
        return -parent[Root(x)];
    }
    public bool Unite(int x, int y)
    {
        x = Root(x); y = Root(y);
        if (x == y) return false;
        if (rank[x] > rank[y]) { var t = x; x = y; y = t; }
        if (rank[x] == rank[y]) rank[y]++;
        parent[y] += parent[x];
        parent[x] = y;
        return true;
    }
    public Dictionary<int, List<int>> Components()
    {
        var dic = new Dictionary<int, List<int>>();
        for (int i = 0; i < parent.Length; i++)
        {
            int root = Root(i);
            if (!dic.ContainsKey(root)) dic[root] = new List<int>();
            dic[root].Add(i);
        }
        return dic;
    }
    public UnionFind Clone()
    {
        var clone = new UnionFind(parent.Length);
        Array.Copy(parent, clone.parent, parent.Length);
        Array.Copy(rank, clone.rank, rank.Length);
        return clone;
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0