結果
問題 | No.348 カゴメカゴメ |
ユーザー | eitaho |
提出日時 | 2016-02-26 23:06:26 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 6,634 bytes |
コンパイル時間 | 1,262 ms |
コンパイル使用メモリ | 117,652 KB |
実行使用メモリ | 52,492 KB |
最終ジャッジ日時 | 2024-09-22 22:41:25 |
合計ジャッジ時間 | 6,127 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | RE | - |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | RE | - |
testcase_31 | RE | - |
testcase_32 | RE | - |
testcase_33 | RE | - |
testcase_34 | RE | - |
testcase_35 | RE | - |
testcase_36 | RE | - |
testcase_37 | RE | - |
testcase_38 | RE | - |
testcase_39 | RE | - |
testcase_40 | RE | - |
testcase_41 | RE | - |
testcase_42 | RE | - |
testcase_43 | RE | - |
testcase_44 | RE | - |
testcase_45 | RE | - |
testcase_46 | RE | - |
testcase_47 | RE | - |
testcase_48 | RE | - |
testcase_49 | RE | - |
testcase_50 | RE | - |
testcase_51 | RE | - |
testcase_52 | RE | - |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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); Func<int, int, bool> IsInside = (r, c) => r >= 0 && r < H && c >= 0 && c < W; var rectDic = new Dictionary<int, Rect>(); for (int r = 0; r < H; r++) for (int c = 0; c < W; c++) if (grid[r][c] == 'x') for (int dr = -1; dr <= 1; dr++) for (int dc = -1; dc <= 1; dc++) { if (dr == 0 && dc == 0) continue; int nr = r + dr, nc = c + dc; if (IsInside(nr, nc) && grid[nr][nc] == 'x') uf.Unite(r * W + c, nr * W + nc); } for (int r = 0; r < H; r++) for (int c = 0; c < W; c++) if (grid[r][c] == 'x') { int id = uf.Root(r * W + c); if (!rectDic.ContainsKey(id)) rectDic[id] = new Rect(id); rectDic[id].UpdateY(r); rectDic[id].UpdateX(c); } throw new Exception(); var ids = rectDic.Keys.ToArray(); var rects = rectDic.Values.ToArray(); int N = rects.Length; int[] parent = Enu.Repeat(-1, N).ToArray(); for (int a = 0; a < N; a++) for (int b = 0; b < N; b++) if (rects[a].IsInside(rects[b]) && (parent[a] == -1 || rects[b].IsInside(rects[parent[a]]))) parent[a] = b; 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); memo = new int?[N, 2]; int ans = 0; for (int i = 0; i < N; i++) if (parent[i] == -1) ans += Rec(i, 1, ids, G, uf); Console.WriteLine(ans); } int?[,] memo; private int Rec(int i, int canUse, int[] ids, List<int>[] G, UnionFind uf) { if (memo[i, canUse].HasValue) return memo[i, canUse].Value; int res = 0; int id = ids[i]; foreach (int child in G[i]) res += Rec(child, 1, ids, G, uf); if (canUse == 1) { int other = uf.NumElements(id); foreach (int child in G[i]) other += Rec(child, 0, ids, G, uf); res = Math.Max(res, other); } memo[i, canUse] = res; return res; } class Rect { public int Id; public int X1 = int.MaxValue, X2 = int.MinValue; public int Y1 = int.MaxValue, Y2 = int.MinValue; public Rect(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(Rect 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; } }