The Accomodation of Students HDU - 2444(染色法判断是否为二分图&&二分图匹配)

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
题目大意:

有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识。如果可以分成两部分,就算出房间最多需要多少间,否则就输出No。
能否分成两部分,就是这个图能否看成是一个二分图。通常我们用染色法判断这张图是否为一个二分图。取没有标记过的点dfs,如果当前的点染色了,但是它与它父节点被染的颜色相同,说明就不是一个二分图,直接输出No即可。
是二分图之后,就直接匈牙利算法求最大匹配就行。
代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1111;

bool book[N];
int match[N],a[N];
vector <int> close[N];
bool f(int x)
{
    int m=close[x].size();
    for(int i=0 ; i < m ; i++)
    {
        int g=close[x][i];
        if(book[g]==false)
        {
            book[g]=true;
            if(match[g]==-1||f(match[g]))
            {
                match[g]=x;
                return true;
            }
        }
    }
    return false;
}
//用染色法判断是否为二分图,若相邻点同色则不是二分图
bool dfs(int x,int c)
{
    a[x]=c;
    int m=close[x].size();
    for(int i=0; i<m; i++)
    {
        int g=close[x][i];
        if(a[g]==c)
            return false;
        if(a[g]==0 && !dfs(g,-c))
            return false;
 
    }
    return true;
}
int main()
{
    int n,m;
    int x,y;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(match,-1,sizeof(match));
        memset(a,0,sizeof(a));
        bool flag=true;
        int sum=0;
        for(int i=0; i<=n; i++)    close[i].clear();
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&x,&y); //无向图要双向存边
            close[x].push_back(y);
            close[y].push_back(x);
        }
        //判断是否为二分图
        for(int i=1; i<=n; i++)
        {
            if(a[i]==0)
            {
                if(flag)
                    flag=dfs(i,1);
            }
        }
        if(flag==false)
        {
            printf("No\n");
            continue;
        }
        for(int i=1; i<=n; i++)
        {
            memset(book,false,sizeof(book));
            if(f(i)) sum++;
        }
        printf("%d\n",sum/2);//无向图匹配数取二分之一
    }
    return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值