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
indicates the number of test cases.
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 ...
随机推荐
- 数据库支持emoji表情
从MySQL5.5.3开始,MySQL 支持一种utf8mb4的字符集,这个字符集能够支持4字节的UTF8编码的字符.utf8mb4字符集能够完美地兼容utf8字符串.在数据存储方面,当一个普通中文字 ...
- CLR via C#(13)-浅谈事件
提起事件,我们都不陌生,事件使类之间有了交互的能力.它是建立在委托基础上的.有了前面对委托的了解,相信读起事件来也不会太难了.关于事件,现成的好文章数不胜数,本不打算写了.不过问道有先后,各抒己见,也 ...
- 编程风格规范google版
python's coding style,google 命名
- 讲解JS的promise,这篇是专业认真的!
http://www.zhangxinxu.com/wordpress/2014/02/es6-javascript-promise-%E6%84%9F%E6%80%A7%E8%AE%A4%E7%9F ...
- IReport问题整理
1. 问题:IReport如何实现变量字段$F{ propertyName}赋值为一个NULL对象时不显示”null”, 而显示为空白? 解决方法:选中动态单元格,右键选择属性,在弹出对话框TextF ...
- 攻城狮在路上(叁)Linux(二十)--- Linux磁盘格式化
磁盘完成分区之后,进行格式化,生成文件系统. 命令格式: mkfs [-t 文件系统格式] 设备文件名 <== 使用 mkfs [Tab][Tab] 可以查看linux支持的文件系统格式 示例 ...
- MyEclipse 选中右侧编辑的文件时自动展开左侧目录树
- Getting Started with Blocks
本文来源为:developer.apple.com,仅仅是博主练习排版所用. Getting Started with Blocks The following sections help you t ...
- Hibernate入门案例 增删改
一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...
- 全面解析windows下Memcache技术应用
原文 http://www.cnblogs.com/liuqin520/p/4615644.html 一.Memcache介绍 Memcache 是 danga.com 的一个项目,最早是为 L ...