HDU 2444 The Accomodation of Students 【二分图匹配】【匈牙利算法】【bfs判断二分图】

26 篇文章 0 订阅
2 篇文章 0 订阅

The Accomodation of Students

 

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8147    Accepted Submission(s): 3605

 

 

Problem Description

There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.

Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.

Calculate the maximum number of pairs that can be arranged into these double rooms.

 

 

Input

For each data set:
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.

Proceed to the end of file.
 

 

 

Output

If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.

 

 

Sample Input

 

4 41 21 31 42 36 51 21 31 42 53 6

 

Sample Output

 

No3

 

 

Source

2008 Asia Harbin Regional Contest Online

题意:
有n个学生。其中有些人相互认识,有些人相互之间不认识。现在有m对认识关系。问你能否把这n个人分成两个组。一个组中没有相互认识的人。如果不能做到的话,输出NO。如果可以的话,把这些人分到一些房间中,只有相互认识的人能分到一个房间里。问你需要多少房间。
思路:
首先要判断该图是否为二分图。当该图为二分图时,才能分为两个组。当判断为二分图时,我们需要求出该二分图的最大匹配数目。得出的结果除2就得出了需要房间数。

二分图的判定:
在判定一个图是否为二分图的时候只需要进行判断图中是否存在奇数长度的回路
常用的办法是基于BFS的相邻染色法 即对任意一个点进行BFS如果uv之间有一条边 我们从u访问到v 如果v未被染色 则v将被染成和u相对的染色(比如1和0)
如果相邻节点(u和v)是相同颜色 则存在奇回路

 

#include<bits/stdc++.h>
using namespace std;
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN = 210;
int link[MAXN];
int vis[MAXN];
int jug[MAXN];
int n,m;
int ai,bi;
int st,ed;
int ans = 0;
vector<int>par[MAXN];
bool bfs()///判断二分图
{
    M(jug,-1);
    queue<int>q;
    q.push(1);
    jug[1] = 0;
    while(!q.empty())
    {
        int temp = q.front();
        for(int i =0;i<par[temp].size();i++)
        {
            if(jug[par[temp][i]]==-1)
            {
                jug[par[temp][i]]  = (jug[temp]+1)%2;///染成不同颜色
                q.push(par[temp][i]);
            }
            else
            {
                if(jug[par[temp][i]]==jug[temp])///相邻节点颜色相同
                {
                    return false;
                }
            }
        }
        q.pop();
    }
    return true;
}
bool FIND(int u)///匈牙利算法
{
    for(int i=0;i<par[u].size();i++)
    {
        int temp = par[u][i];
        if(vis[temp]) continue;
        vis[temp] = 1;
        if(link[temp]==-1||FIND(link[temp]))
        {
            link[temp] = u;
            return true;
        }
    }
    return false;
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        ans=0;
        M(link,-1);
        for(int i=1;i<=MAXN;i++) par[i].clear();///注意清空
        for(int i=0;i<m;i++)
        {
            scanf("%d %d",&ai,&bi);
            par[ai].push_back(bi);///两个关系都要计入邻接表
            par[bi].push_back(ai);
        }
        if(!bfs()) {printf("No\n");continue;}

        for(int i=1;i<=n;i++)
        {
            M(vis,0);
            if(FIND(i)) ans++;
        }
        printf("%d\n",ans/2);///结果要除2

    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值