The Accomodation of Students---hdu2444(二分图,最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2444
题意:有n个学生,m个关系,但是如果a认识b,b认识c,但是a不一定认识c;
求能不能把这n个人分成两个房间,每个房间的人互相都不认识,这就是让我们判断是不是二分图了,可以用涂色法来进行判断;
如果是二分图则求出来最大匹配是多少;
可以把两个房间的人分成两个数组也可以不分让结果除以2就行了
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 210 int maps[N][N], vis[N], used[N], n, ans, p, q;
int color[N];///利用涂色法判断是否为二分图; bool Find(int u)
{
for(int i=; i<=n; i++)
{
int x = i;
if(!vis[x] && maps[u][x])
{
vis[x] = ;
if(!used[x] || Find(used[x]))
{
used[x] = u;
return true;
}
}
}
return false;
} int main()
{
int m;
int x, y, flag;
while(scanf("%d %d", &n, &m)!=EOF)
{
flag = ;
p = q = ;
memset(color, , sizeof(color));
memset(maps, , sizeof(maps));
memset(used, , sizeof(used));
for(int i=; i<=m; i++)
{
scanf("%d%d", &x, &y);
maps[x][y] = maps[y][x] = ;
if(color[x]== && color[y]==)
{
color[x] = ;
color[y] = -;
}
else if(color[x]!= && color[y]==)
color[y] = -color[x];
else if(color[x]== && color[y]!=)
color[x] = -color[y];
else if(color[x]==color[y] && color[x]!=)
flag=;
}
if(flag==)
{
printf("No\n");
continue;
}
int ans = ;
for(int i=; i<=n; i++)
{
memset(vis, , sizeof(vis));
if(Find(i))
ans++;
}
printf("%d\n", ans/);///因为最终会存入1-2和2-1,所以要除以二;
}
return ;
}
The Accomodation of Students---hdu2444(二分图,最大匹配)的更多相关文章
- HDU 2444 The Accomodation of Students(判断二分图+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 2444 The Accomodation of Students【二分图最大匹配问题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...
- (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...
- HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)
[题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...
- HDU-2444-The Accomodation of Students(二分图判定,最大匹配)
链接: https://vjudge.net/problem/HDU-2444#author=634579757 题意: There are a group of students. Some of ...
- hdu 2444 The Accomodation of Students (判断二分图,最大匹配)
The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 2444 The Accomodation of Students 判断二分图+二分匹配
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...
- HUD 2444 The Accomodation of Students (二分图染色+最大匹配)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 2010 using name ...
- HDU 2444:The Accomodation of Students(二分图判定+匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:给出边,判断这个是否是一个二分图,并求最大匹配. 思路:先染色法求出是否是一个二分图,然后再匈牙利求 ...
随机推荐
- opengl 模板测试 glStencilOp glStencilFunc
下面来设置蒙板缓存和蒙板测试. 首先我们启用蒙板测试,这样就可以修改蒙板缓存中的值. 下面我们来解释蒙板测试函数的含义: 当你使用glEnable(GL_STENCIL_TEST)启用蒙板测试之后,蒙 ...
- 浮点纹理 opengl
原文地址:http://wangqingyun84.blog.163.com/blog/static/790836172010323113604/ 因为 glsl部分 绑定fbo 看到要用浮点纹理,搜 ...
- c++ define的用法(转)
#define是C语言中提供的宏定义命令,其主要目的是为程序员在编程时提供一定的方便,并能在一定程度上提高程序的运行效率,但学生在学习时往往不能 理解该命令的本质,总是在此处产生一些困惑,在编程时误用 ...
- 编写Unity3D着色器的三种方式
不管你会不会写Unity3D的shader,估计你会知道,Unity3D编写shader有三种方式,这篇东西主要就是说一下这三种东西有什么区别,和大概是怎样用的. 先来列一下这三种方式: fixed ...
- OpenCV学习:Windows+VS2010+OpenCV配置
OpenCV下载: 百度云下载:https://pan.baidu.com/s/1mhAExdu (2.4.9版本) 下载完成后,双击运行exe,选择输出目录,我选择的是C:\OpenCV 配置环境变 ...
- 移动端web开发技巧 -- 转载
META相关 1. 添加到主屏后的标题(IOS)<meta name="apple-mobile-web-app-title" content="标题"& ...
- Python 解压缩Zip和Rar文件到指定目录
#__author__ = 'Joker'# -*- coding:utf-8 -*-import urllibimport osimport os.pathimport zipfilefrom zi ...
- MongoDB 用户角色
Read:允许用户读取指定数据库 readWrite:允许用户读写指定数据库 dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建.删除,查看统计或访问system.profile user ...
- fgetc read缓冲机制区别
read属于系统调用,它的缓存是基于内核的缓冲,是记在内核空间的. 而fgetc是标准函数, 是在用户空间I/O缓冲区的 比如用fgetc读一个字节,fgetc有可能从内核中预读1024个字节到I/O ...
- solr学习笔记-linux下配置solr(转)
本文地址: http://zhoujianghai.iteye.com/blog/1540176 首先介绍一下solr: Apache Solr (读音: SOLer) 是一个开源.高性能.采用Jav ...