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[ ...
随机推荐
- JavaSE-26 Swing
学习要点 关于Swing Swing容器组件 Swing布局管理器 Swing组件 Swing菜单组件 关于Swing Swing和AWT都是java开发图形用户界面的工具包. AWT:早期Java版 ...
- 《BUG创造队》作业8:软件测试与Alpha冲刺(第四天)
项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 BUG创造队 作业学习目标 (1)掌握软件测试基础技术.(2)学习 ...
- UIWebView与js那些事
UIWebView是IOS SDK中渲染网面的控件,在显示网页的时候,我们可以hack网页然后显示想显示的内容.其中就要用到javascript的知识,而UIWebView与javascript交互的 ...
- centeros 6 远程升级ssl ssh 的shell脚本
变量说明 SSL_N=openssl-1.0.2p #ssl 版本SSH_N=openssh-7.9p1 #ssh 版本ZLIB_N=zlib-1.2.11 # zlib 版本 脚本分为两个,因为升级 ...
- Linux离线安装redis集群
一.应用场景介绍 本文主要是介绍Redis集群在Linux环境下的安装讲解,联网环境安装较为简单,这里只说脱机的Linux环境下是如何安装的.因为大多数时候,公司的生产环境是在内网环境下,无外网,服务 ...
- 第一章:systemverilog简介
1.为何要学systemverilog ..... 2.systemverilog起源 ..... 3.systemverilog标准历程 systemverilog3.0 for 综合 system ...
- Restful API(转)
原文链接 http://www.ruanyifeng.com/blog/2014/05/restful_api.html 一.协议 API与用户的通信协议,总是使用HTTPs协议. 二.域名 应该尽量 ...
- 基于flask的网页聊天室(三)
基于flask的网页聊天室(三) 前言 继续上一次的内容,今天完成了csrf防御的添加,用户头像的存储以及用户的登录状态 具体内容 首先是添加csrf的防御,为整个app添加防御: from flas ...
- 【BZOJ 2761】 不重复数字 (哈希算法)
链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2761 Description 给出N个数,要求把其中重复的去掉,只保留第一次出现的数. 例如, ...
- POJ 1463 Strategic game(树形DP入门)
题意: 给定一棵树, 问最少要占据多少个点才能守护所有边 分析: 树形DP枚举每个点放与不放 树形DP: #include<cstdio> #include<iostream> ...