The Accomodation of Students
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1462 Accepted Submission(s): 716
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
2008 Asia Harbin Regional Contest Online
Recommend
gaojie
又是一遍AC,虽然还是模板题,但是一个小时4个1a让我今天下午很愉快啊有木有.
#include<stdio.h>
#include<string.h>
int N,M;
int color[],match[];
bool visit[],G[][],flag;
void draw(int k,int cc)
{
if (color[k]!=- && color[k]!=cc)
{
flag=false;
return;
}
if (color[k]==cc) return;
color[k]=cc;
int c=-cc;
for (int i=;i<=N;i++)
if (G[k][i] && flag) draw(i,c);
}
bool DFS(int k)
{
int t;
for (int i=;i<=N;i++)
if (G[k][i] && !visit[i])
{
visit[i]=;
t=match[i];
match[i]=k;
if (t==- || DFS(t)) return true;
match[i]=t;
}
return false;
}
int Max_match()
{
int ans=;
memset(match,-,sizeof(match));
for (int i=;i<=N;i++)
{
memset(visit,,sizeof(visit));
if (DFS(i)) ans++;
}
return ans;
}
int main()
{
while (scanf("%d%d",&N,&M)!=EOF)
{
memset(G,,sizeof(G));
for (int i=;i<=M;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u][v]=;
G[v][u]=;
}
memset(color,-,sizeof(color));
flag=true;
for (int i=;i<=N;i++)
if (flag && color[i]==-) draw(i,);
if (!flag) printf("No\n");
else printf("%d\n",Max_match()/);
}
return ;
}
The Accomodation of Students的更多相关文章
- HDOJ 2444 The Accomodation of Students
染色判读二分图+Hungary匹配 The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limi ...
- HD2444The 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 The Accomodation of Students Time Limit:1000MS Me ...
- The Accomodation of Students(判断二分图以及求二分图最大匹配)
The Accomodation of Students Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- hdu_2444The Accomodation of Students(二分图的判定和计算)
hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...
- HDU2444 The Accomodation of Students
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu2444The Accomodation of Students (最大匹配+推断是否为二分图)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- hdu 2444 The Accomodation of Students 判断二分图+二分匹配
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- 高流量站点NGINX与PHP-fpm配置优化
导读 使用Nginx搭配PHP已有7年的经历,这份经历让我们学会如何为高流量站点优化NGINX和PHP-fpm配置. 以下正是这方面的一些提示和建议: 1. 将TCP切换为UNIX域套接字 1. 将T ...
- [BZOJ3624][Apio2008]免费道路
[BZOJ3624][Apio2008]免费道路 试题描述 输入 输出 输入示例 输出示例 数据规模及约定 见“输入”. 题解 第一步,先尽量加入 c = 1 的边,若未形成一个连通块,则得到必须加入 ...
- Leetcode 之Validate Binary Search Tree(53)
判断是否是有效的二叉搜索树,即左子树的值小于根结点,右子树的值大于根结点.可以采用递归的方式来完成,递归时如何 传递有效的参数与根结点进行比较,是此题的难点. bool isValidBST(Tree ...
- poj3295
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10453 Accepted: 3967 Descri ...
- 反转字符串--C和Python
将字符串反转,即“abcde”->"edcba" C语言实现: [转自http://www.kanzhun.com/mianshiti/456.html?sid=mail_1 ...
- python string与list互转
因为python的read和write方法的操作对象都是string.而操作二进制的时候会把string转换成list进行解析,解析后重新写入文件的时候,还得转换成string. >>&g ...
- 《ASP.NET1200例》当前上下文中不存在名称configurationmanager
当前上下文中不存在名称ConfigurationManager的解决方法 今晚做项目在DBHelper.cs类中的数据库连接要改到web.config里面调用,结果在编译的时候却发现提示错误: 当前上 ...
- Android Volley获取json格式的数据
为了让Android能够快速地访问网络和解析通用的数据格式Google专门推出了Volley库,用于Android系统的网络传输.volley库可以方便地获取远程服务器的图片.字符串.json对象和j ...
- css3学习总结9--CSS3过渡
CSS3 过渡 通过 CSS3,我们可以在不使用 Flash 动画或 JavaScript 的情况下,当元素从一种样式变换为另一种样式时为元素添加效果. 过渡属性 属性 描述 CSS transiti ...
- 【读书笔记】读《JavaScript模式》 - 函数复用模式之现代继承模式
现代继承模式可表述为:其他任何不需要以类的方式考虑得模式. 现代继承方式#1 —— 原型继承之无类继承模式 function object(o) { function F() {}; F.protot ...