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个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...
随机推荐
- 4.2 Scikit-Learn简介(机器学习篇)
目录 第四章 机器学习 4.1 机器学习简介 4.1.1 机器学习分类 4.2 Scikit-Learn简介 4.2.1 Scikit-Learn的数据表示 4.2.2 Scikit-Learn的评估 ...
- centos 下yum 安装nginx
centos 下yum 安装nginx 1. 直接yum install nginx不行,要先处理下源: rpm -ivh http://nginx.org/packages/centos/6/noa ...
- liunx命令用到的
su:切换成root用户 sudo su:普通用户申请root权限 ping命令可以检查linux是否联网 ping www.baidu.com 如图就是联网了 结束ping包括其他linux的指令 ...
- SciPy 优化
章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...
- GetHub上很实用的几个Demo
手机号匹配的正则表达式:https://github.com/VincentSit/ChinaMobilePhoneNumberRegex/blob/master/README-CN.md FEBS- ...
- P1079 延迟的回文数
P1079 延迟的回文数 转跳点:
- Maven添加Tomcat插件实现热部署
Maven热部署,顾名思义就是可以不影响项目在服务器中的运行情况,可以实现项目代码的更新,减少启动,编译时间,达到快速开发的目的,也不需要手动拷贝war包到远程项目,可以直接将项目以及war包部署到远 ...
- 洛谷 P1929 迷之阶梯
题目传送门 解题思路: f[i]表示跳到第i层的最少移动次数,如果可以从下面一级跳上来,那么直接跳上来,如果跳不上来,那就往后退,退到不能退或能跳上第i层 AC代码: #include<iost ...
- Day7 - A - Visible Lattice Points POJ - 3090
A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), othe ...
- dataGridView与数据源dataTable同步排序
private void dataGridView1_Sorted(object sender, EventArgs e) { string _sortStr ...