HDU 5285 wyh2000 and pupil (二分图 bfs染色)

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<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
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<int>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;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值