結果
問題 | No.1479 Matrix Eraser |
ユーザー | kakel-san |
提出日時 | 2024-07-04 23:55:43 |
言語 | C# (.NET 8.0.203) |
結果 |
AC
|
実行時間 | 882 ms / 3,000 ms |
コード長 | 7,661 bytes |
コンパイル時間 | 16,722 ms |
コンパイル使用メモリ | 169,560 KB |
実行使用メモリ | 209,104 KB |
最終ジャッジ日時 | 2024-07-04 23:56:24 |
合計ジャッジ時間 | 40,816 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 88 ms
51,324 KB |
testcase_01 | AC | 87 ms
51,288 KB |
testcase_02 | AC | 91 ms
51,200 KB |
testcase_03 | AC | 88 ms
51,284 KB |
testcase_04 | AC | 87 ms
51,256 KB |
testcase_05 | AC | 85 ms
51,156 KB |
testcase_06 | AC | 88 ms
51,280 KB |
testcase_07 | AC | 242 ms
86,240 KB |
testcase_08 | AC | 304 ms
86,264 KB |
testcase_09 | AC | 555 ms
87,812 KB |
testcase_10 | AC | 756 ms
94,720 KB |
testcase_11 | AC | 577 ms
87,728 KB |
testcase_12 | AC | 265 ms
86,140 KB |
testcase_13 | AC | 304 ms
86,272 KB |
testcase_14 | AC | 272 ms
86,272 KB |
testcase_15 | AC | 147 ms
67,200 KB |
testcase_16 | AC | 282 ms
86,272 KB |
testcase_17 | AC | 870 ms
98,372 KB |
testcase_18 | AC | 861 ms
98,560 KB |
testcase_19 | AC | 873 ms
98,556 KB |
testcase_20 | AC | 842 ms
98,628 KB |
testcase_21 | AC | 868 ms
98,468 KB |
testcase_22 | AC | 874 ms
98,628 KB |
testcase_23 | AC | 852 ms
98,372 KB |
testcase_24 | AC | 882 ms
98,688 KB |
testcase_25 | AC | 866 ms
98,500 KB |
testcase_26 | AC | 872 ms
98,496 KB |
testcase_27 | AC | 420 ms
83,936 KB |
testcase_28 | AC | 451 ms
83,896 KB |
testcase_29 | AC | 428 ms
83,968 KB |
testcase_30 | AC | 430 ms
84,096 KB |
testcase_31 | AC | 426 ms
83,932 KB |
testcase_32 | AC | 805 ms
87,296 KB |
testcase_33 | AC | 788 ms
87,176 KB |
testcase_34 | AC | 778 ms
87,296 KB |
testcase_35 | AC | 793 ms
87,296 KB |
testcase_36 | AC | 799 ms
87,436 KB |
testcase_37 | AC | 144 ms
70,796 KB |
testcase_38 | AC | 484 ms
84,076 KB |
testcase_39 | AC | 858 ms
98,492 KB |
testcase_40 | AC | 89 ms
209,104 KB |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (95 ms)。 MSBuild のバージョン 17.9.6+a4ecab324 (.NET) main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (h, w) = (c[0], c[1]); var a = NArr(h); var dic = new List<(int x, int y)>[500_001]; for (var i = 0; i < dic.Length; ++i) dic[i] = new List<(int x, int y)>(); for (var i = 0; i < h; ++i) for (var j = 0; j < w; ++j) { dic[a[i][j]].Add((i, j)); } var ans = 0L; for (var i = 500000; i > 0; --i) if (dic[i].Count > 0) { var xset = new HashSet<int>(); var yset = new HashSet<int>(); foreach (var l in dic[i]) { xset.Add(l.x); yset.Add(l.y); } var xlist = new List<int>(xset); var ylist = new List<int>(yset); var xdic = new Dictionary<int, int>(); for (var j = 0; j < xlist.Count; ++j) xdic[xlist[j]] = j; var ydic = new Dictionary<int, int>(); for (var j = 0; j < ylist.Count; ++j) ydic[ylist[j]] = j; var g = new MaxFlowGraph(xlist.Count + ylist.Count + 2); var s = xlist.Count + ylist.Count; for (var j = 0; j < xlist.Count; ++j) { g.AddEdge(s, j, 1); } for (var j = 0; j < ylist.Count; ++j) { g.AddEdge(j + xlist.Count, s + 1, 1); } foreach (var l in dic[i]) { g.AddEdge(xdic[l.x], ydic[l.y] + xlist.Count, int.MaxValue); } var f = g.Flow(s, s + 1); ans += f; } WriteLine(ans); } // 最大フローグラフ // MaxFlowGraph(n) n:頂点数 // AddEdge(from, to, cap) from,to:0-indexed cap:最大用量 // Flow(from, to) 最大流量を求める 各辺の流量を変更 // Flow(from, to, flowLimit) // MinCut(from) Flow(最小カット)実行後の残余グラフで各頂点への到達可能性を取得 // GetEdge(i), GetEdges() 内部の辺の状態を取得 // ChangeEdge(i, newCap, newFlow) 対象辺の流量を変更 class MaxFlowGraph { int _n; long max = long.MaxValue; class EdgeI { public int to { get; private set; } public int rev { get; private set; } public long cap { get; set; } public EdgeI(int to, int rev, long cap) { this.to = to; this.rev = rev; this.cap = cap; } } List<KeyValuePair<int, int>> pos = new List<KeyValuePair<int, int>>(); List<EdgeI>[] g; public MaxFlowGraph(int n) { _n = n; g = new List<EdgeI>[n]; for (var i = 0; i < n; ++i) g[i] = new List<EdgeI>(); } public int AddEdge(int from, int to, long cap) { var m = pos.Count; pos.Add(new KeyValuePair<int, int>(from, g[from].Count)); g[from].Add(new EdgeI(to, g[to].Count, cap)); g[to].Add(new EdgeI(from, g[from].Count - 1, 0)); return m; } public class Edge { public int from { get; private set; } public int to { get; private set; } public long cap { get; private set; } public long flow { get; private set; } public Edge(int from, int to, long cap, long flow) { this.from = from; this.to = to; this.cap = cap; this.flow = flow; } } public Edge GetEdge(int i) { var m = pos.Count; var e = g[pos[i].Key][pos[i].Value]; var re = g[e.to][e.rev]; return new Edge(pos[i].Key, e.to, e.cap + re.cap, re.cap); } public List<Edge> GetEdges() { var m = pos.Count; var result = new List<Edge>(); for (var i = 0; i < m; ++i) result.Add(GetEdge(i)); return result; } public void ChangeEdge(int i, long new_cap, long new_flow) { var m = pos.Count; var e = g[pos[i].Key][pos[i].Value]; var re = g[e.to][e.rev]; e.cap = new_cap - new_flow; re.cap = new_flow; } public long Flow(int s, int t) { return Flow(s, t, max); } public long Flow(int s, int t, long limit) { var level = new int[_n]; var iter = new int[_n]; var que = new Queue<int>(); void bfs() { for (var i = 0; i < _n; ++i) level[i] = -1; level[s] = 0; que.Clear(); que.Enqueue(s); while (que.Count > 0) { var v = que.Dequeue(); foreach (var e in g[v]) { if (e.cap == 0 || level[e.to] >= 0) continue; level[e.to] = level[v] + 1; if (e.to == t) return; que.Enqueue(e.to); } } } long dfs(int v, long up) { if (v == s) return up; long res = 0; var level_v = level[v]; for (var i = iter[v]; i < g[v].Count; ++i) { var e = g[v][i]; if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue; long d = dfs(e.to, Min(up - res, g[e.to][e.rev].cap)); if (d <= 0) continue; g[v][i].cap += d; g[e.to][e.rev].cap -= d; res += d; if (res == up) break; } return res; } long flow = 0; while (flow < limit) { bfs(); if (level[t] == -1) break; for (var i = 0; i < iter.Length; ++i) iter[i] = 0; while (flow < limit) { var f = dfs(t, limit - flow); if (f == 0) break; flow += f; } } return flow; } long Min(long a, long b) { return a.CompareTo(b) < 0 ? a : b; } public bool[] MinCut(int s) { var visited = new bool[_n]; var que = new Queue<int>(); que.Enqueue(s); while (que.Count > 0) { var p = que.Dequeue(); visited[p] = true; foreach (var e in g[p]) { if (e.cap != 0 && !visited[e.to]) { visited[e.to] = true; que.Enqueue(e.to); } } } return visited; } } }