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;
}
發佈了131 篇原創文章 · 獲贊 254 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章