結果

問題 No.348 カゴメカゴメ
ユーザー eitahoeitaho
提出日時 2016-02-28 16:24:54
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 7,633 bytes
コンパイル時間 6,029 ms
コンパイル使用メモリ 118,396 KB
実行使用メモリ 58,924 KB
最終ジャッジ日時 2024-09-24 12:06:51
合計ジャッジ時間 8,305 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
39,492 KB
testcase_01 AC 41 ms
25,700 KB
testcase_02 AC 316 ms
58,924 KB
testcase_03 AC 166 ms
41,752 KB
testcase_04 AC 35 ms
27,568 KB
testcase_05 AC 36 ms
25,500 KB
testcase_06 AC 41 ms
27,404 KB
testcase_07 AC 40 ms
25,516 KB
testcase_08 AC 35 ms
25,628 KB
testcase_09 AC 40 ms
27,872 KB
testcase_10 AC 40 ms
25,496 KB
testcase_11 AC 41 ms
25,520 KB
testcase_12 AC 43 ms
25,544 KB
testcase_13 AC 43 ms
25,884 KB
testcase_14 WA -
testcase_15 AC 61 ms
40,484 KB
testcase_16 AC 41 ms
27,740 KB
testcase_17 AC 41 ms
25,752 KB
testcase_18 AC 41 ms
27,748 KB
testcase_19 AC 40 ms
25,368 KB
testcase_20 AC 41 ms
25,756 KB
testcase_21 AC 40 ms
25,624 KB
testcase_22 AC 40 ms
23,476 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 42 ms
25,624 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
using System.Text.RegularExpressions;

class Program
{
    public void Solve()
    {
        int H = Reader.Int(), W = Reader.Int();
        var grid = Reader.StringArray(H);
        var uf = new UnionFind(H * W);
        var circleDic = new Dictionary<int, Circle>();
        Func<int, int, bool> IsInside = (y, x) => y >= 0 && y < H && x >= 0 && x < W;

        for (int y = 0; y < H; y++)
            for (int x = 0; x < W; x++)
                if (grid[y][x] == 'x')
                    for (int dy = -1; dy <= 1; dy++)
                        for (int dx = -1; dx <= 1; dx++)
                        {
                            if (dy == 0 && dx == 0) continue;
                            int ny = y + dy, nx = x + dx;
                            if (IsInside(ny, nx) && grid[ny][nx] == 'x')
                                uf.Unite(y * W + x, ny * W + nx);
                        }
        for (int y = 0; y < H; y++)
            for (int x = 0; x < W; x++)
                if (grid[y][x] == 'x')
                {
                    int id = uf.Root(y * W + x);
                    if (uf.NumElements(id) < 3) continue;
                    if (!circleDic.ContainsKey(id)) circleDic[id] = new Circle(id);
                    circleDic[id].UpdateY(y);
                    circleDic[id].UpdateX(x);
                }
        var circles = circleDic.Values.ToArray();
        var numElements = circles.Select(c => uf.NumElements(c.Id)).ToArray();
        int N = circles.Length;
        var parent = Enu.Repeat(-1, N).ToArray();
        var G = CheckChild(circles, N, parent);
        memo = new int?[N, 2];
        int ans = 0;

        for (int i = 0; i < N; i++)
            if (parent[i] == -1)
                ans += Rec(i, 1, numElements, G);

        Console.WriteLine(ans);
    }

    private int[][] CheckChild(Circle[] circles, int N, int[] parent)
    {
        const int Big = 10000, Div = 30;
        var bucket = new Dictionary<int, List<int>>();

        for (int i = 0; i < N; i++)
        {
            int id = circles[i].X1 / Div * Big + circles[i].Y1 / Div;
            if (!bucket.ContainsKey(id)) bucket[id] = new List<int>();
            bucket[id].Add(i);
        }
        for (int a = 0; a < N; a++)
        {
            int sx = circles[a].X1 / Div;
            int sy = circles[a].Y1 / Div;
            for (int x = sx; x <= circles[a].X2 / Div; x++)
                for (int y = sy; y <= circles[a].Y2 / Div; y++)
                {
                    int id = x * Big + y;
                    if (!bucket.ContainsKey(id)) continue;
                    foreach (int b in bucket[id])
                        if (circles[b].IsInside(circles[a]) &&
                            (parent[b] == -1 || circles[a].IsInside(circles[parent[b]])))
                            parent[b] = a;
                }
        }
        var G = Enu.Range(0, N).Select(i => new List<int>()).ToArray();
        for (int i = 0; i < N; i++)
            if (parent[i] != -1)
                G[parent[i]].Add(i);
        return G.Select(g => g.ToArray()).ToArray();
    }


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

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

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

    class Circle
    {
        public int Id;
        public int X1 = int.MaxValue, X2 = int.MinValue;
        public int Y1 = int.MaxValue, Y2 = int.MinValue;

        public Circle(int id) { Id = id; }

        public void UpdateX(int x)
        {
            X1 = Math.Min(X1, x);
            X2 = Math.Max(X2, x);
        }
        public void UpdateY(int y)
        {
            Y1 = Math.Min(Y1, y);
            Y2 = Math.Max(Y2, y);
        }
        public bool IsInside(Circle b)
        {
            return X1 > b.X1 && X2 < b.X2 && Y1 > b.Y1 && Y2 < b.Y2;
        }
    }


    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 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.Values.Select(c => c.ToArray()).ToArray();
        }
    }
}


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