HDU 2444 The Accomodation of Students (二分图判定+最大匹配

本文探讨了一个有趣的学生住宿分配问题:如何将互相认识的学生分为两组并尽可能多地安排他们入住双人间。通过图论中的二分图概念和匈牙利算法解决这一挑战。

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

The Accomodation of Students

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

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

题解:

判断这些人是否能分成两部分,每个部分的人若有互相认识的人则no,例如:1认识2和3,若2和3认识,则NO,
若分成的两部分是YES,则求出最大匹配数(认识的人才能匹配)

AC代码

#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3;
int color[MAXN];
int G[MAXN][MAXN];
int used[MAXN];
int linker[MAXN];
bool flag;
int vN;
int m;
bool bfs(int s)
{
    color[s] = 1;
    queue<int> q;
    q.push(s);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(int v = 1; v <= vN; v++) {
            if(G[u][v] && color[v]==0) {
                q.push(v);
                color[v] = -color[u];
            }
            if(G[u][v] && color[v]==color[u]) return false;
        }
    }
    return true;
}
void check()
{
    for(int v = 1; v <= vN; v++) {
        if(color[v]==0 && !bfs(v)) {
            flag = true; return;
        }
    }
}
bool dfs(int u)
{
    for(int v = 1; v <= vN; v++) {
        if(G[u][v] && !used[v]) {
            used[v] = true;
            if(linker[v]==-1 || dfs(linker[v])) {
                linker[v] = u;
                return true;
            }
        }
    }
return false;
}
int hungary()
{
    int res = 0;
    CLR(linker,-1);
    for(int i = 1; i <= vN; i++) {
        CLR(used,false);
        if(dfs(i)) res++;
    }
    return res;
}
int main()
{
    int m;
    while(~scanf("%d%d",&vN,&m)) {
        CLR(G,0); CLR(color,0);
        for(int i = 0; i < m; i++) {
            int u, v;
            scanf("%d%d",&u,&v);
            G[u][v] = G[v][u] = 1;
        }
        flag = false;
        check();
        if(flag) {
            puts("No");
        }
        else {
            printf("%d\n",hungary()/2);
        }

    }
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值