結果

問題 No.1745 Selfish Spies 2 (à la Princess' Perfectionism)
ユーザー ygussanyygussany
提出日時 2021-11-07 20:32:29
言語 C
(gcc 12.3.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 5,056 bytes
コンパイル時間 927 ms
コンパイル使用メモリ 34,324 KB
実行使用メモリ 25,980 KB
最終ジャッジ日時 2024-05-07 12:18:44
合計ジャッジ時間 11,046 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
14,212 KB
testcase_01 AC 3 ms
14,088 KB
testcase_02 AC 4 ms
14,088 KB
testcase_03 AC 3 ms
12,044 KB
testcase_04 AC 3 ms
14,088 KB
testcase_05 AC 3 ms
9,996 KB
testcase_06 AC 4 ms
12,172 KB
testcase_07 AC 3 ms
12,168 KB
testcase_08 AC 3 ms
10,004 KB
testcase_09 AC 2 ms
12,048 KB
testcase_10 AC 4 ms
14,088 KB
testcase_11 AC 3 ms
14,088 KB
testcase_12 AC 4 ms
16,008 KB
testcase_13 AC 3 ms
12,176 KB
testcase_14 AC 3 ms
14,216 KB
testcase_15 AC 3 ms
16,132 KB
testcase_16 AC 4 ms
14,092 KB
testcase_17 AC 4 ms
14,112 KB
testcase_18 AC 5 ms
12,212 KB
testcase_19 AC 3 ms
14,216 KB
testcase_20 AC 3 ms
16,260 KB
testcase_21 AC 3 ms
14,088 KB
testcase_22 AC 4 ms
16,260 KB
testcase_23 AC 2 ms
12,048 KB
testcase_24 AC 10 ms
14,496 KB
testcase_25 AC 4 ms
12,196 KB
testcase_26 AC 3 ms
12,188 KB
testcase_27 AC 6 ms
14,136 KB
testcase_28 AC 69 ms
19,084 KB
testcase_29 AC 6 ms
14,256 KB
testcase_30 AC 6 ms
12,188 KB
testcase_31 AC 6 ms
14,132 KB
testcase_32 AC 6 ms
14,252 KB
testcase_33 AC 71 ms
19,216 KB
testcase_34 AC 62 ms
17,224 KB
testcase_35 AC 135 ms
17,164 KB
testcase_36 AC 145 ms
17,520 KB
testcase_37 AC 139 ms
17,524 KB
testcase_38 AC 126 ms
21,904 KB
testcase_39 AC 93 ms
14,280 KB
testcase_40 AC 77 ms
15,844 KB
testcase_41 AC 76 ms
18,576 KB
testcase_42 AC 292 ms
21,132 KB
testcase_43 AC 386 ms
25,520 KB
testcase_44 AC 594 ms
25,980 KB
testcase_45 TLE -
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;
} 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;
}



void chmin(int* a, int b)
{
	if (*a > b) *a = b;
}

int DFS_SCC(edge* adj[], int label[], int ord[], int low[], int s[], int* head, int u)
{
	s[(*head)++] = u; // Add u to the stack (which maintains the vertices already found but not determined)
	ord[u] = ord[0]++;
	low[u] = ord[u];
	
	int w;
	edge *p;
	for (p = adj[u]; p != NULL; p = p->next) {
		w = p->v;
		if (ord[w] == 0) chmin(&(low[u]), DFS_SCC(adj, label, ord, low, s, head, w)); // w has been found
		else if (ord[w] <= N_MAX + M_MAX) chmin(&(low[u]), ord[w]); // w is already found but not determined
	}
	
	if (low[u] == ord[u]) { // A new SCC containing u has been determined
		while (s[--(*head)] != u) {
			label[s[*head]] = label[0];
			ord[s[*head]] = N_MAX + M_MAX + 1;
		}
		label[u] = label[0]++;
		ord[u] = N_MAX + M_MAX + 1;
	}
	return low[u];
}

int SCC(int N, edge* adj[], int label[])
{
	int u, w, head;
	static int ord[N_MAX + M_MAX + 1], low[N_MAX + M_MAX + 1], s[N_MAX + M_MAX + 1];
	for (u = 1; u <= N; u++) {
		label[u] = 0;
		ord[u] = 0;
	}
	for (u = 1, label[0] = 1, ord[0] = 1; u <= N; u++) {
		if (ord[u] != 0) continue;
		head = 0;
		DFS_SCC(adj, label, ord, low, s, &head, u);
	}
	return label[0] - 1;
}



// 2. Solution example (O(sqrt{N + M} L) time)
void 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].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]);
	}
	bipartite_matching(N + M, color, adj, mate);
	
	static int par[N_MAX + M_MAX + 1], q[N_MAX + M_MAX + 1], head, tail;
	for (u = 1; u <= N + M; u++) par[u] = 0;
	for (u = 1, tail = 0; u <= N; u++) {
		if (mate[u] == 0) {
			par[u] = u;
			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 (par[w] == 0) {
					par[w] = u;
					q[tail++] = w;
				}
			}
		} else {
			par[mate[u]] = u;
			q[tail++] = mate[u];
		}
	}
	for (u = N + 1, tail = 0; u <= N + M; u++) {
		if (mate[u] == 0) {
			par[u] = -u;
			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 (par[w] == 0) {
					par[w] = -u;
					q[tail++] = w;
				}
			}
		} else {
			par[mate[u]] = -u;
			q[tail++] = mate[u];
		}
	}
	
	int m = 0;
	static int label[N_MAX + M_MAX + 1];
	static edge *aux[N_MAX + M_MAX + 1], f[L_MAX + N_MAX + 1];
	for (u = 1; u <= N; u++) {
		aux[u] = NULL;
		if (par[u] != 0) continue;
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (par[w] != 0) continue;
			f[m].v = w;
			f[m].next = aux[u];
			aux[u] = &(f[m++]);
		}
	}
	for (u = N + 1; u <= N + M; u++) {
		aux[u] = NULL;
		if (par[u] != 0) continue;
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (par[w] == 0 && w == mate[u]) {
				f[m].v = w;
				f[m].next = aux[u];
				aux[u] = &(f[m++]);
			}
		}
	}
	SCC(N + M, aux, label);
	
	for (i = 1; i <= L; i++) {
		u = s[i];
		w = t[i] + N;
		if (par[u] > 0 && par[w] > 0) ans[i] = 1;
		else if (par[u] < 0 && par[w] < 0) ans[i] = 1;
		else if (label[u] == label[w]) ans[i] = 1;
		else ans[i] = 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]));
	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