wyh2000 and pupil
wyh2000 and pupil
青年理论计算机科学家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
/*
Author: 2486
Memory: 7448 KB Time: 592 MS
Language: G++ Result: Accepted
VJ RunId: 4055769 Real RunId: 14058285
Public: No Yes
*/
/*
假设a不认识b,那么在a,b间连一条边,这样有解当且仅当这张图是二分图。
由于可能有多个二分图。而题目要求第一组的人尽可能多,所以贪心的选择就可以。 要注意m=0的情况。 */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=100000+5;
vector<int>G[maxn];
int col[maxn],T;
int n,m,a,b,acnt,bcnt;
void init(int x) {
for(int i=1; i<=x; i++) {
G[i].clear();
}
}
bool bfs(int u) {
queue<int>k;
k.push(u);
col[u]=1;
while(!k.empty()) {
int s=k.front();
if(col[s]==1)acnt++;
else bcnt++;
k.pop();
for(int i=0; i<G[s].size(); i++) {
if(col[G[s][i]]==-1) {
col[G[s][i]]=!col[s];
k.push(G[s][i]);
continue;
}
if(col[G[s][i]]==col[s])return false;
}
}
return true;
}
void slove() {
memset(col,-1,sizeof(col));
bool flag=false;
int Max=0;
for(int i=1; i<=n; i++) {
acnt=0,bcnt=0;
if(col[i]==-1&&!bfs(i)) {
flag=true;
break;
}
Max+=max(acnt,bcnt);//必须这么做,由于这里面为1的或者为0的不一定就是同一阵营。 }
if(flag)printf("Poor wyh\n");
else printf("%d %d\n",Max,n-Max);
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
init(n);
for(int i=0; i<m; i++) {
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
if(n<2) {//题目要求
printf("Poor wyh\n");
continue;
}
if(m==0) {//题目要求
printf("%d 1\n",n-1);
continue;
}
slove();
}
return 0;
}
wyh2000 and pupil的更多相关文章
- HDU 5285 wyh2000 and pupil 判二分图+贪心
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5285 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- HDU 5285 wyh2000 and pupil(dfs或种类并查集)
wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Other ...
- 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil
题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...
- HDU 5285:wyh2000 and pupil
wyh2000 and pupil Accepts: 93 Submissions: 925 Time Limit: 3000/1500 MS (Java/Others) Memory Lim ...
- 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 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)
题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...
- ACM: HDU 5285 wyh2000 and pupil-二分图判定
HDU 5285 wyh2000 and pupil Time Limit:1500MS Memory Limit:65536KB 64bit IO Format:%I64d &a ...
随机推荐
- H.264 Profile-level-id
基于SIP协议的VOIP通信,该字段通常位于视频协商sdp报文中,如: video RTP/AVP rtpmap: H264/ fmtp: profile-level-id=42801E; packe ...
- 解决like '%字符串%'时索引不被使用的方法
解决like '%字符串%'时索引不被使用的方法 分步阅读 解决like '%字符串%'时索引不被使用的方法,如果like以通配符开头('%abc')时索引会失效会变成全表扫描的操作. 工具/原料 ...
- bootshiro---开源的后台管理框架--基于springboot2+ shiro+jwt的真正rest api资源无状态认证权限管理框架,开发人员无需关注权限问题,后端开发完api,前端页面配置即可
https://gitee.com/tomsun28/bootshiro
- BNUOJ 4215 最长公共连续子序列
最长公共连续子序列 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class ...
- CF802D
D. Marmots (easy) time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- TOJ 2596: Music Notes
2596: Music Notes Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Submit: 3 ...
- zoj2112 主席树动态第k大 ( 参考资料链接)
参考链接: http://blog.csdn.net/acm_cxlove/article/details/8565309 http://www.cnblogs.com/Rlemon/archive/ ...
- hdu 1848 sg——dfs&&打表双实现
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- UITableView滑动动画+FPSLabel
主要使用了tableView的代理方法 行将要显示的时候 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableView ...
- 刷题总结——mayan游戏(NOIP2011提高组day2T3)
题目: 题目背景 NOIP2011提高组 DAY1 试题. 题目描述 Mayan puzzle 是最近流行起来的一个游戏.游戏界面是一个 7 行 5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即 ...