HDU 2444 The Accomodation of Students (二分图的最大匹配)

探讨如何将相互认识的学生分组并安排双人间,通过构建图论模型解决最大匹配问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Accomodation of Students

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


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 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
 

Sample Output
No
3

 

/*
  交叉染色判断图是否为二分图,如果遇到相连的点是一样
  的颜色,则说明该图不是二分图。
  最后就是求出最大匹配。
  PS:感觉题目有点坑,之前是说要把学生分成两个组,每个组里
  的任何两个人都不认识。这很容易想到是最大独立集,然后题目
  后面又说只有彼此认识的一对学生,才能有资格进行分组(既把
  他们分到不同组),所以最后求的其实是最大匹配,既找出最多
  组认识的学生。
  
  15MS	312K
  */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define SIZE 205

using namespace std;

int N,M,ans,st;
bool cnt[SIZE][SIZE],vis[SIZE],used[SIZE];
int color[SIZE];
int link[SIZE];

bool check()
{
    for(int i=1; i<=N; i++)
    {
        used[i] = true;
        for(int j=1; j<=N; j++)
        {
            if(used[j] && cnt[i][j] && color[i] == color[j])
                return false;
            if(!used[j] && cnt[i][j])
            {
                color[j] = -color[i];
                used[j] = true;
            }
        }
    }
    return true;
}

bool dfs(int l)
{
    for(int r=1; r<=N; r++)
    {
        if(!vis[r] && cnt[l][r])
        {
            vis[r] = true;
            if(link[r] == -1 || dfs(link[r]))
            {
                link[r] = l;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    while(~scanf("%d%d",&N,&M))
    {
        for(int i=1; i<=N; i++)
        {
            link[i] = -1;
            color[i] = 1;
            used[i] = false;
            for(int j=1; j<=N; j++)
                cnt[i][j] = false;
        }
        for(int i=1; i<=M; i++)
        {
            int s,e;
            scanf("%d%d",&s,&e);
            cnt[s][e] = cnt[e][s] = true;
        }
        if(!check())
            puts("No");
        else
        {
            ans = 0;
            for(int i=1; i<=N; i++)
            {
                memset(vis,0,sizeof(vis));
                if(dfs(i))
                    ans ++;
            }
            printf("%d\n",ans/2);
        }
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值