結果

問題 No.348 カゴメカゴメ
ユーザー 37zigen37zigen
提出日時 2016-06-14 23:18:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,118 ms / 2,000 ms
コード長 5,079 bytes
コンパイル時間 2,591 ms
コンパイル使用メモリ 84,156 KB
実行使用メモリ 159,244 KB
最終ジャッジ日時 2024-04-17 19:46:52
合計ジャッジ時間 25,131 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 883 ms
139,448 KB
testcase_01 AC 60 ms
37,420 KB
testcase_02 AC 1,099 ms
140,376 KB
testcase_03 AC 992 ms
159,244 KB
testcase_04 AC 71 ms
37,820 KB
testcase_05 AC 80 ms
40,112 KB
testcase_06 AC 56 ms
37,168 KB
testcase_07 AC 53 ms
37,304 KB
testcase_08 AC 53 ms
37,232 KB
testcase_09 AC 55 ms
37,176 KB
testcase_10 AC 54 ms
37,296 KB
testcase_11 AC 55 ms
37,296 KB
testcase_12 AC 155 ms
45,196 KB
testcase_13 AC 122 ms
41,348 KB
testcase_14 AC 77 ms
38,256 KB
testcase_15 AC 620 ms
93,164 KB
testcase_16 AC 54 ms
37,044 KB
testcase_17 AC 54 ms
37,000 KB
testcase_18 AC 55 ms
37,180 KB
testcase_19 AC 54 ms
37,132 KB
testcase_20 AC 60 ms
37,320 KB
testcase_21 AC 55 ms
37,296 KB
testcase_22 AC 54 ms
36,872 KB
testcase_23 AC 1,104 ms
140,040 KB
testcase_24 AC 1,100 ms
136,620 KB
testcase_25 AC 1,100 ms
138,756 KB
testcase_26 AC 1,118 ms
140,180 KB
testcase_27 AC 1,103 ms
135,332 KB
testcase_28 AC 1,098 ms
140,760 KB
testcase_29 AC 1,079 ms
138,104 KB
testcase_30 AC 1,069 ms
137,940 KB
testcase_31 AC 1,046 ms
138,800 KB
testcase_32 AC 1,056 ms
139,568 KB
testcase_33 AC 299 ms
50,096 KB
testcase_34 AC 236 ms
52,232 KB
testcase_35 AC 280 ms
50,092 KB
testcase_36 AC 301 ms
51,260 KB
testcase_37 AC 283 ms
50,220 KB
testcase_38 AC 273 ms
52,268 KB
testcase_39 AC 303 ms
49,640 KB
testcase_40 AC 263 ms
51,492 KB
testcase_41 AC 286 ms
51,656 KB
testcase_42 AC 301 ms
51,476 KB
testcase_43 AC 127 ms
42,136 KB
testcase_44 AC 133 ms
42,032 KB
testcase_45 AC 129 ms
41,908 KB
testcase_46 AC 126 ms
42,632 KB
testcase_47 AC 134 ms
42,312 KB
testcase_48 AC 127 ms
41,992 KB
testcase_49 AC 131 ms
41,524 KB
testcase_50 AC 129 ms
41,968 KB
testcase_51 AC 133 ms
42,216 KB
testcase_52 AC 133 ms
41,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Main {
	public static void main(String[] args) {
		new Main().solver();
	}

	void solver() {
		Scanner sc = new Scanner(System.in);
		int N, M;
		N = sc.nextInt();
		M = sc.nextInt();
		char[][] grid = new char[N + 2][M + 2];
		for (int i = 0; i < M + 2; i++) {
			grid[0][i] = '.';
			grid[N + 1][i] = '.';
		}
		for (int i = 0; i < N + 2; i++) {
			grid[i][0] = '.';
			grid[i][M + 1] = '.';
		}
		for (int i = 0; i < N; i++) {
			String s = sc.next();
			for (int j = 0; j < M; j++) {
				grid[i + 1][j + 1] = s.charAt(j);
			}
		}

		N += 2;
		M += 2;
		DJSet ds = new DJSet(N * M);
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				for (int dx = -1; dx <= 1; dx++) {
					for (int dy = -1; dy <= 1; dy++) {
						if (dx != 0 || dy != 0) {
							int yy = i + dy, xx = j + dx;
							if (yy == -1 || xx == -1 || yy == N || xx == M)
								continue;
							if (grid[i][j] == '.' && Math.abs(dx) + Math.abs(dy) > 1)
								continue;
							if (grid[i][j] != grid[yy][xx])
								continue;
							ds.setUnion(i * M + j, yy * M + xx);
						}
					}
				}
			}
		}
		ArrayList<Integer>[] edges = new ArrayList[N * M];
		for (int i = 0; i < N * M; i++)
			edges[i] = new ArrayList<Integer>();

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				for (int dx = -1; dx <= 1; dx++) {
					for (int dy = -1; dy <= 1; dy++) {
						if (dy != 0 || dx != 0) {
							int yy = i + dy, xx = j + dx;
							if (xx == -1 || yy == -1 || xx == M || yy == N)
								continue;
							if (Math.abs(dx) + Math.abs(dy) > 1)
								continue;
							if (grid[i][j] != grid[yy][xx]) {
								edges[ds.root(i * M + j)].add(ds.root(yy * M + xx));
							}
						}
					}
				}
			}
		}

		for (int i = 0; i < N * M; i++) {
			Set<Integer> set = new HashSet<Integer>();
			for (int j = 0; j < edges[i].size(); j++) {
				set.add(edges[i].get(j));
			}
			edges[i].clear();
			for (Iterator<Integer> it = set.iterator(); it.hasNext();) {
				edges[i].add(it.next());
			}
		}

		memo = new int[2 * N * M + 10];
		for (int i = 0; i < 2 * N * M + 10; i++)
			memo[i] = -1;

		System.out.println(rec(ds.root(0), -1, false, edges, grid, ds));
	}

	int rec(int i, int p, boolean b, ArrayList<Integer>[] edges, char[][] grid, DJSet ds) {
		int r = memo[2 * i + (b ? 1 : 0)];
		if (r == -2)
			throw new AssertionError("error");
		if (r != -1)
			return r;
		r = -2;
		if (grid[i / grid[0].length][i % grid[0].length] == '.') {
			int t = 0;
			for (Iterator<Integer> it = edges[i].iterator(); it.hasNext();) {
				int j = it.next();
				if (j != p) {
					t += rec(j, i, b, edges, grid, ds);
				}
			}
			r = t;
		} else {
			int t = 0, u = 0;
			// b=true:一つ外が囲まれている
			// b=false:一つ外が囲まれていない
			for (Iterator<Integer> it = edges[i].iterator(); it.hasNext();) {
				int j = it.next();
				if (j != p) {
					int x = rec(j, i, false, edges, grid, ds);
					int y = rec(j, i, true, edges, grid, ds);
					u += y;
					t += Math.max(x, y);
				}
			}
			r = t;
			u += ds.size(i);
			if (!b) {
				r = Math.max(r, u);
			}
		}
		return memo[2 * i + (b ? 1 : 0)] = r;
	}

	int[] memo;

	class DJSet {
		int n;// the number of vertices
		int[] d;

		DJSet(int n) {
			this.n = n;
			d = new int[n];
			Arrays.fill(d, -1);
		}

		int root(int x) {
			return d[x] < 0 ? x : root(d[x]);
		}

		boolean setUnion(int x, int y) {
			x = root(x);
			y = root(y);
			if (x != y) {
				if (x < y) {
					int d = x;
					x = y;
					y = d;
				}
				// x>y
				d[y] += d[x];
				d[x] = y;
			}
			return x != y;
		}

		boolean equiv(int x, int y) {
			return root(x) == root(y);
		}

		// xを含む木のNodeの数
		int size(int x) {
			return d[root(x)] * (-1);
		}

		// 連結グラフの数
		int count() {
			int ct = 0;
			for (int u : d) {
				if (u < 0)
					ct++;
			}
			return ct;
		}
	}

	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner(InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException {
			try {
				if (it == null || !it.hasNext())
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0