結果

問題 No.1479 Matrix Eraser
ユーザー さかぽんさかぽん
提出日時 2021-07-01 19:20:39
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,332 ms / 3,000 ms
コード長 2,341 bytes
コンパイル時間 2,705 ms
コンパイル使用メモリ 107,560 KB
実行使用メモリ 170,320 KB
最終ジャッジ日時 2023-09-10 17:46:34
合計ジャッジ時間 27,365 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
20,872 KB
testcase_01 AC 68 ms
20,780 KB
testcase_02 AC 68 ms
20,740 KB
testcase_03 AC 66 ms
18,692 KB
testcase_04 AC 66 ms
22,880 KB
testcase_05 AC 68 ms
20,740 KB
testcase_06 AC 65 ms
20,740 KB
testcase_07 AC 189 ms
51,220 KB
testcase_08 AC 267 ms
66,640 KB
testcase_09 AC 548 ms
96,376 KB
testcase_10 AC 1,022 ms
135,948 KB
testcase_11 AC 609 ms
103,516 KB
testcase_12 AC 213 ms
59,244 KB
testcase_13 AC 271 ms
66,684 KB
testcase_14 AC 221 ms
60,240 KB
testcase_15 AC 106 ms
41,996 KB
testcase_16 AC 232 ms
62,848 KB
testcase_17 AC 1,327 ms
150,512 KB
testcase_18 AC 1,313 ms
150,512 KB
testcase_19 AC 1,307 ms
152,540 KB
testcase_20 AC 1,309 ms
154,632 KB
testcase_21 AC 1,323 ms
150,396 KB
testcase_22 AC 1,318 ms
152,440 KB
testcase_23 AC 1,332 ms
150,476 KB
testcase_24 AC 1,326 ms
155,060 KB
testcase_25 AC 1,311 ms
152,604 KB
testcase_26 AC 1,320 ms
150,888 KB
testcase_27 AC 349 ms
52,024 KB
testcase_28 AC 352 ms
48,128 KB
testcase_29 AC 354 ms
54,124 KB
testcase_30 AC 351 ms
50,048 KB
testcase_31 AC 351 ms
49,928 KB
testcase_32 AC 278 ms
48,704 KB
testcase_33 AC 276 ms
46,604 KB
testcase_34 AC 280 ms
48,720 KB
testcase_35 AC 278 ms
48,676 KB
testcase_36 AC 280 ms
48,652 KB
testcase_37 AC 119 ms
25,400 KB
testcase_38 AC 545 ms
77,020 KB
testcase_39 AC 784 ms
170,320 KB
testcase_40 AC 63 ms
22,788 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());
			var res = bm.Dinic();
			r += res.Length;
		}
		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);

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

		var r = new List<int[]>();
		for (int v1 = 0; v1 < n1; ++v1)
			if (match[v1] != -1)
				r.Add(new[] { v1, match[v1] - n1 });
		return r.ToArray();
	}
}
0