結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,808 KB
testcase_01 AC 5 ms
7,808 KB
testcase_02 AC 4 ms
7,808 KB
testcase_03 AC 4 ms
7,808 KB
testcase_04 AC 4 ms
7,808 KB
testcase_05 AC 4 ms
7,808 KB
testcase_06 AC 4 ms
7,808 KB
testcase_07 AC 12 ms
9,216 KB
testcase_08 AC 21 ms
9,728 KB
testcase_09 AC 65 ms
11,008 KB
testcase_10 AC 222 ms
13,440 KB
testcase_11 AC 85 ms
11,392 KB
testcase_12 AC 13 ms
8,832 KB
testcase_13 AC 19 ms
9,344 KB
testcase_14 AC 13 ms
8,832 KB
testcase_15 AC 5 ms
8,064 KB
testcase_16 AC 16 ms
9,216 KB
testcase_17 AC 326 ms
14,464 KB
testcase_18 AC 325 ms
14,592 KB
testcase_19 AC 324 ms
14,592 KB
testcase_20 AC 322 ms
14,592 KB
testcase_21 AC 325 ms
14,592 KB
testcase_22 AC 327 ms
14,592 KB
testcase_23 AC 324 ms
14,592 KB
testcase_24 AC 325 ms
14,464 KB
testcase_25 AC 324 ms
14,592 KB
testcase_26 AC 324 ms
14,592 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 49 ms
23,424 KB
testcase_33 AC 54 ms
23,424 KB
testcase_34 AC 48 ms
23,424 KB
testcase_35 AC 61 ms
23,424 KB
testcase_36 AC 50 ms
23,296 KB
testcase_37 AC 24 ms
8,704 KB
testcase_38 AC 38 ms
14,592 KB
testcase_39 AC 34 ms
14,592 KB
testcase_40 AC 4 ms
7,680 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[1000001];
	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[1000001], *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