B - The Accomodation of Students(二分图匹配)

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

题意:现在有一些学生给你一下朋友关系,先确认能不能把这些人分成两组(组内的人要相互不认识),不能分的话输出No,能分的话,再求出来一对朋友可以住一个房间,最多可以开多少双人房。

先用并查集分组,看看能不能分成两组,不可以就输出No,能分就进行二分匹配。

#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<queue>
typedef long long ll;
using namespace std;
const int N=1e3+10;
const int INF=0x3f3f3f3f3f;
int line[N][N],girl[N],used[N],pre[N],sum[N],fun[N];
int n,m;
void Init()
{
    memset(line,0,sizeof(line));
    memset(girl,0,sizeof(girl));
    memset(fun,0,sizeof(fun));
    memset(sum,0,sizeof(sum));
    for(int i=0; i<=n; i++)
        pre[i]=i;
}
int find(int x)
{
    if(x!=pre[x])
    {
        int i=pre[x];
        pre[x]=find(pre[x]);
        sum[x]=(sum[x]+sum[i])%2;
    }
    return pre[x];
}
bool found(int x)
{
    for(int i=1; i<=n; i++)
    {
        if(line[x][i]&&!used[i])
        {
            used[i]=1;
            if(girl[i]==0||found(girl[i]))
            {
                girl[i]=x;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{

    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int x,y;
        int nx=0;
        Init();
        int flag=0;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&x,&y);
            line[x][y]=line[y][x]=1;
            int fx=find(x);
            int fy=find(y);
            if(fx==fy&&sum[x]==sum[y])
                flag=1;
            else
            {
                pre[fx]=fy;
                sum[fx]=(sum[x]+sum[y]+1)%2;
            }

        }
        if(flag)
            cout<<"No"<<endl;
        else
        {
            for(int i=1;i<=n;i++)
            {
                if(sum[i]==0)
                    fun[nx++]=i;
            }
            int ans=0;
            for(int i=0; i<nx; i++)
            {
                memset(used,0,sizeof(used));
                if(found(fun[i]))
                    ans++;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值