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 ( ...
 
随机推荐
- [codeforces 516]A. Drazil and Factorial
			
[codeforces 516]A. Drazil and Factorial 试题描述 Drazil is playing a math game with Varda. Let's define ...
 - [BZOJ2683][BZOJ4066]简单题
			
[BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...
 - 正则匹配之url的匹配
			
通过这几天的学习,已经对正则有所了解了. 下面动手写一个匹配url的正则吧. <?php $str="http://www.baidu.com"; $reg="/( ...
 - log2取整效率测试
			
RMQ问题中有个ST算法,当然还有个标准算法.LCA问题可以转化为带限制的RMQ(RMQ+-1)问题来解决.我们姑且认为这些问题的时间复杂度是查询$O(1)$的.但是,注意到对于RMQ(/+-1)问题 ...
 - 【云计算】Dockerfile、镜像、容器快速入门
			
Dockerfile.镜像.容器快速入门 1.1.Dockerfile书写示例 Dockerfile可以用来生成Docker镜像,它明确的定义了Image的生成过程.虽然直接修改容器也可以提交生成镜像 ...
 - tcp/IP点对点通信程序
			
点对点的通信 服务器端与客户端在建立连接之后创建一个进程 服务器端: 子进程用于接收主机的输入并将数据发送出去.父进程用于接收客户端的数据并输出到主机. 子进程一直等待主机的输入,输入的数据放在发送缓 ...
 - PYTHON实现HTTP摘要认证(DIGEST AUTHENTICATION)
			
参考: http://blog.csdn.net/kiwi_coder/article/details/28677651 http://blog.csdn.net/gl1987807/article/ ...
 - codeforces  468A. 24 Game  解题报告
			
题目链接:http://codeforces.com/problemset/problem/468/A 题目意思:给出一个数n,利用 1 - n 这 n 个数,每个数只能用一次,能否通过3种运算: + ...
 - fedora yum 清缓存
			
1.清除缓存目录(/var/cache/yum)下的软件包命令:yum clean packages 2.清除缓存目录(/var/cache/yum)下的 headers命令:yum clean he ...
 - 信与信封问题(codevs 1222)
			
题目描述 Description John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出.但是,第二天John的儿子Small John将这n封信都拿出了信封.不幸的是,Small Joh ...