HDU2444 The Accomodation of Students(染色法判断二分图)

The Accomodation of Students

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


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
 
题意:先判断这些人能不能分成两部分,每部分内的所有人都相互不认识。如果可以分成 则求两部分最多相互认识的对数。
其实这就是二分图的构成,问你能不能构成二分图,如果可以的话就再用匈牙利算法求最大匹配。那么怎么判断二分图呢,我最早想的是种类并查集,后来看了博客发现染色法挺好用的。所谓染色法就是先找一个点染色,然后和他相邻的其他点染成另一种染色,所有点都染成这两种颜色,如果发现有的点是同色且相邻则说明不能构成二分图。其实这种染色法用dfs也能写,形式多样。
再说这道题,这道题有一点我不知道是题意是这样还是后台数据水了,大家可以尝试一下这个数据
7 5
1 2
1 3
4 5
4 6
5 6
题目上的数据该是可以从一个点出发找到所有的店,但是,这组数据不一样,他们从1找不到4,我把代码改了,把所有的点都放进队列,也是可以过得...
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int mapp[205][205];
int used[205],pp[205],n;
int bfs()
{
    queue<int>q;
    int flag=1;
    for(int i=1; i<=n; i++)
    {
        memset(used,0,sizeof(used));
        while(!q.empty()) q.pop();
        q.push(i);
        used[i]=1;
        while(!q.empty())
        {
            int p=q.front();
//            printf("%d ",p);
            q.pop();
            for(int j=1; j<=n; j++)
                if(mapp[p][j])
                {
                    if(!used[j])
                    {
                        if(used[p]==1) used[j]=2;
                        else used[j]=1;
                        q.push(j);
                    }
                    else if(used[j]==used[p]) flag=0;
                }
        }
    }
    if(flag)
        return 1;
    else return 0;
}
int found(int x)
{
    for(int i=1; i<=n; i++)
    {
        if(mapp[x][i]&&!used[i])
        {
            used[i]=1;
            if(!pp[i]||found(pp[i]))
            {
                pp[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int m,x,y;
    while(~scanf("%d%d",&n,&m))
    {
        memset(mapp,0,sizeof(mapp));
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&x,&y);
            mapp[x][y]=mapp[y][x]=1;
        }
        if(n==1)
            printf("No\n");
        else if(!bfs())
            printf("No\n");
        else
        {
            int sum=0;
            memset(pp,0,sizeof(pp));
            for(int i=1; i<=n; i++)
            {
                memset(used,0,sizeof(used));
                if(found(i))sum++;
            }
            printf("%d\n",sum/2);
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值