結果
問題 | No.1479 Matrix Eraser |
ユーザー | ygussany |
提出日時 | 2021-06-26 17:06:32 |
言語 | C (gcc 12.3.0) |
結果 |
AC
|
実行時間 | 336 ms / 3,000 ms |
コード長 | 2,603 bytes |
コンパイル時間 | 195 ms |
コンパイル使用メモリ | 32,512 KB |
実行使用メモリ | 25,352 KB |
最終ジャッジ日時 | 2024-06-25 09:10:28 |
合計ジャッジ時間 | 7,477 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 5 ms
11,016 KB |
testcase_01 | AC | 4 ms
10,124 KB |
testcase_02 | AC | 4 ms
10,588 KB |
testcase_03 | AC | 4 ms
10,560 KB |
testcase_04 | AC | 4 ms
10,132 KB |
testcase_05 | AC | 4 ms
12,036 KB |
testcase_06 | AC | 4 ms
11,120 KB |
testcase_07 | AC | 12 ms
12,904 KB |
testcase_08 | AC | 21 ms
12,892 KB |
testcase_09 | AC | 66 ms
13,748 KB |
testcase_10 | AC | 225 ms
15,064 KB |
testcase_11 | AC | 88 ms
13,836 KB |
testcase_12 | AC | 13 ms
12,164 KB |
testcase_13 | AC | 19 ms
12,916 KB |
testcase_14 | AC | 14 ms
12,348 KB |
testcase_15 | AC | 5 ms
10,192 KB |
testcase_16 | AC | 15 ms
11,308 KB |
testcase_17 | AC | 332 ms
15,088 KB |
testcase_18 | AC | 332 ms
15,564 KB |
testcase_19 | AC | 332 ms
14,920 KB |
testcase_20 | AC | 333 ms
15,056 KB |
testcase_21 | AC | 331 ms
16,292 KB |
testcase_22 | AC | 336 ms
15,900 KB |
testcase_23 | AC | 331 ms
15,368 KB |
testcase_24 | AC | 332 ms
15,336 KB |
testcase_25 | AC | 333 ms
14,724 KB |
testcase_26 | AC | 328 ms
15,136 KB |
testcase_27 | AC | 112 ms
15,464 KB |
testcase_28 | AC | 109 ms
15,184 KB |
testcase_29 | AC | 116 ms
15,612 KB |
testcase_30 | AC | 115 ms
16,048 KB |
testcase_31 | AC | 115 ms
15,856 KB |
testcase_32 | AC | 54 ms
24,388 KB |
testcase_33 | AC | 56 ms
24,664 KB |
testcase_34 | AC | 53 ms
25,352 KB |
testcase_35 | AC | 56 ms
24,636 KB |
testcase_36 | AC | 51 ms
25,172 KB |
testcase_37 | AC | 24 ms
9,712 KB |
testcase_38 | AC | 39 ms
15,420 KB |
testcase_39 | AC | 35 ms
14,704 KB |
testcase_40 | AC | 3 ms
7,996 KB |
ソースコード
#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[500003]; 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[250003], *adj[1001], e[500003], *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 = 2; 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; }