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. 16.04 下修改 ssh 默认端口

    打开/etc/ssh/ssh_config,在Port指令下追加新的端口设置: Port 8888 即允许通过端口 8888 进行 ssh 访问. 打开/etc/ssh/sshd_config,进行同 ...

  2. 光猫&路由器网络配置

    前期准备:电脑(工业电脑).网线.光猫.路由器 1.检查连接光猫后能否正常上网:把网线两头的水晶头,一头插在光猫上的千兆口,一头插在电脑(工业电脑)的网口上,看电脑能否正常上网: 可以正常上网:说明光 ...

  3. word break和word wrap

    默认情况下,如果同一行中某个单词太长了,它就会被默认移动到下一行去: word break(normal | break-all | keep-all):表示断词的方式 word wrap(norma ...

  4. 队列的JS实现及广度优先搜索(BFS)的实现

    队列是先进先出(FIFO)的数据结构,插入操作叫做入队,只能添加在队列的末尾:删除操作叫做出队,只能移除第一个元素.在JS中,用数组可以很简单的实现队列.JavaScript实现排序算法 functi ...

  5. React深入 - 手写redux api

    简介: 手写实现redux基础api createStore( )和store相关方法 api回顾: createStore(reducer, [preloadedState], enhancer) ...

  6. 第十三章:MFC库与Windows程序开发概述

    主要内容: 1.Windows程序的基本结构 2.MFC库简介 3.使用Visual C++开发Windows程序 具体内容略

  7. 【04】如何确定ruby安装好

        [04]如何确定ruby安装好     命令行里输入 ruby -v 如果正确输出了 ruby 版本号,就OK了       是不是在Windows平台安装的?如果是,先按照楼上说得打开命令行 ...

  8. cherrypy & gevent patch

    给cherrypy 打gevent WSGIServer的patch 1. patch Serving 类 2. 关闭python的原生WSGIServer 具体使用例子参考 我的开源项目  http ...

  9. XHR2:js异步上传

    http://dev.opera.com/articles/xhr2/ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...

  10. [Istioc]Istio部署sock-shop时rabbitmq出现CrashLoopBackOff

    因Istio官网自带的bookinfo服务依赖关系较少,因此想部署sock-shop进行进一步的实验. kubectl apply -f <(istioctl kube-inject -f so ...