結果

問題 No.1479 Matrix Eraser
ユーザー さかぽんさかぽん
提出日時 2021-07-01 18:20:58
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 2,161 bytes
コンパイル時間 5,075 ms
コンパイル使用メモリ 108,572 KB
実行使用メモリ 168,368 KB
最終ジャッジ日時 2023-09-10 16:02:34
合計ジャッジ時間 14,092 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
24,572 KB
testcase_01 AC 68 ms
24,712 KB
testcase_02 AC 68 ms
22,568 KB
testcase_03 AC 69 ms
22,752 KB
testcase_04 AC 67 ms
22,636 KB
testcase_05 AC 70 ms
22,632 KB
testcase_06 AC 69 ms
22,500 KB
testcase_07 AC 182 ms
50,064 KB
testcase_08 AC 318 ms
61,856 KB
testcase_09 AC 969 ms
96,268 KB
testcase_10 TLE -
testcase_11 AC 1,283 ms
105,912 KB
testcase_12 AC 198 ms
52,280 KB
testcase_13 AC 274 ms
61,752 KB
testcase_14 AC 209 ms
54,768 KB
testcase_15 AC 93 ms
34,840 KB
testcase_16 AC 244 ms
59,264 KB
testcase_17 TLE -
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 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 d = new Dictionary<int, List<int[]>>();
		var dr = new Dictionary<int, HashSet<int>>();
		var dc = new Dictionary<int, HashSet<int>>();

		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.ContainsKey(k))
				{
					d[k] = new List<int[]>();
					dr[k] = new HashSet<int>();
					dc[k] = new HashSet<int>();
				}

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

		var r = 0L;

		foreach (var (k, es) in d)
		{
			if (es.Count == 1)
			{
				r++;
				continue;
			}

			var bm = new BipartiteMatching(h, w);
			bm.AddEdges(es.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