HDU 5285:wyh2000 and pupil
wyh2000 and pupil
青年理论计算机科学家wyh2000在教导他的小学生。
共有n个小学生,编号为1−n。为了增加小学生之间的凝聚力,wyh2000决定将所有小学生分成2组,每组都至少有1个人。
但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a。
Wyh2000希望每组中的小学生都互相认识。而且第一组的人要尽可能多。
请你帮wyh2000求出第一组和第二组的人数是多少。如果找不到分组方案,则输出"Poor wyh"。
第一行一个数T,表示数据组数。
对于每组数据,第一行两个数n,m,表示小学生数量和互相不认识的小学生的数量。
接下来m行,每行两个数x,y(x<y),表示x不认识y,y不认识x。保证一对(x,y)只会出现一次。
T≤10,0≤n,m≤100000
对于每组数据,输出答案。
2
8 5
3 4
5 6
1 2
5 8
3 5
5 4
2 3
4 5
3 4
2 4
5 3
Poor wyh
题解思路已经给出了:
如果a不认识b,那么在a,b间连一条边,这样有解当且仅当这张图是二分图。
由于可能有多个二分图,而题目要求第一组的人尽可能多,所以贪心的选择即可。
要注意m=0的情况。
自己做这道题的时候只顾着“贪“了,每组至少1个人啊。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
using namespace std; vector <int> segment[100005];
int color[100005];
int Test,num,seg,i,flag,temp1,temp2,color0,color1,color2; void tu(int hao,int tu_color)
{
if((tu_color==0&&color[hao])||flag==0)
return; int size = segment[hao].size(); if(tu_color==1)
{
color[hao]=1;
color1++;
}
else if(tu_color==2)
{
color[hao]=2;
color2++;
}
else if(size)
{
color[hao]=1;
color1++;
} if(color[hao]==1)
{
int wang;
for(wang=0;wang<size;wang++)
{
if(color[segment[hao][wang]]==0 )
{
tu(segment[hao][wang],2);
}
else if(color[segment[hao][wang]]==1)
{
flag=0;
}
}
}
else if(color[hao]==2)
{
int chong;
for(chong=0;chong<size;chong++)
{
if(color[segment[hao][chong]]==0 )
{
tu(segment[hao][chong],1);
}
else if(color[segment[hao][chong]]==2)
{
flag=0;
}
}
} } void cal()
{
for(i=1;i<=num;i++)
{
if(color[i]==0)
color0++;
}
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); cin>>Test; while(Test--)
{
scanf_s("%d %d",&num,&seg); for(i=1;i<=100004;i++) segment[i].clear();
memset(color,0,sizeof(color)); for(i=1;i<=seg;i++)
{
scanf_s("%d %d",&temp1,&temp2);
segment[temp1].push_back(temp2);
segment[temp2].push_back(temp1);
}
if(num<= 1) {
puts("Poor wyh");
continue;
}
flag=1;
int max_c=0,min_c=0,xun;
for(xun=1;xun<=num;xun++)
{
color1=0;color2=0;
tu(xun,0);
max_c +=max(color1,color2);
min_c +=min(color1,color2);
}
if(flag)
{
color0=0;
cal();
if(min_c==0)
cout<<num-1<<" "<<1<<endl;
else
cout<<color0+max_c<<" "<<min_c<<endl;
}
else
{
puts("Poor wyh");
} } return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5285:wyh2000 and pupil的更多相关文章
- 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 ...
- ACM: HDU 5285 wyh2000 and pupil-二分图判定
HDU 5285 wyh2000 and pupil Time Limit:1500MS Memory Limit:65536KB 64bit IO Format:%I64d &a ...
- 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil
题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...
- HDU - 6409:没有兄弟的舞会(数学+思维)
链接:HDU - 6409:没有兄弟的舞会 题意: 题解: 求出最大的 l[i] 的最大值 L 和 r[i] 的最大值 R,那么 h 一定在 [L, R] 中.枚举每一个最大值,那么每一个区间的对于答 ...
- wyh2000 and pupil
wyh2000 and pupil Accepts: 93 Submissions: 925 Time Limit: 3000/1500 MS (Java/Others) Memory Lim ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- HDU 5285 wyh2000 and pupil (二分图着色)
题意: 共有n个小学生,编号为1−n.将所有小学生分成2组,每组都至少有1个人.但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a.Wyh2000希望每组中的小学生都互相认识.而且第一组 ...
- Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)
题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...
随机推荐
- SQL中limit的用法
limit子句用于限制查询结果返回的数量 格式: select * from tableName limit i,n # tableName:表名 # i:为查询结果的索引值(默认从0开始),当i=0 ...
- 通过CrawlSpider对招聘网站进行整站爬取(拉勾网实战)
爬虫首先要明确自己要爬取的网站以及内容 进入拉勾网的网站然后看看想要爬取什么内容职位,薪资,城市,经验要求学历要求,全职或者兼职职位诱惑,职位描述提取公司的名称 以及 在拉勾网的url等等 然后在na ...
- Linux磁盘
1.磁盘的接口类型与命名方式 磁盘接口分为SATA.SCSI.SAS.PCI-E.光纤FC通道.常见的设备在Linux中的命名如下: 设备 设备在Linux内的文件名 IDE硬盘 /dev/hd[a- ...
- python学习笔记(三)---高级特性
一.切片 取无数多个list元素 不用一个个取得笨方法就用切片 对这种经常取指定索引范围的操作,用循环十分繁琐,因此,Python提供了切片(Slice)操作符,能大大简化这种操作. 对应上面的问题, ...
- Commons BeanUtils 中对Map的操作
CSDN学院招募微信小程序讲师啦 程序员简历优化指南! [观点]移动原生App开发 PK HTML 5开发 云端应用征文大赛,秀绝招,赢无人机! Commons BeanUtils 中对Map的操作 ...
- (转)spring mvc 中文乱码问题解决
在eclipse环境里,页面传输数据的时候通常用ISO-8859-1这个字符集可以用 str = new String(str.getBytes("ISO-8859-1"), &q ...
- spring中的@Transactional注解
前几天灿哥问我,在做程序的时候,有没有考虑到事务,如果一个函数在中间执行过程中报错了,它会回滚么?我查了一查,spring确实有这样一个注解,能快速帮助我们配置事务管理.下面我就简单介绍一下这个注解. ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:禁用状态
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- SciPy 图像处理
章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...
- BusyBox 添加 自定义命令\小程序 (applet)
背景 在做嵌入式开的时候,busybox无疑是非常好用的命令集,所以很多时候都喜欢把busybox移植到我们的系统里面. 说明 添加一个很简单的命令--hello_busybox,输出"He ...