hdu 2444 The Accomodation of Students(二分图的判断+匈牙利算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2444

The Accomodation of Students

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


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
 

Source
2008 Asia Harbin Regional Contest Online
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  2438 2443 2442 2441 2440 

题目大意:就是将学生两两分成一组,要组内的人互相不认识。如果可以实现的话就输出最大的组数,否则输出No。
解题思路:运用“邻点填色法”来判断是否是二分图,0和1的染色,先选择一个点进行染色,在将其相邻的点是否染色,若未染色,则将其染成和当前点不同的颜色,若已经染色,开始bfs进行搜索,如果相邻的两个点颜色相同,那么就不是二分图。如果不同就继续判断。

详见代码。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

bool Map[210][210];
bool vis[210];//是否访问过
int ok[210];//表示链接好
int judge[210];//判断二分图时 0-1表
int n,m;

bool Bfs()
{
    queue<int>q;
    q.push(1);//将第一个点先放进队列里面
    //vis[1]=1;
    for (int i=1; i<=n; i++)//先全部初始化为-1
        judge[i]=-1;
    judge[1]=0;//先将第一个点标记为0
    while(!q.empty())//将所有的点都扫描一遍
    {
        int s=q.front();
        q.pop();
        for(int i=1;i<=n;i++)
        {
            if(Map[s][i]==1)
            {
                if(judge[i]==-1)//判断是否染色过
                {
                    judge[i]=(judge[s]+1)%2;//如果没染过,那就染成与当前点不同的颜色
                    q.push(i);
                }
                else
                {
                    if(judge[i]==judge[s])//如果染过色,并且与当前点颜色相同,那么就不是二分图
                        return false;
                }
            }
        }
    }
    return true;
}
/*bool bfs()//数组模拟队列方式
{
    int v,start=0,end=1;
    queue[0]=1;
    for (int i=0; i<=n; i++)
        judge[i]=-1;
    v=queue[start];
    judge[1]=0;
    memset(vis,0,sizeof(vis));

    while (start<end)
    {
        v=queue[start];
        for (int i=1; i<=n; i++)
        {
            if (Map[v][i])
            {
                if (judge[i]==-1)
                {
                    judge[i] = (judge[v]+1)%2;
                    queue[end++] = i;
                }
                else
                {
                    if(judge[i] == judge[v])
                        return false;
                }
            }
        }
        start++;
    }
    return true;
}
*/

bool Find(int x)
{
    for (int i=1; i<=n; i++)
    {
        if (Map[x][i]==1&&!vis[i])
        {
            vis[i]=1;
            if (!ok[i])
            {
                ok[i]=x;
                return true;
            }
            else
            {
                if (Find(ok[i])==true)
                {
                    ok[i]=x;
                    return true;
                }
            }
        }
    }
    return false;
}

int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        int a,b;
        memset(Map,0,sizeof(Map));
        for (int i=1; i<=m; i++)
        {
            scanf("%d%d",&a,&b);
            Map[a][b]=Map[b][a]=1;
        }
        if(!Bfs())
        {
            printf("No\n");
            continue;
        }

        //the maximum number of pair
        int num = 0;
        memset(ok,0,sizeof(ok));
        for(int i=1; i<=n; i++)
        {
            memset(vis,0,sizeof(vis));
            if(Find(i)) num++;
        }
        printf("%d\n",num/2);
    }
    return 0;
}


 

你可能感兴趣的:(hdu 2444 The Accomodation of Students(二分图的判断+匈牙利算法))