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 ...
随机推荐
- ListView + PopupWindow实现滑动删除
原文:ListView滑动删除 ,仿腾讯QQ(鸿洋_) 文章实现的功能是:在ListView的Item上从右向左滑时,出现删除按钮,点击删除按钮把Item删除. 看过文章后,感觉没有必要把dispat ...
- 【131031】rel 属性 -- link标签中的rel属性,定义了文档与链接的关系
此属性通常出现在a,link标签中 属性值 Alternate -- 定义交替出现的链接 Alternate 属性值 -- alternate是LinkTypes的一个值,网页设计者可以通过此值,设计 ...
- hibernate中many-to-one关联时出现ObjectNotFoundException异常
采用多对一关联,如果一的那端删除了,多的这端无法感知,虽然数据库中可以通过外键配置将多的一端置空,可是在hibernate里面我暂时不知道如何处理. 目前采用的方式: 1.首先,数据库中需要配置好外键 ...
- android 入门-R文件的死与活
1.图片的名字Btn_Play R文件死了. 1.答:修改图片的名字btn_play R文件活了.
- hdu 4025 2011上海赛区网络赛E 压缩 ***
直接T了,居然可以这么剪枝 题解链接:点我 #include<cstdio> #include<map> #include<cstring> #define ll ...
- 修改vs helpview手册路径
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Help\v2.1\Ca ...
- Java学习笔记(四)——流程控制语句
一.条件语句 1.if条件语句 (1)语法: if(条件) { 条件成立时的代码 } (2)执行过程 2.if else语句 if(条件) { 条件成立时的代码 } else { 不成立的代码 } 3 ...
- (转载)Bash 中的特殊字符大全
转自:https://linux.cn/article-5657-1.html Linux下无论如何都是要用到shell命令的,在Shell的实际使用中,有编程经验的很容易上手,但稍微有难度的是she ...
- 二维数组&多维数组
1.二维数组 二维数组由多个一维数组组成,其定义方式: ,]{ {,,,}, {,,,}, {,,,} }; 二维数组中括号中,逗号左边表示一维数组的个数,也可以说控制行,逗号后面的数表示每个一维数组 ...
- 第一个vs2013控制台程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...