結果

問題 No.1479 Matrix Eraser
ユーザー ningenMeningenMe
提出日時 2021-04-19 02:15:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 278 ms / 3,000 ms
コード長 4,275 bytes
コンパイル時間 2,842 ms
コンパイル使用メモリ 230,404 KB
実行使用メモリ 24,396 KB
最終ジャッジ日時 2023-09-17 08:10:42
合計ジャッジ時間 10,088 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
14,884 KB
testcase_01 AC 7 ms
14,832 KB
testcase_02 AC 7 ms
14,912 KB
testcase_03 AC 8 ms
14,740 KB
testcase_04 AC 7 ms
15,056 KB
testcase_05 AC 7 ms
14,548 KB
testcase_06 AC 7 ms
15,032 KB
testcase_07 AC 40 ms
15,604 KB
testcase_08 AC 63 ms
16,148 KB
testcase_09 AC 126 ms
17,816 KB
testcase_10 AC 231 ms
20,100 KB
testcase_11 AC 147 ms
18,460 KB
testcase_12 AC 49 ms
15,972 KB
testcase_13 AC 64 ms
16,204 KB
testcase_14 AC 51 ms
15,920 KB
testcase_15 AC 16 ms
15,084 KB
testcase_16 AC 56 ms
15,932 KB
testcase_17 AC 276 ms
20,832 KB
testcase_18 AC 277 ms
20,936 KB
testcase_19 AC 276 ms
21,156 KB
testcase_20 AC 276 ms
21,216 KB
testcase_21 AC 275 ms
20,832 KB
testcase_22 AC 275 ms
20,936 KB
testcase_23 AC 276 ms
21,040 KB
testcase_24 AC 274 ms
20,948 KB
testcase_25 AC 277 ms
20,940 KB
testcase_26 AC 278 ms
21,004 KB
testcase_27 AC 162 ms
17,972 KB
testcase_28 AC 165 ms
18,152 KB
testcase_29 AC 163 ms
18,144 KB
testcase_30 AC 163 ms
18,256 KB
testcase_31 AC 164 ms
18,000 KB
testcase_32 AC 72 ms
24,200 KB
testcase_33 AC 72 ms
24,296 KB
testcase_34 AC 71 ms
24,396 KB
testcase_35 AC 73 ms
24,292 KB
testcase_36 AC 73 ms
24,364 KB
testcase_37 AC 23 ms
16,832 KB
testcase_38 AC 176 ms
17,136 KB
testcase_39 AC 265 ms
22,520 KB
testcase_40 AC 7 ms
14,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using int64   = long long;

/*
 * @title Zarts - 座標圧縮
 * @docs md/util/Zarts.md
 */
template<class T> class Zarts{
    vector<T> value;
    map<T,int> key;
    size_t sz;
public:
    vector<int> compressed;
    Zarts(const vector<T> & ar, int light_flag = 0, T pre=-1) : compressed(ar.size()) {
        if(!light_flag) {
            for (auto &e : ar) key[e];
            int cnt=0;
            for (auto &e : key) e.second = cnt++;
            for (int i=0;i<ar.size();++i) compressed[i]=key[ar[i]];
            value.resize(key.size());
            for (auto &e : key) value[e.second] = e.first;
            sz = cnt;
        }
        else {
            vector<pair<int,int>> ord(ar.size());
            for(int i=0;i<ar.size();++i) ord[i]={ar[i],i};
            sort(ord.begin(),ord.end());
            int cnt=-1;
            for(int i=0;i<ar.size();++i) {
                if(pre < ord[i].first) cnt++;
                compressed[ord[i].second] = cnt;
                pre = ord[i].first;
            }
            sz = cnt+1;
        }
    }
    T get_value(int key) {
        return value[key];
    }
    int get_key(T value) {
        assert(key.count(value));
        return key[value];
    }
    size_t size() {
        return sz;
    }
};

/*
 * @title Dinic - Dinicフロー
 * @docs md/graph/Dinic.md
 */
template <class T> class Dinic {
	struct info {
		int to, rev;
		T cap;
	};
	T ini, inf;
	vector<vector<info>> edge;
	vector<int> level, iter;

	inline void bfs(int start) {
		for (int i = 0; i < level.size(); ++i) level[i] = -1;
		queue<int> q;
		level[start] = 0;
		q.push(start);
		while (q.size()) {
			int from = q.front();
			q.pop();
			for (auto& e : edge[from]) {
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[from] + 1;
					q.push(e.to);
				}
			}
		}
	}

	inline T dfs(int from, int goal, T flow) {
		if (from == goal) return flow;
		for (int& i = iter[from]; i < edge[from].size(); ++i) {
			auto& e = edge[from][i];
			if (e.cap <= 0 || level[from] >= level[e.to]) continue;
			T dflow = dfs(e.to, goal, min(flow, e.cap));
			if (dflow <= 0) continue;
			e.cap -= dflow;
			edge[e.to][e.rev].cap += dflow;
			return dflow;
		}
		return ini;
	}

public:
	Dinic(int N, T ini, T inf) : edge(N), level(N), iter(N), ini(ini), inf(inf) {
		// do nothing
	}

	inline void make_edge(int from, int to, T cap) {
		edge[from].push_back({ to, (int)edge[to].size(), cap });
		edge[to].push_back({ from, (int)edge[from].size() - 1, ini });
	}

	inline T maxflow(int start, int goal) {
		T maxflow = ini;
		while (1) {
			bfs(start);
			if (level[goal] < 0) return maxflow;
			for (int i = 0; i < iter.size(); ++i) iter[i] = 0;
			T flow;
			while ((flow = dfs(start, goal, inf))>0) maxflow += flow;
		}
	}
};

//verify https://atcoder.jp/contests/arc085/tasks/arc085_c

int solve(int H, vector<int> A, int W, vector<int> B) {
    int N = A.size();
    int inf = 1000000;

    //行: 「操作を行う」:0,「操作を行わない」:1と割り当てる
    //列: 「操作を行わない」:0,「操作を行う」:1と割り当てる
    Dinic<int> di(H+W+2,0,inf);
    int S = H+W, G = H+W+1;
    int ans = 0;

    //iが0なら1失う
    for(int i=0;i<H;++i) di.make_edge(i,G,1);
    //jが1なら1失う
    for(int j=0;j<W;++j) di.make_edge(S,j+H,1);
    //iが1,jが0のときinf失う
    for(int n=0;n<N;++n) {
        int i = A[n];
        int j = B[n];
        di.make_edge(j+H,i,inf);
    }
    return ans - di.maxflow(S,G);
}

/**
 * @url 
 * @est
 */ 
int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    int H,W; cin >> H >> W;
    int M = 500010;
    vector<vector<pair<int,int>>> vvp(M);
    for(int i=0;i<H;++i) {
        for(int j=0;j<W;++j) {
            int a; cin >> a;
            vvp[a].push_back({i,j});
        }
    }
    int ans = 0;
    for(int i=1;i<M;++i) {
        if(vvp[i].empty()) continue;
        int n = vvp[i].size();
        vector<int> Y(n),X(n);
        for(int j=0;j<n;++j) Y[j] = vvp[i][j].first;
        for(int j=0;j<n;++j) X[j] = vvp[i][j].second;
        Zarts<int> ZY(Y),ZX(X);
        int cnt = solve(ZY.size(),ZY.compressed,ZX.size(),ZX.compressed);
        ans += abs(cnt);
    }
    cout << ans << endl;
    return 0;
}
0