HDU 5285 wyh2000 and pupil(dfs或种类并查集)
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
Wyh2000 has n pupils.Id of them are from 1
to n
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
b
doesn't know a
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".
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
that x
don't know y
and y
don't know x
pair (x,y)
will only appear once.
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
//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或种类并查集)的更多相关文章
- 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 ...
- hdu 1182 A Bug's Life(简单种类并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...
- HDU 5285 wyh2000 and pupil 判二分图+贪心
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5285 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- hdu 5313 Bipartite Graph(dfs染色 或者 并查集)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)
题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...
- HDU 5285 wyh2000 and pupil (二分图着色)
题意: 共有n个小学生,编号为1−n.将所有小学生分成2组,每组都至少有1个人.但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a.Wyh2000希望每组中的小学生都互相认识.而且第一组 ...
- HDU 5285 wyh2000 and pupil
题意:有一群人,已知某两人之间互相不认识,要把这群人分成两部分,每部分至少一人,且在每部分内没有人互不认识. 解法:图染色.某场bestcoder第二题……看完题觉得是个二分图……完全不会二分图什么的 ...
- hdu 5285 wyh2000 and pupil(二染色)
第一次用vector解得题.值得纪念,这道题是二染色问题,我用bfs解得.就是染色,推断,计数问题,其 实挺简单的,就是得判一下特殊情况,当n<2的时候就不能有解,由于题目要求每一个组至少有一个 ...
- 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[ ...
随机推荐
- Elasticsearch document深度剖析
1. 针对Elasticsearch并发冲突问题,ES内部是如何解决的? 1)ES内部是线程异步并发修改的,是基于_version版本号进行乐观锁并发控制的: 2)若后修改的先到了,那么修改后版本发生 ...
- Yii1 用commandBuilder方法往数据表中插入多条记录
$builder = Yii::app()->db->schema->commandBuilder; // 创建builder对象 $command = $builder->c ...
- Openjudge-4132-四则运算表达式求值
这一题我们可以通过递归求解,首先我们可以把一个表达式分为三部分,分别是: (1)表达式 :项.加减 (2)项:因子.乘除 (3)因子:数.()表达式 这三项构成了递归的关系,我们可以看到,要求一个表达 ...
- MySQL Utilities管理工具
前提: 1.安装MySQL Utilities工具 2.复制my_print_defaults命令至/usr/bin下或写入环境变量. 卸载方式: python ./setup.py clean -- ...
- 【HIHOCODER1527 】 快速乘法
描述 在写代码时,我们经常要用到类似 x × a 这样的语句( a 是常数).众所周知,计算机进行乘法运算是非常慢的,所以我们需要用一些加法.减法和左移的组合来实现乘一个常数这个操作.具体来讲, 我们 ...
- 数据结构实验4:C++实现循环队列
实验4 4.1 实验目的 熟练掌握队列的顺序存储结构和链式存储结构. 熟练掌握队列的有关算法设计,并在循环顺序队列和链队列上实现. 根据具体给定的需求,合理设计并实现相关结构和算法. 4.2 实验要求 ...
- OO博客作业
第一次多项式的作业感觉还行,同时用c和java写的话也算是一个从c到java的过渡,也算是有了对 java的初步认识,之后的电梯作业出血了一些小BUG,比如有些情况考虑不完善之类的,也算是对面向对象有 ...
- Task(TPL)简单的实现Winform(WPF)异步
很多时候,我们要实现Winform异步操作,你可以用传统的方法,但个人感觉代码不好理解,而且使用真有点不舒服.也可以用Task来实现,Task(.net4.0新添加的对象)其实就是对线程池线程的一个封 ...
- @locked_cached_property ---flask.helpers模块
源码: class locked_cached_property(object): """A decorator that converts a function int ...
- BigTable
Bigtable发布于2006年,启发了无数的NoSQL数据库,比如:Cassandra.HBase等等. Cassandra架构中有一半是模仿Bigtable,包括了数据模型.SSTables以及提 ...