HDU 2444.The Accomodation of Students【二分圖判斷+最大匹配】【5月4】

The Accomodation of Students

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


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

染色法判斷二分圖,如果是二分圖,則輸出最大匹配:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX = 205;
int n, m, ans,  vis[MAX], Map[MAX][MAX], col[MAX], link[MAX];
int x, y;
bool flag;
void check(int node, int color)
{
    if(!col[node] || col[node] == color)
    {
        col[node] = color;
        for(int i = 1;i <= n; ++i)
        {
            if(!vis[i] && Map[node][i])
            {
                vis[i] = 1;
                check(i, -color);
                vis[i] = 0;
            }
        }
    }
    else
    {
        flag = false;
        return;
    }
}
bool pp(int node)
{
    for(int i = 1;i <= n; ++i)
    {
        if(!vis[i] && Map[node][i])
        {
            vis[i] = 1;
            if(!link[i] || pp(link[i]))
            {
                link[i] = node;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    while(scanf("%d %d", &n, &m) != EOF)
    {
        ans = 0;
        memset(col, 0, sizeof(col));
        memset(vis, 0, sizeof(vis));
        memset(Map, 0, sizeof(Map));
        memset(link, 0, sizeof(link));
        for(int i = 0;i < m; ++i)
        {
            scanf("%d %d", &x, &y);
            Map[x][y] = 1;
            //Map[y][x] = 1;只需從二分圖一邊的集合匹配即可
        }
        flag = true;
        for(int i = 1;i <= n; ++i)
        {
            if(flag && !col[i])
            {
                vis[i] = 1;
                check(i, -1);
                vis[i] = 0;
            }
        }
        if(!flag) cout <<"No"<< endl;
        else
        {
            for(int i = 1;i <= n; ++i)
            {
                memset(vis, 0, sizeof(vis));
                if(pp(i)) ans ++;
            }
            cout << ans << endl;
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章