inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 200007 int bl[N]; vector<int> e[N];
booldfs(int u){ for (auto v : e[u]) { if (~bl[v]) { bl[v] = bl[u] ^ 1; if (!dfs(v)) returnfalse; } else { if (bl[u] == bl[v]) returnfalse; } } returntrue; }
inlinevoidadd(int u, int v){e[u].push_back(v); e[v].push_back(u);}
inlinevoidwork(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) {e[i].clear(); bl[i] = -1;} for (int i = 1; i <= m; ++i) { int u = rd(), v = rd(); add(u, v); add(v, u); } for (int i = 1; i <= n; ++i) if (~bl[i]) { bl[i] = 0; if (!dfs(i)) {puts("NO"); return;} } puts("YES"); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
booldfs(int u){ for (auto v : e[u]) if (!vis[v]) { vis[v] = 1; if (!match[v] || dfs(match[v])) { match[v] = u; returntrue; } } returnfalse; }
intmain(){ int n = rd(), m = rd(); m = rd(); for (int i = 1; i <= m; ++i) { int u = rd(), v = rd(); e[u].push_back(v); } int ans = 0; for (int i = 1; i <= n; ++i) { memset(vis, 0, sizeof(vis)); ans += dfs(i); } printf("%d\n", ans); return0; }
vector<int> e[N * 2]; bool vis[N][2]; int match[N][2];
booldfs(int u){ for (auto v : e[u]) { if (!vis[v][0]) { vis[v][0] = 1; if (!match[v][0] || dfs(match[v][0])) {match[v][0] = u; returntrue;} } if (!vis[v][1]) { vis[v][1] = 1; if (!match[v][1] || dfs(match[v][1])) {match[v][1] = u; returntrue;} } } returnfalse; }
intmain(){ int n = rd(); for (int i = 1; i <= 2 * n; ++i) { for (int j = 1; j <= 2; ++j) { int v = rd(); e[i].push_back(v); } } int ans = 0; for (int i = 1; i <= 2 * n; ++i) { memset(vis, 0, sizeof(vis)); ans += dfs(i); } printf("%d\n", ans); return0; }
booldfs(int u){ for (auto v : e[u]) if (!vis[v]) { vis[v] = 1; if (!match[v] || dfs(match[v])) { match[v] = u; returntrue; } } returnfalse; }
inlinevoidwork(){ memset(match, 0, sizeof(match)); int n = rd(); for (int i = 1; i <= n; ++i) e[i].clear(); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { int c = rd(); if (c) e[i].push_back(j); } } int ans = 0; for (int i = 1; i <= n; ++i) { memset(vis, 0, sizeof(vis)); ans += dfs(i); } if (ans == n) puts("Yes"); elseputs("No"); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
Post title:二分图
Post author:Eva.Q
Create time:2022-07-04 10:06:28
Post link:https://qyy/2022/07/04/Algorithm/bi-partite-graph-matching/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.