HDU5285:wyh2000 and pupil(二分图染色 & 贪心)

本文介绍了一种基于二分图染色的经典算法,用于解决人员分组问题,确保同一组内的成员相互认识。通过DFS深度优先搜索进行图的遍历,并标记节点颜色来判断是否能构成二分图。

wyh2000 and pupil

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


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
 

Source
题意:N个人,有M对人互不认识,现在要将他们分成A,B两堆人,要求两堆人各至少有一个人,且同一堆里面的人要互相认识,A堆的人数要尽量多,输出两堆的人数。

思路:基础的二分图染色,首先是二分图的判定,出现奇数环就表示无法分配,否则取中max(黑点数,白点数),要每堆至少有一人,如果答案A堆有N个人,说明必定所有点都是独立点,即M=0。此时分1个人给B堆就行了。

# include <iostream>
# include <cstdio>
# include <cstring>
# include <vector>
# define pb push_back
using namespace std;
const int maxn = 1e5+3;
vector<int>g[maxn];
int col[maxn], cnt, tot, ans, flag;
void dfs(int cur, int co)
{
    if(flag) return;
    if(co == 1) ++cnt; ++tot;
    col[cur] = co;
    for(int i=0; i<g[cur].size(); ++i)
    {
        if(col[g[cur][i]] == co)
        {
            flag = 1;
            return;
        }
        else if(col[g[cur][i]] == -1)
            dfs(g[cur][i], co^1);
    }
}
int main()
{
    int T, x, y, n, m;
    scanf("%d",&T);
    while(T--)
    {
        cnt = ans = flag = 0;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; ++i) g[i].clear(), col[i] = -1;
        while(m--)
        {
            scanf("%d%d",&x,&y);
            g[x].pb(y);
            g[y].pb(x);
        }
        if(n == 0 || n == 1)
        {
            puts("Poor wyh");
            continue;
        }
        for(int i=1; i<=n; ++i)
        {
            if(col[i] != -1) continue;
            tot=cnt=0;
            dfs(i, 0);
            if(flag) break;
            ans += max(cnt, tot-cnt);
        }
        if(flag) puts("Poor wyh");
        else
        {
            if(ans != n) printf("%d %d\n",ans,n-ans);
            else printf("%d %d\n",ans-1, 1);
        }
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值