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的更多相关文章

  1. HDOJ 2444 The Accomodation of Students

    染色判读二分图+Hungary匹配 The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limi ...

  2. HD2444The Accomodation of Students(并查集判断二分图+匹配)

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

  3. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  4. The Accomodation of Students(判断二分图以及求二分图最大匹配)

    The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

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

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

  6. HDU2444 The Accomodation of Students

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

  7. The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)

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

  8. hdu2444The Accomodation of Students (最大匹配+推断是否为二分图)

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

  9. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

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

随机推荐

  1. 改变edittext边框颜色

    转载自:点击打开链接 第一步:为了更好的比较,准备两个一模一样的EditText(当Activity启动时,焦点会在第一个EditText上,如果你不希望这样只需要写一个高度和宽带为0的EditTex ...

  2. [Effective JavaScript 笔记]第47条:绝不要在Object.prototype中增加可枚举的属性

    之前的几条都不断地重复着for...in循环,它便利好用,但又容易被原型污染.for...in循环最常见的用法是枚举字典中的元素.这里就是从侧面提出不要在共享的Object.prototype中增加可 ...

  3. Vim的使用方法

    导读 Vim是从vi发展出来的一个文本编辑器.代码补全.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用,和Emacs并列成为类Unix系统用户最喜欢的文本编辑器. 一.vi.vim介绍 ...

  4. Apache Common DbUtils

    前段时间使用了Apache Common DbUtils这个工具,在此留个印,以备不时查看.大家都知道现在市面上的数据库访问层的框架很多,当然很多都是包含了OR-Mapping工作步骤的 例如大家常用 ...

  5. python中import和from...import...的区别

    python中import和from...import...的区别: 只用import时,如import xx,引入的xx是模块名,而不是模块内具体的类.函数.变量等成员,使用该模块的成员时需写成xx ...

  6. CSS3.0盒模型display:-webkit-box;的使用

    box-flex是css3新添加的盒子模型属性,它的出现可以解决我们通过N多结构.css实现的布局方式.经典   的一个布局应用就是布局的垂直等高.水平均分.按比例划分. 目前box-flex属性还没 ...

  7. python 列表转为字典的两个小方法

    1.现在有两个列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他们转为这样的字典:{'key1':'1','key2':'2','ke ...

  8. 如何破解excel宏的密码

    http://zhidao.baidu.com/question/140107193.html 最近下载了一个excel模板,使用excel宏编的,但实际需要需更改一下,但是他设置了工作表密码保护,谁 ...

  9. CodeForces - 404A(模拟题)

    Valera and X Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit ...

  10. vs c++中读取数据流并存储

    ifstream in("test.txt"); vector<string> vs; string s; while(!in.eof()) { in>>s ...