wyh2000 and pupil

 Accepts: 93
 Submissions: 925
 Time Limit: 3000/1500 MS (Java/Others)
 Memory Limit: 131072/65536 K (Java/Others)
问题描述
  1. 青年理论计算机科学家wyh2000在教导他的小学生。
  2. 共有n个小学生,编号为1n。为了增加小学生之间的凝聚力,wyh2000决定将所有小学生分成2组,每组都至少有1个人。
  3. 但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a
  4. Wyh2000希望每组中的小学生都互相认识。而且第一组的人要尽可能多。
  5. 请你帮wyh2000求出第一组和第二组的人数是多少。如果找不到分组方案,则输出"Poor wyh"
输入描述
  1. 第一行一个数T,表示数据组数。
  2. 对于每组数据,第一行两个数n,m,表示小学生数量和互相不认识的小学生的数量。
  3. 接下来m行,每行两个数x,y(x<y),表示x不认识yy不认识x。保证一对(x,y)只会出现一次。
  4. T10,0n,m100000
输出描述
  1. 对于每组数据,输出答案。
输入样例
  1. 2
  2. 8 5
  3. 3 4
  4. 5 6
  5. 1 2
  6. 5 8
  7. 3 5
  8. 5 4
  9. 2 3
  10. 4 5
  11. 3 4
  12. 2 4
输出样例
  1. 5 3
  2. Poor wyh

题解思路已经给出了:

  1. 如果a不认识b,那么在a,b间连一条边,这样有解当且仅当这张图是二分图。
  2. 由于可能有多个二分图,而题目要求第一组的人尽可能多,所以贪心的选择即可。
  3. 要注意m=0的情况。

自己做这道题的时候只顾着“贪“了,每组至少1个人啊。

代码:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <string>
  5. #include <cstring>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. vector <int> segment[100005];
  10. int color[100005];
  11. int Test,num,seg,i,flag,temp1,temp2,color0,color1,color2;
  12.  
  13. void tu(int hao,int tu_color)
  14. {
  15. if((tu_color==0&&color[hao])||flag==0)
  16. return;
  17.  
  18. int size = segment[hao].size();
  19.  
  20. if(tu_color==1)
  21. {
  22. color[hao]=1;
  23. color1++;
  24. }
  25. else if(tu_color==2)
  26. {
  27. color[hao]=2;
  28. color2++;
  29. }
  30. else if(size)
  31. {
  32. color[hao]=1;
  33. color1++;
  34. }
  35.  
  36. if(color[hao]==1)
  37. {
  38. int wang;
  39. for(wang=0;wang<size;wang++)
  40. {
  41. if(color[segment[hao][wang]]==0 )
  42. {
  43. tu(segment[hao][wang],2);
  44. }
  45. else if(color[segment[hao][wang]]==1)
  46. {
  47. flag=0;
  48. }
  49. }
  50. }
  51. else if(color[hao]==2)
  52. {
  53. int chong;
  54. for(chong=0;chong<size;chong++)
  55. {
  56. if(color[segment[hao][chong]]==0 )
  57. {
  58. tu(segment[hao][chong],1);
  59. }
  60. else if(color[segment[hao][chong]]==2)
  61. {
  62. flag=0;
  63. }
  64. }
  65. }
  66.  
  67. }
  68.  
  69. void cal()
  70. {
  71. for(i=1;i<=num;i++)
  72. {
  73. if(color[i]==0)
  74. color0++;
  75. }
  76. }
  77.  
  78. int main()
  79. {
  80. //freopen("i.txt","r",stdin);
  81. //freopen("o.txt","w",stdout);
  82.  
  83. cin>>Test;
  84.  
  85. while(Test--)
  86. {
  87. scanf_s("%d %d",&num,&seg);
  88.  
  89. for(i=1;i<=100004;i++) segment[i].clear();
  90. memset(color,0,sizeof(color));
  91.  
  92. for(i=1;i<=seg;i++)
  93. {
  94. scanf_s("%d %d",&temp1,&temp2);
  95. segment[temp1].push_back(temp2);
  96. segment[temp2].push_back(temp1);
  97. }
  98. if(num<= 1) {
  99. puts("Poor wyh");
  100. continue;
  101. }
  102. flag=1;
  103. int max_c=0,min_c=0,xun;
  104. for(xun=1;xun<=num;xun++)
  105. {
  106. color1=0;color2=0;
  107. tu(xun,0);
  108. max_c +=max(color1,color2);
  109. min_c +=min(color1,color2);
  110. }
  111. if(flag)
  112. {
  113. color0=0;
  114. cal();
  115. if(min_c==0)
  116. cout<<num-1<<" "<<1<<endl;
  117. else
  118. cout<<color0+max_c<<" "<<min_c<<endl;
  119. }
  120. else
  121. {
  122. puts("Poor wyh");
  123. }
  124.  
  125. }
  126.  
  127. return 0;
  128. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5285:wyh2000 and pupil的更多相关文章

  1. HDU 5285 wyh2000 and pupil 判二分图+贪心

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5285 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  2. HDU 5285 wyh2000 and pupil(dfs或种类并查集)

    wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Other ...

  3. ACM: HDU 5285 wyh2000 and pupil-二分图判定

     HDU 5285  wyh2000 and pupil Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%I64d &a ...

  4. 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil

    题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...

  5. HDU - 6409:没有兄弟的舞会(数学+思维)

    链接:HDU - 6409:没有兄弟的舞会 题意: 题解: 求出最大的 l[i] 的最大值 L 和 r[i] 的最大值 R,那么 h 一定在 [L, R] 中.枚举每一个最大值,那么每一个区间的对于答 ...

  6. wyh2000 and pupil

    wyh2000 and pupil  Accepts: 93  Submissions: 925  Time Limit: 3000/1500 MS (Java/Others)  Memory Lim ...

  7. 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 ...

  8. HDU 5285 wyh2000 and pupil (二分图着色)

    题意: 共有n个小学生,编号为1−n.将所有小学生分成2组,每组都至少有1个人.但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a.Wyh2000希望每组中的小学生都互相认识.而且第一组 ...

  9. Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)

    题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...

随机推荐

  1. 4.2 Scikit-Learn简介(机器学习篇)

    目录 第四章 机器学习 4.1 机器学习简介 4.1.1 机器学习分类 4.2 Scikit-Learn简介 4.2.1 Scikit-Learn的数据表示 4.2.2 Scikit-Learn的评估 ...

  2. centos 下yum 安装nginx

    centos 下yum 安装nginx 1. 直接yum install nginx不行,要先处理下源: rpm -ivh http://nginx.org/packages/centos/6/noa ...

  3. liunx命令用到的

    su:切换成root用户 sudo su:普通用户申请root权限 ping命令可以检查linux是否联网 ping www.baidu.com 如图就是联网了 结束ping包括其他linux的指令 ...

  4. SciPy 优化

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  5. GetHub上很实用的几个Demo

    手机号匹配的正则表达式:https://github.com/VincentSit/ChinaMobilePhoneNumberRegex/blob/master/README-CN.md FEBS- ...

  6. P1079 延迟的回文数

    P1079 延迟的回文数 转跳点:

  7. Maven添加Tomcat插件实现热部署

    Maven热部署,顾名思义就是可以不影响项目在服务器中的运行情况,可以实现项目代码的更新,减少启动,编译时间,达到快速开发的目的,也不需要手动拷贝war包到远程项目,可以直接将项目以及war包部署到远 ...

  8. 洛谷 P1929 迷之阶梯

    题目传送门 解题思路: f[i]表示跳到第i层的最少移动次数,如果可以从下面一级跳上来,那么直接跳上来,如果跳不上来,那就往后退,退到不能退或能跳上第i层 AC代码: #include<iost ...

  9. 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 ...

  10. dataGridView与数据源dataTable同步排序

    private void dataGridView1_Sorted(object sender, EventArgs e)         {             string _sortStr ...