hdu2444二分匹配

来源:互联网 发布:html中调用js函数 编辑:程序博客网 时间:2024/05/19 21:00
B - The Accomodation of Students
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 2444

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

No

3

能否形成二分图,能得话,最匹配数

#include <iostream>#include <stdio.h>#include <vector>#include <string.h>#include <queue>using namespace std;int const maxn=1010;int linker[maxn],used[maxn];vector<int>G[maxn];int col[maxn];bool bfs(int s){    col[s]=1;    queue<int>p;    p.push(s);    while(!p.empty())    {        int from=p.front();        p.pop();        for(int i=0;i<G[from].size();i++)        {            if(col[G[from][i]]==-1)            {                p.push(G[from][i]);                col[G[from][i]]=!col[from];            }            if(col[G[from][i]]==col[from])                return false;        }    }    return true;}int dfs(int u){    for(int i=0;i<G[u].size();i++)    {        int v=G[u][i];        if(!used[v])        {            used[v]=true;            if(linker[v]==-1||dfs(linker[v]))            {                linker[v]=u;                return true;            }        }    }    return false;}int solve(int n){    int res=0;    memset(linker,-1,sizeof(linker));    for(int i=1;i<=n;i++)    {        memset(used,false,sizeof(used));        if(dfs(i))res++;    }    return res/2;}int main(){    int n,m;    int x,y;    while(scanf("%d%d",&n,&m)!=-1)    {        for(int i=0;i<=n;i++)        {            G[i].clear();        }        for(int i=0; i<m; i++)        {            scanf("%d%d",&x,&y);            G[x].push_back(y);            G[y].push_back(x);        }        int flag=1;        memset(col,-1,sizeof(col));        for(int i=1; i<=n; i++)            if(col[i]==-1&&!bfs(i))            {                flag=0;                break;            }        if(flag==0)        {            cout<<"No"<<endl;            continue;        }        int ans=solve(n);        cout<<ans<<endl;    }    return 0;}


0 0
原创粉丝点击