結果

問題 No.1745 Selfish Spies 2 (à la Princess' Perfectionism)
ユーザー ygussanyygussany
提出日時 2021-11-11 00:08:39
言語 C
(gcc 12.3.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 6,177 bytes
コンパイル時間 427 ms
コンパイル使用メモリ 34,756 KB
実行使用メモリ 13,440 KB
最終ジャッジ日時 2024-05-07 12:20:02
合計ジャッジ時間 17,902 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 0 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 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 23 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 544 ms
7,552 KB
testcase_29 AC 14 ms
5,376 KB
testcase_30 AC 20 ms
5,376 KB
testcase_31 AC 14 ms
5,376 KB
testcase_32 AC 16 ms
5,376 KB
testcase_33 AC 1,017 ms
7,296 KB
testcase_34 AC 1,096 ms
7,552 KB
testcase_35 AC 407 ms
7,424 KB
testcase_36 AC 625 ms
7,424 KB
testcase_37 AC 924 ms
7,552 KB
testcase_38 AC 1,174 ms
7,552 KB
testcase_39 AC 10 ms
5,376 KB
testcase_40 AC 3,448 ms
5,376 KB
testcase_41 AC 19 ms
5,376 KB
testcase_42 TLE -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define N_MAX 100000
#define M_MAX 100000
#define L_MAX 200000

typedef struct Edge {
	struct Edge *next;
	int v, id;
} 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[], int mate[])
{
	static int i, u, w, depth[N_MAX + M_MAX + 1], par[N_MAX + M_MAX + 1], q[N_MAX + M_MAX + 1], head, tail;
	static edge *aux[N_MAX + M_MAX + 1], f[L_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[], 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, mate);
		ans += dif;
	} while (dif != 0);
	return ans;
}



// 2. Relatively correct solution (O(L min{N, M}) time)
void rel_solve2(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].id = i + 1;
		e[i*2].next = adj[u];
		adj[u] = &(e[i*2]);
		e[i*2+1].v = u;
		e[i*2+1].id = i + 1;
		e[i*2+1].next = adj[w];
		adj[w] = &(e[i*2+1]);
	}
	
	static char flag[N_MAX + M_MAX + 1];
	static int q[N_MAX + M_MAX + 1], head, tail;
	int mu = bipartite_matching(N + M, color, adj, mate);
	for (u = 1; u <= N + M; u++) flag[u] = 0;

	// Reachability from unmatched spies
	for (u = 1, tail = 0; u <= N; u++) {
		if (mate[u] == 0) {
			flag[u] = 1;
			q[tail++] = u;
		}
	}
	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 (flag[w] == 0) {
					flag[w] = 1;
					q[tail++] = w;
				}
			}
		} else if (mate[u] != 0) {
			flag[mate[u]] = 1;
			q[tail++] = mate[u];
		}
	}

	// Reachability from unmatched tasks
	for (u = N + 1, tail = 0; u <= N + M; u++) {
		if (mate[u] == 0) {
			flag[u] = -1;
			q[tail++] = u;
		}
	}
	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 (flag[w] == 0) {
					flag[w] = -1;
					q[tail++] = w;
				}
			}
		} else if (mate[u] != 0) {
			flag[mate[u]] = -1;
			q[tail++] = mate[u];
		}
	}
		
	if (N <= M) {
		// Alternating cycle
		for (u = 1; u <= N; u++) {
			if (flag[u] == -1) {
				for (p = adj[u]; p != NULL; p = p->next) ans[p->id] = (flag[p->v] == -1)? 1: 0;
				continue;
			} else if (flag[u] == 1) {
				for (p = adj[u]; p != NULL; p = p->next) ans[p->id] = 1;
				continue;
			}
			
			flag[u] = 2;
			q[0] = u;
			for (head = 0, tail = 1; head < tail; head++) {
				w = q[head];
				if (color[w] == 0) {
					if (mate[w] != 0) {
						flag[mate[w]] = 2;
						q[tail++] = mate[w];
					}
				} else {
					for (p = adj[w]; p != NULL; p = p->next) {
						if (flag[p->v] == 0) {
							flag[p->v] = 2;
							q[tail++] = p->v;
						}
					}
				}
			}
			for (p = adj[u]; p != NULL; p = p->next) ans[p->id] = (flag[p->v] == 2)? 1: 0;
			for (head = 0; head < tail; head++) flag[q[head]] = 0;
		}
	} else {
		// Alternating cycle
		for (u = N + 1; u <= N + M; u++) {
			if (flag[u] == 1) {
				for (p = adj[u]; p != NULL; p = p->next) ans[p->id] = (flag[p->v] == 1)? 1: 0;
				continue;
			} else if (flag[u] == -1) {
				for (p = adj[u]; p != NULL; p = p->next) ans[p->id] = 1;
				continue;
			}
			
			flag[u] = 2;
			q[0] = u;
			for (head = 0, tail = 1; head < tail; head++) {
				w = q[head];
				if (color[w] != 0) {
					if (mate[w] != 0) {
						flag[mate[w]] = 2;
						q[tail++] = mate[w];
					}
				} else {
					for (p = adj[w]; p != NULL; p = p->next) {
						if (flag[p->v] == 0) {
							flag[p->v] = 2;
							q[tail++] = p->v;
						}
					}
				}
			}
			for (p = adj[u]; p != NULL; p = p->next) ans[p->id] = (flag[p->v] == 2)? 1: 0;
			for (head = 0; head < tail; head++) flag[q[head]] = 0;
		}
	}
}



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_solve2(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