The Accomodation of Students (判断是否二分图+二分图匹配)

本文探讨了如何通过二分图匹配算法解决学生分组问题,确保每组内的学生互不认识,同时最大化配对数量。文章详细介绍了使用点染法判断二分图,并运用匈牙利算法求解最大匹配问题,提供了完整的代码实现。

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

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

题目大义就是给你一堆人 并且a认识b b也认识a(无向图)而a认识b b认识c a不一定认识c(非传递)

然后问你是否可以把这些人分成两堆 这两堆里面的人互相谁也不认识 如果可以的话 让你把两个互相认识的人两两配对 找这个图的最大匹配 

我用的点染法判断二分图(网上找了个错板子,然后用了自己之前做的cf上判断二分图的一个代码)

(判断二分图还可以用冰茶几...并查集

然后匈牙利来求最大匹配

//
// Created by xingchaoyue on 2019/4/12.
//

//
// Created by xingchaoyue on 2019/4/8.
//

#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
using namespace std;
vector<int>v[200000+100];

int linker[200000+100];
int used[505];
int n,m;
int col[200000+100];
int flag = 1;
void init(){
    for(int i =0;i<=200000;++i){
        v[i].clear();
    }
    memset(linker,-1,sizeof(linker));
    memset(used,0,sizeof(used));
    memset(col,0,sizeof(col));
    flag = 1;
}

int dfs(int u){
    for(int i =0;i<v[u].size();++i){
        int pos = v[u][i];
        if(!used[pos]){
            used[pos]=true;
            if(linker[pos]==-1||dfs(linker[pos])){
                linker[pos] = u;
                return 1;
            }

        }
    }
    return 0;
}

int hungry(){
    int res = 0;
    memset(linker,-1,sizeof(linker));
    for(int i = 1;i<=n;++i){
       // cout<<":"<<i<<endl;
        memset(used,0,sizeof(used));
        if(dfs(i)){
            res++;
        }
    }
    return res;
}
void prin(int x,int color){

    col[x] = color;
    for(int i = 0;i<v[x].size();++i){
        int pos = v[x][i];
        if(!col[pos]){
            prin(pos,-color);
        }
        else{
            if(col[pos]==col[x])
                flag = 1;
        }
    }
}

void prin1(int n,int c){
    if(col[n]){
        if(col[n]==c)
            return;
        else{
            flag = 0;
            return;
        }
    }
    else{
        col[n] = c;
    }

    for(int i =0;i<v[n].size();++i){
        int nex = v[n][i];

        if(c==1)
            prin1(nex,2);
        else
            prin1(nex,1);
    }

}
int edge[200000+100];
int main(){
    while(~scanf("%d%d",&n,&m)){
        init();
        for(int i = 0;i<m;++i){
            int a,b;
            scanf("%d%d",&a,&b);
            v[a].push_back(b);
            v[b].push_back(a);
            edge[i] = a;
        }

        for(int i =1;i<=n;++i){
            if(col[i]==0){
                prin1(i,1);
                //cout<<":"<<i<<endl;
            }
        }

        if(!flag)
            cout<<"No"<<endl;
        else{
            cout<<hungry()/2<<endl;
        }

    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值