結果
問題 | No.1744 Selfish Spies 1 (à la Princess' Perfectionism) |
ユーザー |
👑 |
提出日時 | 2021-11-07 19:52:12 |
言語 | C (gcc 13.3.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 4,025 bytes |
コンパイル時間 | 250 ms |
コンパイル使用メモリ | 32,512 KB |
実行使用メモリ | 15,348 KB |
最終ジャッジ日時 | 2024-11-30 05:01:39 |
合計ジャッジ時間 | 67,251 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 31 TLE * 8 |
ソースコード
#include <stdio.h>#include <stdlib.h>#define N_MAX 500#define M_MAX 500#define L_MAX 100000typedef 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 sinkpar[w] = u;return w;} else if (par[w] > 0) continue; // w is already checkedpar[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 sinkif (color[u] == 0) { // u is a sourcedepth[u] = 0;q[tail++] = u;} else depth[u] = N;par[u] = 0;} else {depth[u] = N;par[u] = -1;}}// BFS for constructing the layered networkfor (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 directionif (depth[w] == N) { // w has been found for the first timedepth[w] = depth[u] + 1;q[tail++] = w;}if (depth[w] == depth[u] + 1) { // Add the arc uwf[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 timedepth[w] = depth[u] + 1;q[tail++] = w;}if (depth[w] == depth[u] + 1) { // Add the arc uwf[i].v = w;f[i].next = aux[u];aux[u] = &(f[i++]);}}}// DFS for finding disjoint augmenting pathsfor (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}// Augmentationfor (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; // Initializationdo { // Augmentationdif = bipartite_matching_augmentation(N, color, adj, mate);ans += dif;} while (dif != 0);return ans;}// 1. Naive solution (O(sqrt{N + M} L^2) time)void 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]);}int j, mu = bipartite_matching(N + M, color, adj, mate);for (j = 0; j < L; j++) {for (u = 1; u <= N + M; u++) adj[u] = NULL;for (i = 0; i < L; i++) {if (i == j) continue;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]);}if (mu == bipartite_matching(N + M, color, adj, mate)) ans[j+1] = 1;else ans[j+1] = 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]));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;}