wyh2000 and pupil

 Accepts: 93
 Submissions: 925
 Time Limit: 3000/1500 MS (Java/Others)
 Memory Limit: 131072/65536 K (Java/Others)
问题描述
青年理论计算机科学家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的更多相关文章

  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. robotframework+appium 实现App自动化值环境搭建(一)

    第一步: Cmd命令输入pip install robotframework-appiumlibrary  下载和导入appiumlibrary 第二步: 安装JDK,附件有JDK1.8安装包 第三步 ...

  2. git/github使用详解

    介绍:gitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名gitHub. 2018年6月4日,微软宣布,通过75亿美元的股票交易收购代码托管平台Gi ...

  3. Hive事务原理和Datax同步事务表问题解决

    一.事务的概述 1.定义 事务就是一组单元化操作,这些操作要么都执行,要么都不执行,是一个不可分割的工作单位. 2.特点 事务(transaction)具有的四个要素:原子性(Atomicity).一 ...

  4. Mysql使用存储过程创建测试数据

    一.概述 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集.其存储在数据库中,经过第一次编译后调用不需要再次编译,用户通过指定存储过程的名字并给出 ...

  5. 搜索await page.waitForSelector(allResultsSelector);

    /** * Copyright 2017 Google Inc. All rights reserved. * * Licensed under the Apache License, Version ...

  6. 2-10 就业课(2.0)-oozie:4、通过oozie执行shell脚本

    oozie的配置文件job.properties:里面主要定义的是一些key,value对,定义了一些变量,这些变量往workflow.xml里面传递workflow.xml :workflow的配置 ...

  7. win7 & win10 安装AD管理工具

    总所周知,AD域的作用对于一个公司有着无比重要的作用,但是在Win7/10系统下该如何去管理AD域呢. 对于AD域的服务器搭建,在这里我们不进行说明,感兴趣的同学可以去Google相关的资料,现在主要 ...

  8. Day6 - L - Mokia HYSBZ - 1176

    维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. Input 第 ...

  9. NO3 cat-xargs-cp-mv-rm-find命令

    ·cat            #查看文件内容        eg:cat oldboy.txt·xargs        #从标准输入获取内容创建和执行命令 -n 加数字:分组 ·cp       ...

  10. mysql8 安装&问题解决

    1.下载:https://dev.mysql.com/downloads/mysql/ 2.安装 1).设置环境变量 MYSQL_HOME D:\env\j2ee\mysql\mysql-8.0.19 ...