結果

問題 No.348 カゴメカゴメ
ユーザー eitahoeitaho
提出日時 2016-02-29 02:31:26
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 704 ms / 2,000 ms
コード長 6,355 bytes
コンパイル時間 1,155 ms
コンパイル使用メモリ 114,664 KB
実行使用メモリ 132,272 KB
最終ジャッジ日時 2024-09-24 12:41:35
合計ジャッジ時間 14,266 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 601 ms
102,376 KB
testcase_01 AC 36 ms
26,608 KB
testcase_02 AC 704 ms
132,272 KB
testcase_03 AC 585 ms
106,616 KB
testcase_04 AC 37 ms
26,656 KB
testcase_05 AC 37 ms
28,880 KB
testcase_06 AC 36 ms
26,440 KB
testcase_07 AC 35 ms
24,624 KB
testcase_08 AC 35 ms
26,416 KB
testcase_09 AC 37 ms
26,412 KB
testcase_10 AC 37 ms
28,364 KB
testcase_11 AC 36 ms
26,672 KB
testcase_12 AC 46 ms
27,296 KB
testcase_13 AC 40 ms
26,668 KB
testcase_14 AC 37 ms
28,792 KB
testcase_15 AC 598 ms
101,908 KB
testcase_16 AC 36 ms
26,656 KB
testcase_17 AC 37 ms
26,404 KB
testcase_18 AC 37 ms
28,480 KB
testcase_19 AC 35 ms
26,444 KB
testcase_20 AC 36 ms
28,508 KB
testcase_21 AC 37 ms
26,924 KB
testcase_22 AC 36 ms
26,660 KB
testcase_23 AC 655 ms
121,308 KB
testcase_24 AC 665 ms
122,216 KB
testcase_25 AC 665 ms
121,316 KB
testcase_26 AC 654 ms
121,452 KB
testcase_27 AC 664 ms
120,148 KB
testcase_28 AC 671 ms
123,124 KB
testcase_29 AC 666 ms
118,652 KB
testcase_30 AC 673 ms
119,724 KB
testcase_31 AC 673 ms
119,732 KB
testcase_32 AC 671 ms
121,568 KB
testcase_33 AC 82 ms
39,424 KB
testcase_34 AC 84 ms
39,184 KB
testcase_35 AC 81 ms
37,528 KB
testcase_36 AC 82 ms
37,148 KB
testcase_37 AC 83 ms
37,640 KB
testcase_38 AC 84 ms
39,380 KB
testcase_39 AC 85 ms
37,440 KB
testcase_40 AC 86 ms
39,564 KB
testcase_41 AC 82 ms
37,664 KB
testcase_42 AC 79 ms
37,656 KB
testcase_43 AC 41 ms
26,672 KB
testcase_44 AC 41 ms
26,548 KB
testcase_45 AC 42 ms
26,416 KB
testcase_46 AC 41 ms
28,864 KB
testcase_47 AC 42 ms
26,544 KB
testcase_48 AC 41 ms
30,760 KB
testcase_49 AC 42 ms
26,804 KB
testcase_50 AC 40 ms
26,796 KB
testcase_51 AC 41 ms
26,804 KB
testcase_52 AC 42 ms
26,668 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.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

class Program
{
    static readonly int[] DY = { 0, 1, 0, -1 };
    static readonly int[] DX = { 1, 0, -1, 0 };
    int H, W;
    string[] grid;
    bool[] seen;
    UnionFind uf;
    Dictionary<int, List<int>> components;
    List<int>[] G;

    public void Solve()
    {
        H = Reader.Int() + 2;
        W = Reader.Int() + 2;
        grid = Surround(Reader.StringArray(H - 2), '.');
        seen = new bool[H * W];
        uf = new UnionFind(H * W);
        memo = new int?[H * W, 2];
        G = new List<int>[H * W];
        for (int i = 0; i < G.Length; i++) G[i] = new List<int>();

        for (int y = 0; y < H; y++)
            for (int x = 0; x < W; x++)
                for (int dy = -1; dy <= 1; dy++)
                    for (int dx = -1; dx <= 1; dx++)
                    {
                        int ny = y + dy, nx = x + dx;
                        if (IsInside(ny, nx) && grid[y][x] == grid[ny][nx] &&
                            (dx == 0 || dy == 0 || grid[y][x] == 'x'))
                            uf.Unite(y * W + x, ny * W + nx);
                    }
        components = uf.Components();
        CheckChild(uf.Root(0));
        int ans = Rec(uf.Root(0), 1, true);
        Console.WriteLine(ans);
    }

    private void CheckChild(int id)
    {
        seen[id] = true;
        var nexts = new HashSet<int>();
        foreach (int cell in components[id])
        {
            int y = cell / W, x = cell % W;
            for (int d = 0; d < DY.Length; d++)
            {
                int ny = y + DY[d], nx = x + DX[d];
                if (!IsInside(ny, nx)) continue;
                int next = uf.Root(ny * W + nx);
                if (IsInside(ny, nx) && grid[ny][nx] != grid[y][x] && !seen[next] && nexts.Add(next))
                {
                    CheckChild(next);
                    G[id].Add(next);
                }
            }
        }
    }

    bool IsInside(int y, int x) { return y >= 0 && y < H && x >= 0 && x < W; }

    int?[,] memo;
    private int Rec(int i, int canUse, bool empty)
    {
        if (memo[i, canUse].HasValue) return memo[i, canUse].Value;
        int res = 0;

        if (empty)
        {
            foreach (int child in G[i])
                res += Rec(child, canUse, !empty);
            return res;
        }

        foreach (int child in G[i])
            res += Rec(child, 1, !empty);
        if (canUse == 1)
        {
            int use = uf.NumElements(i);
            foreach (int child in G[i])
                use += Rec(child, 0, !empty);
            res = Math.Max(res, use);
        }

        memo[i, canUse] = res;
        return res;
    }

    string[] Surround(string[] grid, char c)
    {
        int H = grid.Length + 2, W = grid[0].Length + 2;
        var res = new string[H];
        for (int i = 0; i < H; i++)
            if (i == 0 || i == H - 1) res[i] = new string(c, W);
            else res[i] = c + grid[i - 1] + c;
        return res;
    }

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

        public UnionFind(int N)
        {
            parent = new int[N];
            rank = new int[N];
            numElements = new int[N];
            for (int i = 0; i < N; i++)
            {
                parent[i] = i;
                numElements[i] = 1;
            }
        }
        public int Root(int x)
        {
            if (parent[x] == x) return x;
            return parent[x] = Root(parent[x]);
        }
        public bool Same(int x, int y)
        {
            return Root(x) == Root(y);
        }
        public int NumElements(int x)
        {
            return numElements[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[x] = y;
            numElements[y] += numElements[x];
            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;
        }
    }
}

class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    private static TextReader reader = Console.In;
    private static readonly char[] separator = { ' ' };
    private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    private static string[] A = new string[0];
    private static int i;
    private 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 Enu.Range(0, N).Select(i => Int()).ToArray(); }
    public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
    public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); }
    public static string Line() { return reader.ReadLine().Trim(); }
    private static string[] Split(string s) { return s.Split(separator, op); }
    private static string Next() { CheckNext(); return A[i++]; }
    private 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