【二分匹配】 HDU 2444 The Accomodation of Students

来源:互联网 发布:linux c pipe 编辑:程序博客网 时间:2024/05/29 18:34

点击打开链接

求两个集合,使得一个集合内的任意两个人互不认识。

最大独立集==1 输出 No。

#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <iostream>#include <algorithm>#include <sstream>#include <cmath>using namespace std;#include <queue>#include <stack>#include <set>#include <vector>#include <deque>#include <map>#define cler(arr, val)    memset(arr, val, sizeof(arr))typedef long long  LL;const int MAXN = 210;const int MAXM = 140000;const int INF = 0x3f3f3f3f;const LL mod = 10000007;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int mp[MAXN][MAXN];int linker[MAXN],vN,uN;bool used[MAXN];bool dfs(int u)//从左边开始找增广路径{    int v;    for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改      if(mp[u][v]&&!used[v])      {          used[v]=true;          if(linker[v]==-1||dfs(linker[v]))          {//找增广路,反向              linker[v]=u;              return true;          }      }    return false;//这个不要忘了,经常忘记这句}int hungary(){    int res=0;    int u;    memset(linker,-1,sizeof(linker));    for(u=0;u<uN;u++)    {        memset(used,0,sizeof(used));        if(dfs(u))            res++;    }    return res;}int col[MAXN];bool check(int x,int c){    col[x]=!c;    for(int i=0;i<vN;i++)    {        if(mp[x][i])        {            if(col[i]==col[x])                return false;            else if(col[i]==!col[x])                continue;            if(!check(i,!c))                return false;        }    }    return true;}int main(){    int n,m;    while(cin>>n>>m)    {        int a,b;        cler(mp,0);        cler(col,-1);        vN=uN=n;        for(int i=1;i<=m;i++)        {            cin>>a>>b;            a--,b--;            mp[a][b]=mp[b][a]=1;        }        col[0]=1;        if(!check(0,1))            puts("No");        else        {            int ans=hungary();            cout<<ans/2<<endl;        }    }}


0 0
原创粉丝点击