博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5285 wyh2000 and pupil (二分图 bfs染色)
阅读量:4613 次
发布时间:2019-06-09

本文共 2748 字,大约阅读时间需要 9 分钟。

Young theoretical computer scientist wyh2000 is teaching his pupils.

Wyh2000 has n pupils.Id of them are from 11 to nn.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 aa doesn’t know bb,then bb doesn’t know aa).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 TT indicates the number of test cases.

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

T≤10,0≤n,m≤100000T≤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

题意:给你T组数据,每组数据有一个n和一个m表示有n个小朋友,m对关系,每个关系里的小朋友都互不认识,问能否将这些小朋友分到两个集合中,使得每个集合中的小朋友都互相认识。若不能,则输出“Poor wyh“,若能,则输出两个集合中小朋友的数目,并使其中一个集合中的小朋友尽量多。

*注:必须分到两个集合中,若小朋友们之间都互相认识,那就随便把一个人分到另一个集合中;如果只有1个小朋友或者没有小朋友,也是不合法的。

思路:二分图染色问题,将m对关系中的两个小朋友染成不同的两种颜色,若发生冲突则不合法。

题解:

#include
#include
#include
#include
#include
using namespace std;const int maxn=200000+10;int color[maxn];struct cc{ int from,to;}es[maxn];int first[maxn],nxt[maxn];int tot=0;void build(int ff,int tt){ es[++tot]=(cc){ff,tt}; nxt[tot]=first[ff]; first[ff]=tot;} queue
q;bool flag;int bfs(int x){ int sum1=0,sum2=0; q.push(x); if(color[x]==0) { color[x]=1; sum1++; } while(!q.empty()) { int u=q.front(); q.pop(); for(int i=first[u];i;i=nxt[i]) { int v=es[i].to; if(!color[v]) { if(color[u]==1) { color[v]=2; sum2++; q.push(v); } if(color[u]==2) { color[v]=1; sum1++; q.push(v); } } else { if(color[v]==color[u]) { flag=0; } } } } return max(sum1,sum2);}int main(){ int T; scanf("%d",&T); while(T) { T--; int n,m; scanf("%d%d",&n,&m); flag=1; tot=0; memset(first,0,sizeof(first)); memset(es,0,sizeof(es)); memset(nxt,0,sizeof(nxt)); memset(color,0,sizeof(color)); for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); build(x,y); build(y,x); } int ans=0; for(int i=1;i<=n;i++) { if(!color[i]) { ans+=bfs(i); } } if(!flag||n<=1) { printf("Poor wyh\n"); } else { if(m==0) { printf("%d 1\n",n-1); } else { printf("%d %d\n",ans,n-ans); } } } return 0;}

转载于:https://www.cnblogs.com/-feather/p/7779860.html

你可能感兴趣的文章
exceptionfunction[LeetCode]Permutations
查看>>
xp_cmdshell 命令的开启与关闭,和状态查询
查看>>
Linux sudoers
查看>>
bzoj 4595 激光发生器
查看>>
multi cookie & read bug
查看>>
js时间转换
查看>>
(转载) Android Studio你不知道的调试技巧
查看>>
队列实现霍夫曼树
查看>>
关于微信公众平台测试号配置失败的问题
查看>>
【NOIP2001】统计单词个数
查看>>
linux常用端口
查看>>
异常处理
查看>>
/proc/uptime详解
查看>>
如何建立合适的索引?
查看>>
acwing 651. 逛画展
查看>>
Vijos P1243 生产产品 (单调队列优化DP)
查看>>
iOS常用第三方库 -转
查看>>
Android布局学习
查看>>
jQuery中事件绑定与解绑
查看>>
js原生Ajax的封装与使用
查看>>