hdu 5285 wyh2000 and pupil(判断是否是二分图+贪心)

wyh2000 and pupil

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 787    Accepted Submission(s): 259


Problem Description
Young theoretical computer scientist wyh2000 is teaching his pupils.

Wyh2000 has n pupils.Id of them are from  1  to  n .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  a  doesn't know  b ,then  b  doesn't know  a ).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  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 < y) ,indicates that  x  don't know  y  and  y  don't know  x ,the pair  (x,y)  will only appear once.

T10,0n,m100000
 

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
 


n个人里面有m组人互相不认识 需要将这些人分为两组 每组里面的人互相之间都会认识 且每组至少一个人


就是将互不认识的两个人之间连一条边建图 判断这个图是不是二分图

染色来判断图是否是二分图 因为第一组人数要最多 所以还需要贪心 而且要确保每组至少一个人!

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 100010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

int n,m;
vector<int> vec[MAXN];
int col[MAXN];
int num[3];

bool dfs(int u,int color)
{
    col[u]=color;
    num[color]++;
    int len=vec[u].size();
    for(int i=0;i<len;i++)
    {
        int v=vec[u][i];
        if(col[v])
        {
            if(col[v]!=3-color)
                return 0;
        }
        else
        {
            if(!dfs(v,3-color))
                return 0;
        }
    }
    return 1;
}

void solve()
{
    int cnt1=0,cnt2=0;
    for(int i=1;i<=n;i++)
    {
        if(!col[i])
        {
            int c1=num[1],c2=num[2];
            if(!dfs(i,1))
            {
                puts("Poor wyh");
                return;
            }
            cnt1+=max(num[1]-c1,num[2]-c2);
            cnt2+=min(num[1]-c1,num[2]-c2);
            num[1]+=c1; num[2]+=c2;
        }
    }
    if(cnt2==0)
    {
        cnt1--; cnt2++;
    }
    printf("%d %d\n",cnt1,cnt2);
}

int main()
{
    //fread;
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)   vec[i].clear();
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
        if(n<=1)//每个组都至少一人
        {
            puts("Poor wyh");
            continue;
        }
        MEM(col,0); MEM(num,0);
        solve();
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值