結果

問題 No.1744 Selfish Spies 1 (à la Princess' Perfectionism)
ユーザー 👑 ygussanyygussany
提出日時 2021-11-07 19:54:27
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 446 ms / 5,000 ms
コード長 3,183 bytes
コンパイル時間 954 ms
コンパイル使用メモリ 31,872 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2024-05-07 12:18:21
合計ジャッジ時間 4,131 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 7 ms
5,376 KB
testcase_18 AC 6 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 13 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 5 ms
5,376 KB
testcase_28 AC 120 ms
5,888 KB
testcase_29 AC 7 ms
5,376 KB
testcase_30 AC 8 ms
5,376 KB
testcase_31 AC 6 ms
5,376 KB
testcase_32 AC 6 ms
5,376 KB
testcase_33 AC 93 ms
6,016 KB
testcase_34 AC 88 ms
5,760 KB
testcase_35 AC 443 ms
5,888 KB
testcase_36 AC 446 ms
6,016 KB
testcase_37 AC 370 ms
6,016 KB
testcase_38 AC 201 ms
5,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

#define N_MAX 500
#define M_MAX 500
#define L_MAX 100000

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



int bipartite_matching_augmentation_naive(int N, char color[], edge* adj[], int mate[])
{
	static int i, u, w, par[N_MAX + M_MAX + 1], q[N_MAX + M_MAX + 1], head, tail;
	edge *p;
	for (u = 1, tail = 0; u <= N; u++) {
		if (color[u] == 0 && mate[u] == 0) {
			par[u] = u;
			q[tail++] = u;
		} else par[u] = 0;
	}
	for (head = 0; head < tail; head++) {
		u = q[head];
		if (color[u] == 0) {
			for (p = adj[u]; p != NULL; p = p->next) {
				w = p->v;
				if (par[w] == 0) {
					par[w] = u;
					if (mate[w] == 0) break;
					q[tail++] = w;
				}
			}
			if (p != NULL) break;
		} else {
			par[mate[u]] = u;
			q[tail++] = mate[u];
		}
	}
	if (head == tail) return 0;
	
	// Augmentation
	for (u = par[w]; u != w; w = par[u], u = par[w]) {
		mate[u] = w;
		mate[w] = u;
	}
	return 1;
}

int bipartite_matching(int N, char color[], edge* adj[], 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_naive(N, color, adj, mate);
		ans += dif;
	} while (dif != 0);
	return ans;
}



// 1. Relatively Naive solution (O((N + M) L) time)
void rel_naive1(int N, int M, int L, int s[], int t[], char ans[])
{
	static char color[N_MAX + M_MAX + 1];
	static int i, u, w, mate[N_MAX + M_MAX + 1];
	static edge *adj[N_MAX + M_MAX + 1], e[L_MAX * 2 + 1], *p;
	for (u = 1; u <= N + M; u++) {
		adj[u] = NULL;
		color[u] = (u > N)? 1: 0;
	}
	for (i = 0; i < L; i++) {
		u = s[i+1];
		w = t[i+1] + N;
		e[i*2].v = w;
		e[i*2].next = adj[u];
		adj[u] = &(e[i*2]);
		e[i*2+1].v = u;
		e[i*2+1].next = adj[w];
		adj[w] = &(e[i*2+1]);
	}
	
	static int tmp_mate[N_MAX + M_MAX + 1];
	int j = 0, x, mu = bipartite_matching(N + M, color, adj, mate);
	for (i = 0; i < L; i++) {
		u = s[i+1];
		w = t[i+1] + N;
		if (mate[u] != w) {
			ans[i+1] = 1;
			continue;
		}
		for (p = adj[u]; p != NULL; p = p->next) if (mate[p->v] == 0) break;
		if (p != NULL) {
			ans[i+1] = 1;
			continue;
		}
		for (p = adj[w]; p != NULL; p = p->next) if (mate[p->v] == 0) break;
		if (p != NULL) {
			ans[i+1] = 1;
			continue;
		}
		
		for (x = 1; x <= N + M; x++) tmp_mate[x] = mate[x];
		tmp_mate[u] = 0;
		tmp_mate[w] = 0;
		if (adj[u]->v == w) adj[u] = adj[u]->next;
		else {
		    for (p = adj[u]; p->next->v != w; p = p->next);
		    p->next = p->next->next;
		}
		if (adj[w]->v == u) adj[w] = adj[w]->next;
		else {
		    for (p = adj[w]; p->next->v != u; p = p->next);
		    p->next = p->next->next;
		}
		if (bipartite_matching_augmentation_naive(N + M, color, adj, tmp_mate) == 0) ans[i+1] = 0;
		else ans[i+1] = 1;
		e[i*2].next = adj[u];
		adj[u] = &(e[i*2]);
		e[i*2+1].next = adj[w];
		adj[w] = &(e[i*2+1]);
	}
}



int main()
{
	char ans[L_MAX + 1];
	int i, N, M, L, s[L_MAX + 1], t[L_MAX + 1];
	scanf("%d %d %d", &N, &M, &L);
	for (i = 1; i <= L; i++) scanf("%d %d", &(s[i]), &(t[i]));
	rel_naive1(N, M, L, s, t, ans);
	for (i = 1; i <= L; i++) {
		if (ans[i] == 0) printf("No\n");
		else printf("Yes\n");
	}
	fflush(stdout);
	return 0;
}
0