結果

問題 No.1479 Matrix Eraser
ユーザー trineutrontrineutron
提出日時 2021-04-17 01:03:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 478 ms / 3,000 ms
コード長 1,665 bytes
コンパイル時間 2,757 ms
コンパイル使用メモリ 225,288 KB
実行使用メモリ 26,304 KB
最終ジャッジ日時 2023-09-16 05:05:16
合計ジャッジ時間 11,980 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 47 ms
5,872 KB
testcase_08 AC 83 ms
7,572 KB
testcase_09 AC 188 ms
12,396 KB
testcase_10 AC 372 ms
18,356 KB
testcase_11 AC 224 ms
13,800 KB
testcase_12 AC 59 ms
6,324 KB
testcase_13 AC 83 ms
7,820 KB
testcase_14 AC 63 ms
6,676 KB
testcase_15 AC 14 ms
4,396 KB
testcase_16 AC 76 ms
7,396 KB
testcase_17 AC 451 ms
22,372 KB
testcase_18 AC 456 ms
22,284 KB
testcase_19 AC 462 ms
22,324 KB
testcase_20 AC 463 ms
22,308 KB
testcase_21 AC 474 ms
22,256 KB
testcase_22 AC 465 ms
22,356 KB
testcase_23 AC 473 ms
22,276 KB
testcase_24 AC 471 ms
22,264 KB
testcase_25 AC 478 ms
22,368 KB
testcase_26 AC 455 ms
22,344 KB
testcase_27 AC 147 ms
7,072 KB
testcase_28 AC 146 ms
7,024 KB
testcase_29 AC 145 ms
7,312 KB
testcase_30 AC 145 ms
7,280 KB
testcase_31 AC 145 ms
7,364 KB
testcase_32 AC 76 ms
11,548 KB
testcase_33 AC 79 ms
11,560 KB
testcase_34 AC 78 ms
11,432 KB
testcase_35 AC 79 ms
11,332 KB
testcase_36 AC 79 ms
11,524 KB
testcase_37 AC 36 ms
4,564 KB
testcase_38 AC 196 ms
6,536 KB
testcase_39 AC 360 ms
26,304 KB
testcase_40 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/maxflow>

using namespace std;
using namespace atcoder;

int main()
{
    int h, w;
    cin >> h >> w;
    vector a(h, vector<int>(w));
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            cin >> a.at(i).at(j);
        }
    }
    unordered_map<int, vector<pair<int, int>>> m;
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            if (a.at(i).at(j))
            {
                m[a.at(i).at(j)].emplace_back(i, h + j);
            }
        }
    }
    int ans = 0;
    for (auto &&[i, v] : m)
    {
        unordered_map<int, int> newindex;
        for (auto &&k : v)
        {
            if (newindex[k.first] == 0)
            {
                newindex[k.first] = newindex.size();
            }
            if (newindex[k.second] == 0)
            {
                newindex[k.second] = newindex.size();
            }
        }
        const int n = newindex.size();
        int s = 0, t = n + 1;
        mf_graph<int> flow(n + 2);
        vector<bool> used(n + 2);
        for (auto &&k : v)
        {
            flow.add_edge(newindex[k.first], newindex[k.second], 1);
            if (not used.at(newindex[k.first]))
            {
                flow.add_edge(s, newindex[k.first], 1);
                used.at(newindex[k.first]) = true;
            }
            if (not used.at(newindex[k.second]))
            {
                flow.add_edge(newindex[k.second], t, 1);
                used.at(newindex[k.second]) = true;
            }
        }
        ans += flow.flow(s, t);
    }
    cout << ans << endl;
    return 0;
}
0