The Accomodation of Students 二分图判定+匹配
来源:互联网 发布:java程序设计流程图 编辑:程序博客网 时间:2024/06/05 22:39
模板题,复习一下模板
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 205;int n, m, tot, head[maxn], vis[maxn], match[maxn];struct Edge { int to, next; } edges[maxn*maxn];void init() { tot = 0; memset(head, -1, sizeof(head)); }void addedge(int u, int v) { edges[++tot].to = v; edges[tot].next = head[u]; head[u] = tot;}bool dfs(int u) { for (int i = head[u]; ~i; i = edges[i].next) { int v = edges[i].to; if (vis[v]) { if (vis[v] == -vis[u]) continue; else return false; } else { vis[v] = -vis[u]; if (!dfs(v)) return false; } } return true;}bool solve(int u) { vis[u] = 1; for (int i = head[u]; ~i; i = edges[i].next) { int v = edges[i].to, w = match[v]; if (w < 0 || !vis[w] && solve(w)) { match[u] = v; match[v] = u; return true; } } return false;}int maxflow() { int ans = 0; memset(match, -1, sizeof(match)); for (int i = 1; i <= n; i++) { if (match[i] < 0) { memset(vis, 0, sizeof(vis)); if (solve(i)) ans++; } } return ans;}int main() { int u, v; while (~scanf("%d %d", &n, &m)) { init(); for (int i = 1; i <= m; i++) { scanf("%d %d", &u, &v); addedge(u, v); addedge(v, u); } memset(vis, 0, sizeof(vis)); bool ok = true; for (int i = 1; i <= n; i++) { if (vis[i]) continue; vis[i] = 1; if (!dfs(i)) { ok = false; break; } } if (!ok) printf("No\n"); else printf("%d\n", maxflow()); }}
阅读全文
1 0
- The Accomodation of Students 二分图判定+匹配
- HDU 2444 The Accomodation of Students(二分图判定 + 二分图匹配)
- hdu 2444 The Accomodation of Students 二分图判定+二分匹配个数
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
- HDU 2444 The Accomodation of Students (二分图判定、求最大匹配)
- hdu 2444The Accomodation of Students 二分图判定+找最大匹配
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
- hdu2444—The Accomodation of Students(二分图判定+最大匹配)
- HDU 2444 The Accomodation of Students (二分图判定+最大匹配
- The Accomodation of Students(HDU-2444)(二分图判定与最大匹配)
- HDU 2444 The Accomodation of Students——二分图判定+最大匹配
- hdu2444 二分匹配 The Accomodation of Students
- hdu The Accomodation of Students(二分匹配)
- The Accomodation of Students (二分匹配)
- hdu2444 The Accomodation of Students (二分图判断+最大匹配)
- hdu2444 The Accomodation of Students【二分图判断+最大匹配】
- C++中this指针的应用
- 一个常见的错误
- 常见攻击和解决方法
- Android简单集成Jiecaovideoplayer
- 当当分库分表(一)
- The Accomodation of Students 二分图判定+匹配
- IO操作-知识概要
- 论我
- pip
- java 读取txt中矩阵
- 再说装饰器模式
- 蓝桥杯算法训练 ALGO-1 区间k大数查询 Java
- 自定义EditText_password输入框
- Ubuntu-Java-SerialPort-RXTX