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

来源:互联网 发布:怎么使用同花顺软件 编辑:程序博客网 时间:2024/05/30 07:13

题目链接: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): 7104    Accepted Submission(s): 3173

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 41 21 31 42 36 51 21 31 42 53 6
 

Sample Output
No3

题目大意:

有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识。如果可以分成两部分,就算出房间最多需要多少间,否则就输出No。

题目分析:

首先判断是否为二分图,然后判断最大匹配。  

利用交叉染色法判断一个图是否为二分图。     不知道怎么染色的点击链接:交叉染色法  

用DFS取任意点,标记,搜索与其有边的点,如果该点没有被标记,则将其标记为相反,继续向下搜索;

如果被标记了,则判断其标记与应该被标记的值是否一致,一致则继续,不一致则判断该图不是二分图。

二分图的最大匹配的计算:

建立一张二分图,左右两边都是图的所有节点。取左端节点,依次向下判断是否在右端存在节点与其相连;

每次判断时,如果节点的右端相连的节点已经存在左端的节点与之相连,则判断该左端节点是否还有另外的节点可以与之相连,依次递归的向下推;

代码:

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <vector>using namespace std;typedef long long LL;const int N=1111;bool book[N];int match[N],a[N];vector <int> close[N];bool f(int x) //匈牙利算法{    int m=close[x].size();    for(int i=0 ; i < m ; i++)    {        int g=close[x][i];        if(book[g]==false)        {            book[g]=true;            if(match[g]==-1||f(match[g]))            {                match[g]=x;                return true;            }        }    }    return false;}//用染色法判断是否为二分图,若相邻点同色则不是二分图bool dfs(int x,int c){    a[x]=c;    int m=close[x].size();    for(int i=0; i<m; i++)    {        int g=close[x][i];        if(a[g]==c)            return false;        if(a[g]==0 && !dfs(g,-c))            return false;    }    return true;}int main(){    int n,m;    int x,y;    while(scanf("%d%d",&n,&m)!=EOF)    {        memset(match,-1,sizeof(match));        memset(a,0,sizeof(a));        bool flag=true;        int sum=0;        for(int i=0; i<=n; i++)    close[i].clear();        for(int i=1; i<=m; i++)        {            scanf("%d%d",&x,&y); //无向图要双向存边            close[x].push_back(y);            close[y].push_back(x);        }        //判断是否为二分图        for(int i=1; i<=n; i++)        {            if(a[i]==0)            {                if(flag)                    flag=dfs(i,1);            }        }        if(flag==false)        {            printf("No\n");            continue;        }        for(int i=1; i<=n; i++)        {            memset(book,false,sizeof(book));            if(f(i)) sum++;        }        printf("%d\n",sum/2);//无向图匹配数取二分之一    }    return 0;}






阅读全文
0 0