提出 #38099631


ソースコード 拡げる

#include <stdio.h>

#define N_MAX 180000
#define M_MAX 540000

typedef struct Edge {
	struct Edge *next;
	int v;
} edge;

int DFS_bipartite_matching(edge* aux[], int par[], int u)
{
	int w;
	for (; aux[u] != NULL; aux[u] = aux[u]->next) {
		w = aux[u]->v;
		if (par[w] == 0) { // w is a sink
			par[w] = u;
			return w;
		} else if (par[w] > 0) continue; // w is already checked
		par[w] = u;
		w = DFS_bipartite_matching(aux, par, w);
		if (w > 0) return w;
	}
	return 0;
}

int bipartite_matching_augmentation(int N, char color[], edge* adj[], edge e[], int mate[])
{
	static int i, u, w, depth[N_MAX + 1], par[N_MAX + 1], q[N_MAX + 1], head, tail;
	static edge *aux[N_MAX + 1], f[M_MAX * 2], *p;
	for (u = 1, tail = 0, par[0] = 0; u <= N; u++) {
		if (mate[u] == 0) { // u is a source of sink
			if (color[u] == 0) { // u is a source
				depth[u] = 0;
				q[tail++] = u;
			} else depth[u] = N;
			par[u] = 0;
		} else {
			depth[u] = N;
			par[u] = -1;
		}
	}
	
	// BFS for constructing the layered network
	for (head = 0, i = 0; head < tail; head++) {
		u = q[head];
		aux[u] = NULL;
		if (color[u] == 0) {
			for (p = adj[u]; p != NULL; p = p->next) {
				w = p->v;
				if (mate[u] == w) continue; // No arc in this direction
				if (depth[w] == N) { // w has been found for the first time
					depth[w] = depth[u] + 1;
					q[tail++] = w;
				}
				if (depth[w] == depth[u] + 1) { // Add the arc uw
					f[i].v = w;
					f[i].next = aux[u];
					aux[u] = &(f[i++]);
				}
			}
		} else if (mate[u] != 0) {
			w = mate[u];
			if (depth[w] == N) { // w has been found for the first time
				depth[w] = depth[u] + 1;
				q[tail++] = w;
			}
			if (depth[w] == depth[u] + 1) { // Add the arc uw
				f[i].v = w;
				f[i].next = aux[u];
				aux[u] = &(f[i++]);
			}
		}
	}

	// DFS for finding disjoint augmenting paths
	for (u = 1, tail = 0; u <= N; u++) {
		if (depth[u] != 0) continue;
		w = DFS_bipartite_matching(aux, par, u);
		if (w > 0) q[tail++] = w; // An augmenting path from u to w has been found
	}
	
	// Augmentation
	for (head = 0; head < tail; head++) {
		for (w = q[head], u = par[w]; u > 0; w = par[u], u = par[w]) {
			mate[u] = w;
			mate[w] = u;
		}
	}
	return tail;
}

int bipartite_matching(int N, char color[], edge* adj[], edge e[], int mate[])
{
	int i, u, dif, ans = 0;
	edge *p;
	for (u = 1; u <= N; u++) mate[u] = 0; // Initialization
	do { // Augmentation
		dif = bipartite_matching_augmentation(N, color, adj, e, mate);
		ans += dif;
	} while (dif != 0);
	return ans;
}



int main()
{
	int i, H, W;
	char c[302][302];
	scanf("%d %d", &H, &W);
	for (i = 1; i <= H; i++) scanf("%s", &(c[i][1]));
	
	int j, n = 0, v[302][302] = {}, u, w;
	char color[N_MAX + 1];
	for (i = 1; i <= H; i++) {
		for (j = 1; j <= W; j++) {
			if (c[i][j] == '1') continue;
			v[i][j] = ++n;
			color[n] = (i + j) % 2;
		}
	}
	
	int m = 0;
	edge *adj[N_MAX * 2 + 1] = {}, e[M_MAX * 2];
	for (i = 1; i <= H; i++) {
		for (j = 1; j <= W; j++) {
			if (v[i][j] == 0) continue;
			u = v[i][j];
			color[u+n] = color[u] ^ 1;
			
			if (c[i][j] == '?') {
				w = u + n;
				e[m].v = w;
				e[m].next = adj[u];
				adj[u] = &(e[m++]);
				e[m].v = u;
				e[m].next = adj[w];
				adj[w] = &(e[m++]);
			}
			
			if (v[i][j+1] != 0) {
				w = v[i][j+1];
				e[m].v = w;
				e[m].next = adj[u];
				adj[u] = &(e[m++]);
				e[m].v = u;
				e[m].next = adj[w];
				adj[w] = &(e[m++]);
				e[m].v = w + n;
				e[m].next = adj[u+n];
				adj[u+n] = &(e[m++]);
				e[m].v = u + n;
				e[m].next = adj[w+n];
				adj[w+n] = &(e[m++]);
			}
			
			if (v[i+1][j] != 0) {
				w = v[i+1][j];
				e[m].v = w;
				e[m].next = adj[u];
				adj[u] = &(e[m++]);
				e[m].v = u;
				e[m].next = adj[w];
				adj[w] = &(e[m++]);
				e[m].v = w + n;
				e[m].next = adj[u+n];
				adj[u+n] = &(e[m++]);
				e[m].v = u + n;
				e[m].next = adj[w+n];
				adj[w+n] = &(e[m++]);
			}
		}
	}
	
	int mate[N_MAX + 1];
	if (bipartite_matching(n * 2, color, adj, e, mate) == n) printf("Yes\n");
	else printf("No\n");
	fflush(stdout);
	return 0;
}

提出情報

提出日時
問題 G - Tatami
ユーザ ygussany
言語 C (GCC 9.2.1)
得点 600
コード長 4192 Byte
結果 AC
実行時間 86 ms
メモリ 19624 KiB

コンパイルエラー

./Main.c: In function ‘main’:
./Main.c:111:2: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
  111 |  scanf("%d %d", &H, &W);
      |  ^~~~~~~~~~~~~~~~~~~~~~
./Main.c:112:27: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
  112 |  for (i = 1; i <= H; i++) scanf("%s", &(c[i][1]));
      |                           ^~~~~~~~~~~~~~~~~~~~~~~

ジャッジ結果

セット名 Sample All AfterContest
得点 / 配点 0 / 0 600 / 600 0 / 0
結果
AC × 3
AC × 42
AC × 1
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, sample_01.txt, sample_02.txt, sample_03.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt
AfterContest after_contest_01.txt
ケース名 結果 実行時間 メモリ
after_contest_01.txt AC 8 ms 4796 KiB
hand_01.txt AC 4 ms 4808 KiB
hand_02.txt AC 5 ms 4704 KiB
hand_03.txt AC 4 ms 4768 KiB
hand_04.txt AC 4 ms 4828 KiB
hand_05.txt AC 4 ms 4840 KiB
hand_06.txt AC 3 ms 4784 KiB
hand_07.txt AC 4 ms 4804 KiB
random_01.txt AC 86 ms 19216 KiB
random_02.txt AC 53 ms 15444 KiB
random_03.txt AC 10 ms 6356 KiB
random_04.txt AC 46 ms 14172 KiB
random_05.txt AC 73 ms 19180 KiB
random_06.txt AC 53 ms 15700 KiB
random_07.txt AC 13 ms 7480 KiB
random_08.txt AC 15 ms 7632 KiB
random_09.txt AC 59 ms 19400 KiB
random_10.txt AC 13 ms 7464 KiB
random_11.txt AC 17 ms 8440 KiB
random_12.txt AC 13 ms 6816 KiB
random_13.txt AC 58 ms 19412 KiB
random_14.txt AC 18 ms 8972 KiB
random_15.txt AC 20 ms 10512 KiB
random_16.txt AC 18 ms 9192 KiB
random_17.txt AC 55 ms 19624 KiB
random_18.txt AC 10 ms 6532 KiB
random_19.txt AC 50 ms 19080 KiB
random_20.txt AC 10 ms 6964 KiB
random_21.txt AC 51 ms 19616 KiB
random_22.txt AC 23 ms 11432 KiB
random_23.txt AC 22 ms 8704 KiB
random_24.txt AC 9 ms 5668 KiB
random_25.txt AC 21 ms 9132 KiB
random_26.txt AC 12 ms 6720 KiB
random_27.txt AC 40 ms 15712 KiB
random_28.txt AC 7 ms 5424 KiB
random_29.txt AC 8 ms 6304 KiB
random_30.txt AC 15 ms 6848 KiB
random_31.txt AC 7 ms 5956 KiB
random_32.txt AC 21 ms 12836 KiB
sample_01.txt AC 3 ms 4700 KiB
sample_02.txt AC 3 ms 4780 KiB
sample_03.txt AC 4 ms 4704 KiB