首页 > 代码库 > HDU 2444 The Accomodation of Students(二分图判定+最大匹配)

HDU 2444 The Accomodation of Students(二分图判定+最大匹配)

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
 
题意:n个人m种关系。问能不能将他们分成两组。每组的人互不认识。
二分图判定采用染色法:对于无向图如果没有还就一定可以二分。
染色法:相邻的染成不同颜色,如果发现相邻且颜色相同者判定不是二分图。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int maxn=220;
int mp[maxn][maxn];
int vis[maxn],col[maxn];
int used[maxn],link[maxn];
int n,m,flag;
bool bfs(int x)
{
    queue<int>q;
    q.push(x);
    col[x]=1;
    while(!q.empty())
    {
        int xx=q.front();
        q.pop();
        REPF(i,1,n)
        {
            if(mp[xx][i]&&col[i]==-1)
            {
                q.push(i);
                col[i]=!col[xx];
            }
            if(mp[xx][i]&&col[i]==col[xx])
                return false;
        }
    }
    return true;
}
bool ok()
{
    flag=0;
    REPF(i,1,n)
    {
        if(col[i]==-1&&!bfs(i))
        {
            flag=1;
            break;
        }
    }
    if(flag) return false;
    return true;
}
bool dfs(int x)
{
    REPF(i,1,n)
    {
        if(mp[x][i]&&!used[i])
        {
            used[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}
void work()
{
    CLEAR(link,-1);
    int res=0;
    REPF(i,1,n)
    {
        CLEAR(used,0);
        if(dfs(i)) res++;
    }
    printf("%d\n",res>>1);
}
int main()
{
    int x,y;
    while(~scanf("%d%d",&n,&m))
    {
        CLEAR(col,-1);
        CLEAR(mp,0);
        REP(i,m)
        {
            scanf("%d%d",&x,&y);
            mp[x][y]=mp[y][x]=1;
        }
        if(!ok())  puts("No");
        else  work();
    }
    return 0;
}


HDU 2444 The Accomodation of Students(二分图判定+最大匹配)