The Accomodation of Students
来源:互联网 发布:淘宝王成勇培训靠谱吗 编辑:程序博客网 时间:2024/05/21 17:09
典型的二分图最大匹配题
我们要先判断这个图是不是二分图,可以用染色法来实现
#include <iostream>#include <stdlib.h>#include <stdio.h>#include <string.h>using namespace std;#define MAXN 222bool gra[MAXN][MAXN], vis[MAXN];int match[MAXN], col[MAXN];int ans, n;bool check(int cur, int color){ col[cur] = color; for( int i = 1; i <= n; i++) { if(gra[cur][i]) { if(col[i] != -1) { if(col[i] == color) return false; } else if(!check(i, !color)) return 0; } } return 1;}bool dfs( int x){ for( int i = 1; i <= n; i++) { if( i != x && gra[x][i] && !vis[i]) { vis[i] = 1; if(!match[i] || dfs(match[i])) { match[i] = x; return 1; } } } return 0;}void solve( ){ ans = 0; for(int i = 1; i <= n; i++) { memset(vis, 0, sizeof(vis)); if(dfs(i)) ans++; } printf("%d\n",ans/2);}int main(){ int u, v, m; while(scanf("%d %d",&n, &m) != EOF) { memset(gra, 0, sizeof(gra)); memset(match, 0, sizeof(match)); memset(col, -1, sizeof(col)); for(int i = 0; i < m; i++) { scanf("%d %d",&u, &v); gra[u][v] = gra[v][u] = 1; } if(!check(1,1)) printf("No\n"); else solve(); } return 0;}
0 0
- The Accomodation of Students
- The Accomodation of Students
- The Accomodation of Students
- The Accomodation of Students
- The Accomodation of Students
- The Accomodation of Students
- HDU2444--The Accomodation of Students
- HDU2444 The Accomodation of Students
- hdu2444 The Accomodation of Students
- hdu2444 The Accomodation of Students
- 【HDU2444】The Accomodation of Students
- The Accomodation of Students---hdu2444
- HDU2444-The Accomodation of Students
- HDU2444 The Accomodation of Students
- The Accomodation of Students HDU
- The Accomodation of Students HDU
- The Accomodation of Students HDU
- hdu 2444 The Accomodation of Students
- cocos2dx 3.X 在windows下的环境搭建并建立第一个HelloWorld工程
- zoj 1760 Doubles
- Firebreath2——Firebreath中用到的技术
- 关于伸缩盒模型 Flexible Box 的布局
- SpringMVC工作原理
- The Accomodation of Students
- Phone开发之深入浅出 (1) — ARC是什么
- 继承时构造函数、析构函数的顺序研究
- 如何用CFileDialog得到一个文件夹的路径?
- poj 1787 Charlie's Change
- java 网络通讯工具类
- Referenced file contains errors (http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd#).
- RFID第三周作业
- 模式识别:分类器的性能评价