結果

問題 No.1479 Matrix Eraser
ユーザー さかぽんさかぽん
提出日時 2021-07-01 19:30:41
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,182 ms / 3,000 ms
コード長 2,195 bytes
コンパイル時間 2,455 ms
コンパイル使用メモリ 109,424 KB
実行使用メモリ 168,356 KB
最終ジャッジ日時 2023-09-10 17:56:23
合計ジャッジ時間 24,457 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
20,728 KB
testcase_01 AC 64 ms
20,792 KB
testcase_02 AC 65 ms
20,836 KB
testcase_03 AC 64 ms
20,816 KB
testcase_04 AC 64 ms
22,876 KB
testcase_05 AC 65 ms
20,828 KB
testcase_06 AC 63 ms
20,760 KB
testcase_07 AC 145 ms
51,208 KB
testcase_08 AC 235 ms
66,572 KB
testcase_09 AC 463 ms
92,408 KB
testcase_10 AC 852 ms
138,048 KB
testcase_11 AC 517 ms
101,188 KB
testcase_12 AC 173 ms
52,384 KB
testcase_13 AC 229 ms
64,792 KB
testcase_14 AC 191 ms
60,232 KB
testcase_15 AC 98 ms
41,904 KB
testcase_16 AC 201 ms
60,652 KB
testcase_17 AC 1,124 ms
152,536 KB
testcase_18 AC 1,150 ms
150,372 KB
testcase_19 AC 1,182 ms
152,472 KB
testcase_20 AC 1,130 ms
150,552 KB
testcase_21 AC 1,130 ms
152,448 KB
testcase_22 AC 1,147 ms
150,372 KB
testcase_23 AC 1,126 ms
154,440 KB
testcase_24 AC 1,117 ms
153,204 KB
testcase_25 AC 1,177 ms
150,456 KB
testcase_26 AC 1,154 ms
152,512 KB
testcase_27 AC 345 ms
51,960 KB
testcase_28 AC 346 ms
50,124 KB
testcase_29 AC 347 ms
52,120 KB
testcase_30 AC 347 ms
50,056 KB
testcase_31 AC 348 ms
51,876 KB
testcase_32 AC 271 ms
46,676 KB
testcase_33 AC 274 ms
48,756 KB
testcase_34 AC 273 ms
48,716 KB
testcase_35 AC 273 ms
48,664 KB
testcase_36 AC 273 ms
48,644 KB
testcase_37 AC 115 ms
25,344 KB
testcase_38 AC 495 ms
76,788 KB
testcase_39 AC 704 ms
168,356 KB
testcase_40 AC 61 ms
20,728 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.Collections.Generic;

class D
{
	static int[] Read() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
	static (int, int) Read2() { var a = Read(); return (a[0], a[1]); }
	static void Main()
	{
		var (h, w) = Read2();
		var a = Array.ConvertAll(new bool[h], _ => Read());

		var amax = 500000;
		var d = new List<int[]>[amax + 1];
		var dr = new Dictionary<int, int>[amax + 1];
		var dc = new Dictionary<int, int>[amax + 1];

		for (int i = 0; i < h; i++)
		{
			for (int j = 0; j < w; j++)
			{
				var k = a[i][j];
				if (k == 0) continue;

				if (d[k] == null)
				{
					d[k] = new List<int[]>();
					dr[k] = new Dictionary<int, int>();
					dc[k] = new Dictionary<int, int>();
				}

				if (!dr[k].ContainsKey(i))
					dr[k][i] = dr[k].Count;
				if (!dc[k].ContainsKey(j))
					dc[k][j] = dc[k].Count;

				d[k].Add(new[] { dr[k][i], dc[k][j] });
			}
		}

		var r = 0L;

		for (int k = 0; k <= amax; k++)
		{
			if (d[k] == null) continue;
			if (d[k].Count == 1)
			{
				r++;
				continue;
			}

			var bm = new BipartiteMatching(dr[k].Count, dc[k].Count);
			bm.AddEdges(d[k].ToArray());
			r += bm.Dinic();
		}
		Console.WriteLine(r);
	}
}

public class BipartiteMatching
{
	int n1;
	List<int>[] map;
	public int[][] Map;
	int[] match;
	bool[] u;

	// 0 <= v1 < n1, 0 <= v2 < n2
	public BipartiteMatching(int n1, int n2)
	{
		this.n1 = n1;
		var n = n1 + n2;
		map = Array.ConvertAll(new bool[n], _ => new List<int>());
		u = new bool[n];
	}

	public void AddEdge(int from, int to)
	{
		map[from].Add(n1 + to);
		map[n1 + to].Add(from);
	}

	// { from, to }
	public void AddEdges(int[][] des)
	{
		foreach (var e in des) AddEdge(e[0], e[1]);
	}

	bool Dfs(int v1)
	{
		u[v1] = true;
		foreach (var v2 in Map[v1])
		{
			var u1 = match[v2];
			if (u1 == -1 || !u[u1] && Dfs(u1))
			{
				match[v1] = v2;
				match[v2] = v1;
				return true;
			}
		}
		return false;
	}

	public int Dinic()
	{
		Map = Array.ConvertAll(map, l => l.ToArray());
		match = Array.ConvertAll(map, _ => -1);

		var r = 0;
		for (int v1 = 0; v1 < n1; ++v1)
		{
			if (match[v1] != -1) continue;
			Array.Clear(u, 0, u.Length);
			if (Dfs(v1)) r++;
		}
		return r;
	}
}
0