ACM: HDU 5285 wyh2000 and pupil-二分图判定
Description
Wyh2000 has n pupils.Id of them are from to
.In order to increase the cohesion between pupils,wyh2000 decide to divide them into 2 groups.Each group has at least 1 pupil.
Now that some pupils don't know each other(if doesn't know
,then
doesn't know
).Wyh2000 hopes that if two pupils are in the same group,then they know each other,and the pupils of the first group must be as much as possible.
Please help wyh2000 determine the pupils of first group and second group. If there is no solution, print "Poor wyh".
Input

For each case, the first line contains two integers indicate the number of pupil and the number of pupils don't konw each other.
In the next m lines,each line contains 2 intergers <
,indicates that
don't know
and
don't know
,the pair
will only appear once.
Output
Sample Input
2
8 5
3 4
5 6
1 2
5 8
3 5
5 4
2 3
4 5
3 4
2 4
Sample Output
5 3
Poor wyh
/*/
二分图染色法判定 题解: 把两个相连的点标记颜色为不同颜色1和0,表示两位同学相互不认识; 统计两个颜色的个数。 注意,应为可能出现多个联通块的情况,这时候只要将各个联通块的最少的颜色 加起来,用总人数减去这个和就是人数最多那个组的人数了。 本题还要注意几个特判。 AC代码:
/*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"string"
#include"cstdio"
#include"vector"
#include"cmath"
#include"queue"
using namespace std;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define MX 500005 struct Edge {
int v,nxt;
} E[MX*2]; int col[3]; int Head[MX],erear; void edge_init() {
erear=0;
memset(E,0);
memset(Head,-1);
}
void edge_add(int u,int v) {
E[erear].v=v;
E[erear].nxt=Head[u];
Head[u]=erear++;
} int color[MX];
bool DFS(int u,int c) {
color[u]=c;
if(color[u]==1) {
col[1]++;
} else col[0]++;
for(int i=Head[u]; ~i; i=E[i].nxt) {
int v=E[i].v;
if(color[v]!=-1&&color[u]==color[v]) return 0;
else if(color[v]==-1) {
if(DFS(v,c^1)==0) return 0;
}
}
return 1;
} int main() {
int n,m;
int T;
cin>>T;
while(T--) {
scanf("%d%d",&n,&m);
edge_init();
for(int i=1; i<=m; i++) {
int u,v;
scanf("%d%d",&u,&v);
edge_add(u,v);
edge_add(v,u);
}
if(n<2) {
puts("Poor wyh");
continue;
}
if(!m) {
printf("%d 1\n",n-1);
}
memset(color,-1);
bool sign=1;
int minn=0;
for(int i=1; i<=n; i++) {
if(color[i]==-1) {
memset(col,0);
sign=DFS(i,0);
minn+=min(col[0],col[1]);
if(!sign)break;
} }
if(!sign)puts("Poor wyh") ;
else printf("%d %d\n",n-minn,minn);
}
return 0;
}
ACM: HDU 5285 wyh2000 and pupil-二分图判定的更多相关文章
- HDU 5285 wyh2000 and pupil (二分图着色)
题意: 共有n个小学生,编号为1−n.将所有小学生分成2组,每组都至少有1个人.但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a.Wyh2000希望每组中的小学生都互相认识.而且第一组 ...
- HDU 5285 wyh2000 and pupil 判二分图+贪心
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5285 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- HDU 5285 wyh2000 and pupil(dfs或种类并查集)
wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Other ...
- Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)
题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...
- HDU 5285 wyh2000 and pupil
题意:有一群人,已知某两人之间互相不认识,要把这群人分成两部分,每部分至少一人,且在每部分内没有人互不认识. 解法:图染色.某场bestcoder第二题……看完题觉得是个二分图……完全不会二分图什么的 ...
- hdu 5285 wyh2000 and pupil(二染色)
第一次用vector解得题.值得纪念,这道题是二染色问题,我用bfs解得.就是染色,推断,计数问题,其 实挺简单的,就是得判一下特殊情况,当n<2的时候就不能有解,由于题目要求每一个组至少有一个 ...
- [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil
题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...
- HDU 5285:wyh2000 and pupil
wyh2000 and pupil Accepts: 93 Submissions: 925 Time Limit: 3000/1500 MS (Java/Others) Memory Lim ...
随机推荐
- 七牛:关于图片 EXIF 信息中旋转参数 Orientation 的理解
EXIF(Exchangeable Image File)是 “可交换图像文件” 的缩写,当中包含了专门为数码相机的照片而定制的元数据,可以记录数码照片的拍摄参数.缩略图及其他属性信息,简单来说,Ex ...
- centos6.4下安装php的imagick和imagemagick扩展教程
imagick在centos6.4的安装方法: .安装ImageMagick 代码如下: wget http://soft.vpser.net/web/imagemagick/ImageMagick- ...
- WPF Navigation
在开始学习WPF时,一开始对WPF的Window, Page, UserControl感到很迷惑.不知道什么时候该使用哪一个.下面简单介绍一下这三者的区别. Window:故名思意,桌面程序的窗体.在 ...
- 发送http请求get方法
//获取网页html NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"]; NSMutableURLRequest ...
- C++模板【转】
1. 模板的概念. 我们已经学过重载(Overloading),对重载函数而言,C++的检查机制能通过函数参数的不同及所属类的不同.正确的调用重载函数.例如,为求两个数的最大值,我们定义MAX()函数 ...
- Arduino101学习(一)——Windows下环境配置
一.Arduino IDE下载 要开发Arduino 101/Genuino 101,你需要先安装并配置好相应的开发环境.下载地址 http://www.arduino.cn/thread-5838- ...
- supervisor简介
一什么是supervisor Superviosr是一个UNIX-like系统上的进程监控工具. Supervisor是一个Python开发的client/server系统,可以管理和监控*nix上面 ...
- 深入理解KMP算法
前言:本人最近在看<大话数据结构>字符串模式匹配算法的内容,但是看得很迷糊,这本书中这块的内容感觉基本是严蔚敏<数据结构>的一个翻版,此书中给出的代码实现确实非常精炼,但是个人 ...
- java&Protocol Buffers
ps: Protocol Buffers简称PB PB 安装配置 下载 PB: 在 PB 官网,下载最新版(或者其他版本)PB,这里为了与 Java 项目中的 PB Maven 依赖版本一致,使用 P ...
- poj3642 01背包
http://poj.org/problem?id=3624 #include<iostream> #include<cstdio> #include<algorithm ...