結果

問題 No.421 しろくろチョコレート
ユーザー 37zigen37zigen
提出日時 2016-09-08 23:51:03
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,152 bytes
コンパイル時間 2,534 ms
コンパイル使用メモリ 80,700 KB
実行使用メモリ 653,680 KB
最終ジャッジ日時 2024-04-28 00:52:14
合計ジャッジ時間 7,225 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 MLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package No400番台;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

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

	int[] dx = { 1, -1, 0, 0 };
	int[] dy = { 0, 0, 1, -1 };
	int h, w;
	char[][] choco;
	int[][] comp;

	void solver() {
		Scanner sc = new Scanner(System.in);
		h = sc.nextInt();
		w = sc.nextInt();
		choco = new char[h][w];
		for (int i = 0; i < h; i++) {
			choco[i] = sc.next().toCharArray();
		}

		comp = new int[h][w];
		for (int i = 0; i < h; i++) {
			Arrays.fill(comp[i], -1);
		}

		int color = 0;
		int ans = 0;
		int odd = 0, even = 0;

		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				if (on_filed(x, y) && comp[y][x] == -1) {

					ArrayList<Integer>[] g = new ArrayList[h * w];
					for (int i = 0; i < h * w; i++) {
						g[i] = new ArrayList<>();
					}

					ArrayDeque<Pair> que = new ArrayDeque<>();
					que.add(new Pair(x, y));
					while (!que.isEmpty()) {
						Pair p = que.poll();
						comp[p.y][p.x] = color;

						if ((p.x + p.y) % 2 == 0)
							even++;
						else
							odd++;

						for (int i = 0; i < 4; i++) {
							int nx = p.x + dx[i];
							int ny = p.y + dy[i];
							if (on_filed(nx, ny) && comp[ny][nx] == -1) {

								int a = id(p.x, p.y);
								int b = id(nx, ny);
								g[a].add(b);
								g[b].add(a);

								que.add(new Pair(nx, ny));
							}
						}
					}
					int the_number_of_matchings = bipartiteMatching(g, (h * w) / 2);
					ans += the_number_of_matchings * 100;
					even -= the_number_of_matchings;
					odd -= the_number_of_matchings;
					color++;
				}
			}
		}

		int common = Math.min(even, odd);
		ans += common * 10;
		even -= common;
		odd -= common;
		ans += even + odd;
		System.out.println(ans);
	}

	int id(int x, int y) {
		if ((y * w + x) % 2 == 0)
			return (y * w + x) / 2;
		else
			return (y * w + x) / 2 + (h * w - 1) / 2 + 1;
	}

	boolean on_filed(int x, int y) {
		if (0 <= x && x < w && 0 <= y && y < h && choco[y][x] != '.') {
			return true;
		} else
			return false;
	}

	class Pair {
		int x;
		int y;

		Pair(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

	boolean augment(ArrayList<Integer>[] g, int u, Integer[] matchTo, Boolean[] visited) {
		if (u < 0)
			return true;
		for (int v : g[u]) {
			if (!visited[v]) {
				visited[v] = true;
				if (augment(g, matchTo[v], matchTo, visited)) {
					matchTo[u] = v;
					matchTo[v] = u;
					return true;
				}
			}
		}
		return false;
	}

	// Lは左側の頂点の数
	int bipartiteMatching(ArrayList<Integer>[] g, int L) {
		final int n = g.length;
		Integer[] matchTo = new Integer[n];
		Arrays.fill(matchTo, -1);
		int match = 0;
		for (int i = 0; i <= L; i++) {
			Boolean[] visited = new Boolean[n];
			Arrays.fill(visited, false);
			if (augment(g, i, matchTo, visited))
				match++;
		}
		for (int i = 0; i < L; i++) {
			if (matchTo[i] > 0)
				matching.add(new Edge(i, matchTo[i]));
		}
		return match;
	}

	ArrayList<Edge> matching = new ArrayList<>();

	class Edge {
		int a;
		int b;

		Edge(int a, int b) {
			this.a = a;
			this.b = b;
		}
	}
}
0