题目链接:https://vjudge.net/problem/HDU-2444

The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7328    Accepted Submission(s): 3270

Problem Description
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.

Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.

Calculate the maximum number of pairs that can be arranged into these double rooms.

 
Input
For each data set:
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.

Proceed to the end of file.

 
Output
If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
 
Sample Input
4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
 
Sample Output
No
3
 
Source
 
Recommend
gaojie

题解:

任务1:能否把这些人分成两组,且在每一组内,所有人互不相识(即两点间没有边直接相连)?

任务2:求最大匹配数, 直接用hungary()算法。

1.由于要把所有点分成两组,所以我们可以用两种颜色,对整幅图进行染色。规定:相邻两点间的颜色不同,然后把颜色相同的归为一组。

2.对于没有被染色的点u,对其进行染色,然后遍历所有与之相连的点v,如果点v没有被染色,则对其进行访问,染上另外一种颜色;如果点v已经被染色,则根据点u和点v的的染色情况来判断是否有冲突:

3.如果颜色相同,即把他们放在同一组,但他们是相互认识的,不能放在同一组,所以产生了冲突;如果颜色不同,则他们被分在了两组,符合要求。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXN = +; int n;
char a[MAXN][MAXN];
int M[MAXN][MAXN], link[MAXN];
bool vis[MAXN]; bool dfs(int u)
{
for(int i = ; i<=n; i++)
if(M[u][i] && !vis[i])
{
vis[i] = true;
if(link[i]==- || dfs(link[i]))
{
link[i] = u;
return true;
}
}
return false;
} int hungary()
{
int ret = ;
memset(link, -, sizeof(link));
for(int i = ; i<=n; i++)
{
memset(vis, , sizeof(vis));
if(dfs(i)) ret++;
}
return ret;
} int col[MAXN];
bool Color(int u, int c) //染色,如果有冲突,则返回true
{
col[u] = c;
for(int i = ; i<=n; i++)
if(M[u][i])
{
if(col[i]==col[u]) return true; //与之前访问过的点相连,且为同色,则有冲突。
if(col[i]==- && Color(i, !c)) return true; //如果没有没有访问过,则对其染色。
}
return false;
} int main()
{
int m;
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(M, false, sizeof(M));
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
M[u][v] = M[v][u] = true;
} bool flag = false;
memset(col, -, sizeof(col));
for(int i = ; i<=n; i++) //染色
if(col[i]==-)
flag = flag|Color(i, ); if(flag)
{
printf("No\n");
continue;
} int cnt = hungary();
printf("%d\n", cnt/);
}
}

HDU2444 The Accomodation of Students —— 二分图最大匹配的更多相关文章

  1. HDU2444 The Accomodation of Students(二分图最大匹配)

    有n个关系,他们之间某些人相互认识.这样的人有m对.你需要把人分成2组,使得每组人内部之间是相互不认识的.如果可以,就可以安排他们住宿了.安排住宿时,住在一个房间的两个人应该相互认识.最多的能有多少个 ...

  2. hdu_2444The Accomodation of Students(二分图的判定和计算)

    hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...

  3. HDU2444 :The Accomodation of Students(二分图染色+二分图匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. HDU 2444 The Accomodation of Students 二分图判定+最大匹配

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

  5. HDU2444 The Accomodation of Students

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. HDU 2444 The Accomodation of Students (二分图存在的判定以及最大匹配数)

    There are a group of students. Some of them may know each other, while others don't. For example, A ...

  7. hdu2444 The Accomodation of Students(推断二分匹配+最大匹配)

    //推断是否为二分图:在无向图G中,假设存在奇数回路,则不是二分图.否则是二分图. //推断回路奇偶性:把相邻两点染成黑白两色.假设相邻两点出现颜色同样则存在奇数回路. 也就是非二分图. # incl ...

  8. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

  9. HDU2444 The Accomodation of Students【匈牙利算法】

    题意: 有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出N ...

随机推荐

  1. SpringMVC最核心

    如图所示:

  2. 【07】QQ群管理公告小结:

    [07]QQ群管理公告小结:   01,请看公告遵守相关规定. 02,群内除QQ自带的缺省表情外(不是QQ的VIP或大图表情),禁止发送大表情,大图片(展示问题的屏幕截图除外),   03,修改群名片 ...

  3. Python Tornado简单的http request

    这是关于chunk encoding传输以前相关传输编码的处理.没有做压缩解码的处理. import tornado.ioloop import tornado.iostream import soc ...

  4. python之GUI自定义界面设计 2014-4-10

    #自定义界面设计mybutton = Button(parent, **configuration options)也可以这么写mybutton.configure(**options)颜色可以用rg ...

  5. struts2拦截器加自定义注解实现权限控制

    https://blog.csdn.net/paul342/article/details/51436565 今天结合Java的Annotation和Struts2进行注解拦截器权限控制. 功能需求: ...

  6. 两个很实用很方便的函数核心及用法{(lower_bound)+(max_element))~~

    (1)            关于 lower_bound(a,a+n,x)-a的用法:                                                求x在数组a中的 ...

  7. HackerRank# The Coin Change Problem

    原题地址 背包问题,没啥好说的,记得用long long,否则会爆 代码: #include <cmath> #include <cstdio> #include <ve ...

  8. 修改K/3 Cloud管理中心端口

    有时候可能会应为端口号被占用或者数据隔离等等,不会使用K/3 Cloud默认的8000端口,这时候就设计到要修改端口号了,具体步骤如下: 1. 2. 打开{安装目录}\ManageSite\App_D ...

  9. [NOIP2002] 提高组 洛谷P1031 均分纸牌

    题目描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若于张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸牌,只能移到编号为 2 ...

  10. 【ZJOI2017 Round1练习&BZOJ4765】D1T3 普通计算姬(主席树,分块)

    题意: 思路:分块 使用树状数组维护sum[i]的前缀和 使用主席树维护root到u的路径上点的编号出现的个数 每次操作如果是修改就加入队列 如果是询问,考虑块内操作对询问的影响,每次在x点加上y会使 ...