結果
| 問題 |
No.1479 Matrix Eraser
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-07-01 18:20:58 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,161 bytes |
| コンパイル時間 | 3,186 ms |
| コンパイル使用メモリ | 116,472 KB |
| 実行使用メモリ | 203,584 KB |
| 最終ジャッジ日時 | 2024-06-28 07:09:44 |
| 合計ジャッジ時間 | 10,931 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 14 TLE * 2 -- * 23 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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();
}
}