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 Round #FF (Div. 2) C. DZY Loves Sequences
解题报告:输入一个数列,选取一个子数列,要求最多只能改动这个子数列中的一个数,使得这个子数列是严格的升序的(严格升序没有相等的) 我的做法是,第一步把这个 数列的每个升序的子数列都找出来,然后看这些子 ...
- python4delphi import lxml pandas 出错的小结
环境: 1.win10 64位 2.delphi xe8 3.python2.7 4.python4delphi (svn 2015-03-21 发布的83版本号) 5.lxml 3.4.4(通过p ...
- linux开机启动服务和chkconfig使用方法(自定义服务路径启动)
服务概述在linux操作系统下,经常需要创建一些服务,这些服务被做成shell脚本,这些服务需要在系统启动的时候自动启动,关闭的时候自动关闭.将 需要自动启动的脚本/etc/rc.d/init.d目录 ...
- 面向侧面的程序设计AOP-------《三》.Net平台AOP技术概览
本文转载自张逸:晴窗笔记 .Net平台与Java平台相比,由于它至今在服务端仍不具备与unix系统的兼容性,也不具备类似于Java平台下J2EE这样的企业级容器,使得.Net平台在大型的企业级应用上, ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 高阶函数复习:利用reduce和map把字符串转为数字
复习高阶函数的时候,有一道题想了半天解不出来.看了下别人的解法,发现学习编程,思维真的很重要. 习题: 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数1 ...
- PageBean分页组件
Page.java package org.guangsoft.bean; import java.util.List; public class Page { private int current ...
- JPush开发
主要功能 保持与服务器的长连接,以便消息能够即时推送到达客户端 接收通知与自定义消息,并向开发者App 传递相关信息 SDK集成步骤 1.导入 SDK 开发包到你自己的应用程序项目 解压缩 jpush ...
- Set和Map
Set和Map
- 关于Java异常和错误的几个问题
1.Java中什么是Exception? 异常是Java传达给你的系统和程序错误的方式. 在java中,异常功能是通过实现比如Throwable,Exception,RuntimeException之 ...