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

来源:互联网 发布:安卓原笔迹手写软件 编辑:程序博客网 时间:2024/05/29 21:30

The Accomodation of Students

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

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

#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;int e[205][205],n,m,color[205],match[205],book[205];int bfs(int x){    memset(color,0,sizeof(color));    queue<int>q;    int fr,ne;    q.push(x);    color[x]=1;    while(!q.empty())    {        fr=q.front();        q.pop();        for(int i=1;i<=n;i++)        {            if(e[fr][i]==1)            {                if(color[i]==0)                {                    color[i]=3-color[fr];                    q.push(i);                }                else                {                    if(color[i]==color[fr])                    {                        return 0;                    }                }            }        }    }    return 1;}int dfs(int u){    for(int i=1;i<=n;i++)    {        if(book[i]==0&&e[u][i]==1)        {            book[i]=1;            if(match[i]==-1||dfs(match[i]))            {                match[i]=u;                return 1;            }        }    }    return 0;}int main(){    while(~scanf("%d%d",&n,&m))    {        memset(e,0,sizeof(e));        int u,v;        for(int i=0;i<m;i++)        {            scanf("%d%d",&u,&v);            e[u][v]=1;        }        int re=bfs(1);        if(re==0)        {            printf("No\n");        }        else        {            memset(match,-1,sizeof(match));            int sum=0;            for(int i=1;i<=n;i++)            {                memset(book,0,sizeof(book));                if(dfs(i))                {                    sum++;                }            }            printf("%d\n",sum);        }    }    return 0;}
0 0
原创粉丝点击