题目来源:HDU 2444 The Accomodation of Students

题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人间 最多能够有几组

思路:二分图判定+最大匹配

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 550;
int vis[maxn];
int y[maxn];
vector <int> G[maxn];
int n, m;
int color[maxn];
bool bipartite(int u)
{
for(int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
if(color[u] == color[v])
return false;
if(!color[v])
{
color[v] = 3 - color[u];
if(!bipartite(v))
return false;
}
}
return true;
}
bool dfs(int u)
{
for(int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
if(vis[v] || color[v] == 1)
continue;
vis[v] = true;
if(y[v] == -1 || dfs(y[v]))
{
y[v] = u;
return true;
}
}
return false;
}
int match()
{
int ans = 0;
memset(y, -1, sizeof(y));
for(int i = 1; i <= n; i++)
{
memset(vis, 0, sizeof(vis));
if(color[i] == 1 && dfs(i))
ans++;
}
return ans;
} int main()
{
//int T;
//scanf("%d", &T);
while(scanf("%d %d", &n, &m) != EOF)
{
for(int i = 0; i <= n; i++)
G[i].clear();
while(m--)
{
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
memset(color, 0, sizeof(color));
int flag = 0;
for(int i = 1; i <= n; i++)
if(!color[i])
{
color[i] = 1;
if(!bipartite(i))
{
puts("No");
flag = 1;
break;
}
}
if(flag)
continue;
printf("%d\n", match()); }
return 0;
}

最新文章

  1. MySQL 备份与恢复
  2. 微小,但是美好的改变 G2 2.2发布
  3. 神奇的expect
  4. 天猫浏览型应用的CDN静态化架构演变
  5. 推导大O阶方法
  6. Linux下crontab详解
  7. mybatis从dao传入多个参数到sqlmap时dao中要使用map或实例对象(如:user)作为参数传入, 否则报错找不到属性getter方法
  8. webuploader文件上传问题总结
  9. nyoj 517 最小公倍数 【java睑板】
  10. jquery学习笔记3 jq遍历
  11. 数据分析之---Python可视化工具
  12. C# 添加枚举中文资源
  13. Java基础12-工具类;变长参数;IO
  14. PowerDesigner15连接Oracle数据库并导出Oracle的表结构
  15. 学习虚拟机时Vbox提示硬件加速不可用时应该怎么办?
  16. Linux 驱动——Button驱动2
  17. JavaSE| String常用方法
  18. ADO.NET获取数据(DataSet)同时获取表的架构
  19. floodlight路由机制分析
  20. android--实现通过点击链接打开apk(应用图标在桌面消失)

热门文章

  1. NodeJS学习笔记 (1)资源压缩-zlib(ok)
  2. autosar
  3. nginx开启gzip压缩后导致apk包下载不能正常安装
  4. HTTP——学习笔记(6)https
  5. Pleasant sheep and big big wolf
  6. JAVA输出带BOM的UTF-8编码的文件
  7. 5.array
  8. BZOJ 3209 数位DP
  9. 再谈Ubuntu和CentOS安装好之后的联网问题(桥接和NAT、静态和动态ip)(博主推荐)
  10. tomcat:Could not publish to the server. java.lang.IndexOutOfBoundsException