1. 程式人生 > >二分圖 板題 HDU2444 判斷是否為二分圖&求二分圖最大匹配

二分圖 板題 HDU2444 判斷是否為二分圖&求二分圖最大匹配

The Accomodation of Students

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

 

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

 

染色法判斷是否為二分圖:

無向圖G為二分圖的充分必要條件是:G至少有兩個頂點,且當存在迴路時,其所有迴路的長度均為偶數。迴路就是環路,也就是判斷是否存在奇數環。

判斷二分圖方法:用染色法,把圖中的點染成黑色和白色。

首先取一個點染成白色,然後將其相鄰的點染成黑色,如果發現有相鄰且同色的點,那麼就退出,可知這個圖並非二分圖。

 

求最大匹配,dfs匈牙利演算法回退實現。

 

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int maxn=205;

int n,m;
bool g[maxn][maxn];
bool vst[maxn];
int match[maxn];
int ans;

//求最大匹配
bool dfs(int x)
{
    vst[x]=1;
    for(int i=1;i<=n;++i){
        if(vst[i]||!g[x][i]) continue;
        vst[i]=1;
        if(!match[i]||dfs(match[i])){
            match[i]=x;
            return true;
        }
    }
    return false;
}

//染色法判斷是否為二分圖
/*無向圖G為二分圖的充分必要條件是:G至少有兩個頂點,且當存在迴路時,其所有迴路的長度均為偶數。迴路就是環路,也就是判斷是否存在奇數環。
判斷二分圖方法:用染色法,把圖中的點染成黑色和白色。
首先取一個點染成白色,然後將其相鄰的點染成黑色,如果發現有相鄰且同色的點,那麼就退出,可知這個圖並非二分圖。
*/
bool judge()
{
    int color[maxn];
    queue <int> q;
    memset(color,-1,sizeof(color));
    q.push(1);color[1]=0;
    while(!q.empty()){
        int p=q.front();q.pop();
        for(int i=1;i<=n;++i){
            if(p==i) continue;
            if(g[p][i]){
                if(color[i]==-1){
                    color[i]=color[p]^1;
                    q.push(i);//媽耶漏了這句
                }
                else if(color[i]^color[p]==0){
                    return false;
                }
            }
        }
    }
    return true;
}

int main()
{
    int u,v;

    while(~scanf("%d%d",&n,&m)){
        ans=0;
        memset(g,0,sizeof(g));
        memset(match,0,sizeof(match));
        for(int i=0;i<m;++i){
            scanf("%d%d",&u,&v);
            g[u][v]=g[v][u]=1;
        }
        if(!judge()){
            printf("No\n");
            continue;
        }
        for(int i=1;i<=n;++i){
            memset(vst,0,sizeof(vst));
            if(dfs(i)) ++ans;
        }
        printf("%d\n",(ans+1)/2);
    }

    return 0;
}