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 ...
随机推荐
- 设计模式学习之中介者模式(Mediator,行为型模式)(18)
转载地址:http://www.cnblogs.com/zhili/p/MediatorPattern.html 一.引言 在现实生活中,有很多中介者模式的身影,例如QQ游戏平台,聊天室.QQ群和短信 ...
- at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) 报空指针
原来发布到weblogic 的项目,想改动发布到tomcat上.启动发布一切都正常.出入项目请求路径却包错: java.lang.NullPointerException at org.apache. ...
- [LeetCode] Isomorphic Strings
Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...
- HDU3364 Lanterns(求矩阵的秩)
求矩阵的秩,及判断有无解 #include<cstdio> #include<iostream> #include<cstdlib> #include<cst ...
- 使用git进行团队合作开发
1.git 和 svn 的差异 git和svn 最大的差异在于git是分布式的管理方式而svn是集中式的管理方式.如果不习惯用代码管理工具,可能比较难理解分布式管理和集中式管理的概念.下面介绍两种工具 ...
- js 上传文件后缀名的判断 var flag=false;应用
js 上传文件后缀名的判断 var flag=false;应用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...
- Java 类装载器
类装载器 基本概念: 顾名思义,类加载器(class loader)用来把Java 类动态的加载到 Java 虚拟机中.也就是说当程序需要某个类时,类加载器就把这个类的二进行加入到虚拟机中. 类加载器 ...
- PMP 第十章 项目沟通管理
1识别干系人 2规划沟通 3发布信息 4管理干系人期望 5报告绩效 1.沟通的维度有哪些?沟通技巧有哪些? 2.规划沟通管理的目的是什么?沟通渠道的计算(重点).影响沟通技术的因素有哪些?沟通模型的步 ...
- 第十二篇:SOUI的utilities模块为什么要用DLL编译?
SOUI相对于DuiEngine一个重要的变化就是很多模块变成了一个单独的DLL. 然后很多情况下用户可能希望整个产品就是一个EXE,原来DuiEngine提供了LIB编译模式,此时链接LIB模式的D ...
- HDU 3727 Jewel 可持久化线段树
Jewel Problem Description Jimmy wants to make a special necklace for his girlfriend. He bought man ...