結果

問題 No.1479 Matrix Eraser
ユーザー ygussanyygussany
提出日時 2021-03-21 10:32:32
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,603 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 32,384 KB
実行使用メモリ 25,948 KB
最終ジャッジ日時 2024-11-22 01:33:35
合計ジャッジ時間 7,273 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,988 KB
testcase_01 AC 4 ms
10,164 KB
testcase_02 AC 4 ms
12,172 KB
testcase_03 AC 3 ms
11,064 KB
testcase_04 AC 3 ms
10,336 KB
testcase_05 AC 3 ms
10,124 KB
testcase_06 AC 3 ms
10,124 KB
testcase_07 AC 10 ms
11,432 KB
testcase_08 AC 19 ms
12,944 KB
testcase_09 AC 57 ms
14,612 KB
testcase_10 AC 198 ms
15,272 KB
testcase_11 AC 77 ms
14,764 KB
testcase_12 AC 12 ms
12,328 KB
testcase_13 AC 16 ms
12,492 KB
testcase_14 AC 12 ms
11,192 KB
testcase_15 AC 5 ms
11,168 KB
testcase_16 AC 14 ms
12,536 KB
testcase_17 AC 291 ms
14,740 KB
testcase_18 AC 291 ms
14,948 KB
testcase_19 AC 291 ms
16,472 KB
testcase_20 AC 291 ms
16,340 KB
testcase_21 AC 290 ms
14,688 KB
testcase_22 AC 292 ms
14,760 KB
testcase_23 AC 293 ms
15,716 KB
testcase_24 AC 292 ms
15,096 KB
testcase_25 AC 289 ms
15,056 KB
testcase_26 AC 290 ms
16,148 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 47 ms
24,356 KB
testcase_33 AC 50 ms
25,948 KB
testcase_34 AC 48 ms
25,836 KB
testcase_35 AC 51 ms
24,512 KB
testcase_36 AC 49 ms
25,596 KB
testcase_37 AC 24 ms
8,904 KB
testcase_38 AC 38 ms
14,824 KB
testcase_39 AC 34 ms
15,736 KB
testcase_40 AC 3 ms
8,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct List {
	struct List *next;
	int v, flag, id;
} list;

int DFS(list *adj[], int par[], int u)
{
	int w;
	for (; adj[u] != NULL; adj[u] = adj[u]->next) {
		w = adj[u]->v;
		if (par[w] == -2) {
			par[w] = adj[u]->id;
			return w;
		} else if (par[w] >= 0) continue;
		par[w] = adj[u]->id;
		w = DFS(adj, par, w);
		if (w > 0) return w;
	}
	return 0;
}

int bipartite_matching(int N1, int N2, list *adj[], list e[], int mu[])
{
	int i, j, u, w, ans = 0, par[1001], q[1001], head, tail;
	list *aux[1001] = {}, *p;
	static list f[500001];
	while (1) {
		for (u = 1; u <= N1; u++) par[u] = -1;
		for (w = N1 + 1; w <= N1 + N2; w++) par[w] = (mu[w] == 0)? -2: -1;
		for (u = 1, i = 0; u <= N1 + N2; u++) {
			for (p = adj[u], aux[u] = NULL; p != NULL; p = p->next) {
				if (p->flag == 0) continue;
				w = p->v;
				f[i].v = w;
				f[i].id = p->id;
				f[i].next = aux[u];
				aux[u] = &(f[i++]);
			}
		}
		
		for (u = 1, tail = 0; u <= N1; u++) {
			if (mu[u] > 0) continue;
			else par[u] = 0;
			w = DFS(aux, par, u);
			if (w > 0) q[tail++] = w;
		}
		if (tail == 0) break;
		else ans += tail;
		
		for (head = 0; head < tail; head++) {
			w = q[head];
			i = par[w] * 2;
			u = e[i+1].v;
			for (i = par[w] * 2, u = e[i+1].v; par[u] != 0; w = e[par[u]*2].v, i = par[w] * 2, u = e[i+1].v) {
				j = par[u] * 2;
				mu[u] = w;
				mu[w] = u;
				e[i].flag = 0;
				e[i+1].flag = 1;
				e[j].flag = 1;
				e[j+1].flag = 0;
			}
			mu[u] = w;
			mu[w] = u;
			e[i].flag = 0;
			e[i+1].flag = 1;
		}
	}
	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, count[500001] = {}, ans = 0, mu[1001];
	list *appear[500001] = {}, d[250001], *adj[1001], e[500001], *p;
	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].v = i;
			d[k].id = 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; u++) {
			adj[u] = NULL;
			mu[u] = 0;
		}
		for (p = appear[i], k = 0; p != NULL; p = p->next) {
			u = p->v;
			w = p->id + H;
			e[k].v = w;
			e[k].flag = 1;
			e[k].id = k / 2;
			e[k].next = adj[u];
			adj[u] = &(e[k++]);
			e[k].v = u;
			e[k].flag = 0;
			e[k].id = k / 2;
			e[k].next = adj[w];
			adj[w] = &(e[k++]);
		}
		ans += bipartite_matching(H, W, adj, e, mu);
	}
	printf("%d\n", ans);
	fflush(stdout);
	return 0;
}
0