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 ( ...
随机推荐
- Unity3d Static 静态批处理和动态批处理
表示物体时静态的,多用于静止不动的物体,此外static有多种,有的用于烘焙,有的用于遮挡剔除 物理效果是rigidbody组件,和这个没关系,用transform.Translate 无法移动,因为 ...
- vim在选中的几行中查找并替换
(文章是从我的个人主页上粘贴过来的, 大家也可以访问我的主页 www.iwangzheng.com) 使用vim编辑器是能带来很大的便捷的,如何替换选中的多行中的某个单词呢. 先选中,按下冒号,左下方 ...
- DICOM:DICOM3.0网络通信协议
转载:http://blog.csdn.net/zssureqh/article/details/41016091 背景: 专栏取名为DICOM医学图像处理原因是:博主是从医学图像处理算法研究时开始接 ...
- Linux 查看系统用户的登录日志
查看用户登录系统的日志 有两类日志记录用户登录的行为,一是记录登录者的数据,一个是记录用户的登录时间 一,记录用户登录数据 /var/log/wtmp日志文件记录用户登录的数据 ...
- XP 之后, Delphi 动注册表不方便了...逼出来一个办法:
XP 之后, Delphi 动注册表不方便了...逼出来一个办法: 手头的程序需要修改注册表, 以让当前程序成为某格式的默认打开程序并关联图标; Vista 之后需要管理员权限才能操作注册表, 很麻烦 ...
- CStringUtf8ToUnicode
CString CStringUtf8ToUnicode( CString Utf8 ) { int wLen = 0; CString strUnicode; LPSTR pBufChar = NU ...
- Java for LeetCode 055 Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【mysql】执行mysql脚本
来源:http://zhidao.baidu.com/link?url=p78PlTJZlheB4uSKCkmZApg-3qrTIBCS3yI5LbGJLEAnUuO3-4GE6dLqq1LWC_kn ...
- JavaScript、Jquery选择题
尚学堂Java EE软件工程师认证考试 试题库-选择题 一. 选择题(包括单选和双选) 1.B 在JavaScript中,以下变量命名非法的是( )(选择一项) A. numb_1 ...
- NSOperation基本使用
NSOperation简单介绍 a. 是OC语言中基于GCD的面向对象的封装 b. 使用起来比GCD更加简单(面向对象) c. 提供了一些用GCD不好实现的功能 d. 苹果推荐使用,使用NSOper ...