結果

問題 No.1479 Matrix Eraser
ユーザー ygussanyygussany
提出日時 2021-03-21 10:55:28
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1,292 ms / 3,000 ms
コード長 4,197 bytes
コンパイル時間 1,338 ms
コンパイル使用メモリ 33,280 KB
実行使用メモリ 30,336 KB
最終ジャッジ日時 2024-05-01 19:46:20
合計ジャッジ時間 17,966 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,808 KB
testcase_01 AC 5 ms
7,808 KB
testcase_02 AC 5 ms
7,808 KB
testcase_03 AC 6 ms
7,808 KB
testcase_04 AC 5 ms
7,808 KB
testcase_05 AC 5 ms
7,680 KB
testcase_06 AC 5 ms
7,808 KB
testcase_07 AC 22 ms
8,960 KB
testcase_08 AC 49 ms
9,216 KB
testcase_09 AC 206 ms
10,240 KB
testcase_10 AC 820 ms
11,904 KB
testcase_11 AC 281 ms
10,368 KB
testcase_12 AC 22 ms
8,576 KB
testcase_13 AC 39 ms
8,960 KB
testcase_14 AC 23 ms
8,448 KB
testcase_15 AC 6 ms
7,808 KB
testcase_16 AC 31 ms
8,704 KB
testcase_17 AC 1,241 ms
12,800 KB
testcase_18 AC 1,264 ms
12,800 KB
testcase_19 AC 1,267 ms
12,800 KB
testcase_20 AC 1,228 ms
12,672 KB
testcase_21 AC 1,292 ms
12,672 KB
testcase_22 AC 1,267 ms
12,800 KB
testcase_23 AC 1,251 ms
12,800 KB
testcase_24 AC 1,262 ms
12,800 KB
testcase_25 AC 1,258 ms
12,800 KB
testcase_26 AC 1,252 ms
12,800 KB
testcase_27 AC 113 ms
13,184 KB
testcase_28 AC 111 ms
13,184 KB
testcase_29 AC 116 ms
13,184 KB
testcase_30 AC 113 ms
13,056 KB
testcase_31 AC 117 ms
13,184 KB
testcase_32 AC 57 ms
30,336 KB
testcase_33 AC 63 ms
30,336 KB
testcase_34 AC 57 ms
30,336 KB
testcase_35 AC 55 ms
30,336 KB
testcase_36 AC 55 ms
30,336 KB
testcase_37 AC 23 ms
8,576 KB
testcase_38 AC 58 ms
12,672 KB
testcase_39 AC 34 ms
12,544 KB
testcase_40 AC 5 ms
7,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int sup = 1 << 30;

typedef struct vList {
	struct vList *next;
	int i, j;
} vlist;

typedef struct {
	int id, head, tail, flow, cap;
} edge;

typedef struct List {
	struct List *next;
	edge *e;
} list;

void chmin(int* a, int b)
{
	if (*a > b) *a = b;
}

int DFS(list *aux[], edge f[], int par[], int u)
{
	int i, w, c, argmin;
	for (; aux[u] != NULL; aux[u] = aux[u]->next) {
		if (aux[u]->e->flow == aux[u]->e->cap) continue;
		w = aux[u]->e->head;
		if (par[w] == -3) {
			for (i = aux[u]->e->id, c = sup; par[w] != -2; i = par[w]) {
				chmin(&c, f[i].cap - f[i].flow);
				w = f[i].tail;
				if (f[i].cap - f[i].flow == c) argmin = w;
			}
			for (i = aux[u]->e->id, w = aux[u]->e->head; par[w] != -2; i = par[w]) {
				f[i].flow += c;
				w = f[i].tail;
			}
			if (argmin != u) {
				par[u] = -1;
				return argmin;
			}
		} else if (par[w] == -1) {
			par[w] = aux[u]->e->id;
			w = DFS(aux, f, par, w);
			if (w > 0 && w != u) {
				par[u] = -1;
				return w;
			}
		}
	}
	return 0;
}

int Dinic(int N, int M, list* adj[], edge e[], int s, int t)
{
	int i, u, w, ans = 0, add, par[1001], dist[1001], q[1001], head, tail;
	static list *aux[1001], df[500001], *p;
	static edge f[500001];
	do {
		for (u = 1; u <= N; u++) {
			dist[u] = -1;
			aux[u] = NULL;
		}
		dist[s] = 0;
		q[0] = s;
		for (head = 0, tail = 1; head < tail; head++) {
			u = q[head];
			for (p = adj[u]; p != NULL; p = p->next) {
				if (p->e->flow == p->e->cap) continue;
				w = p->e->head;
				if (dist[w] == -1) {
					dist[w] = dist[u] + 1;
					q[tail++] = w;
				}
				if (dist[w] == dist[u] + 1) {
					i = p->e->id;
					f[i].id = i;
					f[i].tail = u;
					f[i].head = w;
					f[i].flow = 0;
					f[i].cap = p->e->cap - p->e->flow;
					df[i].e = &(f[i]);
					df[i].next = aux[u];
					aux[u] = &(df[i]);
				}
			}
		}
		for (u = 1; u <= N; u++) par[u] = -1;
		par[s] = -2;
		par[t] = -3;
		DFS(aux, f, par, s);

		for (i = 0, add = 0; i < M; i++) {
			if (e[i].flow == e[i].cap || dist[e[i].head] != dist[e[i].tail] + 1 || f[i].flow == 0) continue;
			if (f[i].tail == s) add += f[i].flow;
			if (e[i^1].flow > 0) {
				e[i^1].flow -= f[i].flow;
				e[i].cap -= f[i].flow;
			} else {
				e[i].flow += f[i].flow;
				e[i^1].cap += f[i].flow;
			}
		}
		ans += add;
	} while (add != 0);
	return ans;
}

int main()
{
	int i, j, H, W, A[501][501];
	scanf("%d %d", &H, &W);
	for (i = 1; i <= H; i++) for (j = 1; j <= W; j++) scanf("%d", &(A[i][j]));

	int k, u, w, N = H + W, s = N + 1, t = N + 2, count[500001] = {}, ans = 0;
	vlist *appear[500001] = {}, d[250001], *p;
	list *adj[1001], f[500001];
	edge e[500001];
	for (i = 1, k = 0; i <= H; i++) {
		for (j = 1; j <= W; j++) {
			if (A[i][j] == 0) continue;
			else count[A[i][j]]++;
			d[k].i = i;
			d[k].j = j;
			d[k].next = appear[A[i][j]];
			appear[A[i][j]] = &(d[k++]);
		}
	}
	for (i = 1; i <= 500000; i++) {
		if (count[i] <= 1) {
			if (count[i] == 1) ans++;
			continue;
		}
		for (u = 1; u <= N + 2; u++) adj[u] = NULL;
		for (p = appear[i], k = 0; p != NULL; p = p->next) {
			u = p->i;
			w = p->j + H;
			
			e[k].id = k;
			e[k].tail = u;
			e[k].head = w;
			e[k].flow = 0;
			e[k].cap = 1;
			f[k].e = &(e[k]);
			f[k].next = adj[u];
			adj[u] = &(f[k++]);
			e[k].id = k;
			e[k].tail = w;
			e[k].head = u;
			e[k].flow = 0;
			e[k].cap = 0;
			f[k].e = &(e[k]);
			f[k].next = adj[w];
			adj[w] = &(f[k++]);
		}
		for (w = 1, u = s; w <= H; w++) {
			e[k].id = k;
			e[k].tail = u;
			e[k].head = w;
			e[k].flow = 0;
			e[k].cap = 1;
			f[k].e = &(e[k]);
			f[k].next = adj[u];
			adj[u] = &(f[k++]);
			e[k].id = k;
			e[k].tail = w;
			e[k].head = u;
			e[k].flow = 0;
			e[k].cap = 0;
			f[k].e = &(e[k]);
			f[k].next = adj[w];
			adj[w] = &(f[k++]);
		}
		for (u = H + 1, w = t; u <= N; u++) {
			e[k].id = k;
			e[k].tail = u;
			e[k].head = w;
			e[k].flow = 0;
			e[k].cap = 1;
			f[k].e = &(e[k]);
			f[k].next = adj[u];
			adj[u] = &(f[k++]);
			e[k].id = k;
			e[k].tail = w;
			e[k].head = u;
			e[k].flow = 0;
			e[k].cap = 0;
			f[k].e = &(e[k]);
			f[k].next = adj[w];
			adj[w] = &(f[k++]);
		}
		ans += Dinic(N + 2, k, adj, e, s, t);
	}
	printf("%d\n", ans);
	fflush(stdout);
	return 0;
}
0