wyh2000 and pupil

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

Total Submission(s): 755    Accepted Submission(s): 251

Problem Description
Young theoretical computer scientist wyh2000 is teaching his pupils.



Wyh2000 has n pupils.Id of them are from 1
to n.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 a
doesn't know b,then
b
doesn't know a).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
In the first line, there is an integer
T
indicates the number of test cases.



For each case, the first line contains two integers
n,m
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
x,y(x<y),indicates
that x
don't know y
and y
don't know x,the
pair (x,y)
will only appear once.



T≤10,0≤n,m≤100000
 
Output
For each case, output the answer.
 
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
 

大致题意:
n个点分成两组,m条边,每条边连接的两个点必须是在不同的两组,且第一组要尽量的大

思路:显然。dfs染色,不是二分图就无解。
还能够用种类并查集来做,把全部可能性合并,由对称性可知,仅仅统计1~n是祖先时。看祖先的子节点中黑白节点是多少个就ok

//468MS 4036K 1981 B C++
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
typedef pair<ll,ll> pii; const int N = 1e5+100; int sz[N*2];
int sz2[N*2];
int fa[N*2];
int n,m;
void ini(){
REP(i,2*n) fa[i] = i;
REP(i,2*n) sz[i] = (i <= n);
REP(i,2*n) sz2[i] = (i > n);
}
int getf(int x){
return x == fa[x] ? x : fa[x] = getf(fa[x]);
}
bool same(int a,int b){
return getf(a) == getf(b);
}
void Merge(int a,int b){
int f1 = getf(a), f2 = getf(b);
if(f1 == f2) return ;
fa[f1] = f2;
sz[f2] += sz[f1];
sz2[f2] += sz2[f1];
}
int main(){
int T;
cin>>T;
while(T--){
scanf("%d%d",&n,&m);
ini();
bool flag = 0;
REP(i,m){
int a,b;
scanf("%d%d",&a,&b);
if(flag) continue;
if(same(a,b) || same(a+n,b+n) ) flag = 1;
else Merge(a+n,b),Merge(a,b+n);
}
if( n < 2 || flag) puts("Poor wyh");
else if(m == 0) printf("%d 1\n",n-1);
else {
int ans = 0;
REP(i,n){
if( fa[i] == i) ans += min(sz[i],sz2[i]);
}
printf("%d %d\n",n-ans,ans);
} }
}

HDU 5285 wyh2000 and pupil(dfs或种类并查集)的更多相关文章

  1. HDU 1829 A Bug's Life (种类并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Oth ...

  2. hdu 1182 A Bug's Life(简单种类并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...

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

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

  4. hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

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

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

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

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

  7. HDU 5285 wyh2000 and pupil

    题意:有一群人,已知某两人之间互相不认识,要把这群人分成两部分,每部分至少一人,且在每部分内没有人互不认识. 解法:图染色.某场bestcoder第二题……看完题觉得是个二分图……完全不会二分图什么的 ...

  8. hdu 5285 wyh2000 and pupil(二染色)

    第一次用vector解得题.值得纪念,这道题是二染色问题,我用bfs解得.就是染色,推断,计数问题,其 实挺简单的,就是得判一下特殊情况,当n<2的时候就不能有解,由于题目要求每一个组至少有一个 ...

  9. HDU 3038 How Many Answers Are Wrong(种类并查集)

    题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[ ...

随机推荐

  1. Hash冲突的几种解决方法

    1. 开放定值法: 也叫再散列法,当关键字key的哈希地址p=H(key)出现冲突时,以p为基础,产生另一个哈希地址p1,如果p1仍然冲突,再以p为基础,产生另一个哈希地址p2,…,直到找出一个不冲突 ...

  2. Centos 7 编译nginx 1.14.0

    步骤一:下载nginx安装包 wget https://nginx.org/download/nginx-1.14.0.tar.gz 步骤二:安装nginx依赖包 yum install -y gcc ...

  3. 【EL&JSTL】学习笔记

    一.EL表达式(形式:${ }) 1. 1 EL运算符 算术运算符: +.-.*./.% 示例 结果 ${1+1} 2 ${1-1} 0 ${1*3} 3 ${3/2} 1.5 ${5%3} 2 关系 ...

  4. hdu1394(Minimum Inversion Number)线段树

    明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...

  5. python_字符串类型

    1.在python中用单引号' ',双引号'' '',三引号'''  ''' 标注字符串类型. >>> name = "Alex Li" #双引号 >> ...

  6. pwntools使用简介3

    连接 本地process().远程remote().对于remote函数可以接url并且指定端口. IO模块 下面给出了PwnTools中的主要IO函数.这个比较容易跟zio搞混,记住zio是read ...

  7. .NET Core使用log4Net记录日志

    1.引入Nuget包 log4net 2.添加log4Net配置文件 <?xml version="1.0" encoding="utf-8" ?> ...

  8. unittest多线程执行用例

    前言 假设执行一条脚本(.py)用例一分钟,那么100个脚本需要100分钟,当你的用例达到一千条时需要1000分钟,也就是16个多小时... 那么如何并行运行多个.py的脚本,节省时间呢?这就用到多线 ...

  9. 为应用指定多个struts配置文件

    在大部分应用里,随着应用规模的增加,系统中Action的数量也会大量增加,导致struts.xml配置文件变得非常臃肿.为了避免struts.xml文件过于庞大.臃肿,提高struts.xml文件的可 ...

  10. python018 Python3 输入和输出

    Python3 输入和输出在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能.本章节我们将具体介绍 Python 的输入输出. 输出格式美化Python两种输出值的方式: 表达式语句 ...