hdu 2444 是否是二分图+二分图最大匹配数

来源:互联网 发布:大管家收银软件 编辑:程序博客网 时间:2024/05/16 08:13


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


#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<vector>#include<string>#include<algorithm>#define LL long long#define inf 0x3f3f3f3fusing namespace std;int color[250];vector<int> g[250];bool visited[250];int linker[250];int n;bool col(int u){    for(int i=0;i<g[u].size();i++)    {        if(color[g[u][i]]==-1)        {            color[g[u][i]]=!color[u];            if(!col(g[u][i]))                return false;        }        else if(color[g[u][i]]==color[u])            return false;    }    return true;}bool dfs(int u){    for(int i=0;i<g[u].size();i++)        if(!visited[g[u][i]])    {        visited[g[u][i]]=1;        if(linker[g[u][i]]==-1||dfs(linker[g[u][i]]))        {            linker[g[u][i]]=u;            return true;        }    }    return false;}int hungary(){    int num=0;    memset(linker,-1,sizeof(linker));    for(int u=1;u<=n;u++)    {        memset(visited,0,sizeof(visited));        if(dfs(u))            num++;    }    return num;}int main(){    while(cin>>n)    {        int m;        cin>>m;        for(int i=0;i<=210;i++)            g[i].clear();        memset(visited,0,sizeof(visited));        while(m--)        {            int a,b;            cin>>a>>b;            g[a].push_back(b);            g[b].push_back(a);        }        memset(color,-1,sizeof(color));        color[1]=1;        if(!col(1))            cout<<"No"<<endl;        else            cout<<hungary()/2<<endl;    }    return 0;}

判断是否是二分图:

bool col(int u)//判断是否是二分图{    for(int i=0;i<g[u].size();i++)    {        if(color[g[u][i]]==-1)        {            color[g[u][i]]=!color[u];            if(!col(g[u][i]))                return false;        }        else if(color[g[u][i]]==color[u])            return false;    }    return true;}
 color[1]=1;        if(!col(1))//返回false则不是二分图            cout<<"No"<<endl;



0 0
原创粉丝点击