POJ 3692 Kindergarten (补图的最大独立集||匈牙利算法)

来源:互联网 发布:易语言炫舞辅助源码 编辑:程序博客网 时间:2024/05/17 03:30
Kindergarten
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 7291 Accepted: 3590

Description

In a kindergarten, there are a lot of kids. All girls of the kids know each other and all boys also know each other. In addition to that, some girls and boys know each other. Now the teachers want to pick some kids to play a game, which need that all players know each other. You are to help to find maximum number of kids the teacher can pick.

Input

The input consists of multiple test cases. Each test case starts with a line containing three integers
GB (1 ≤ GB ≤ 200) and M (0 ≤ M ≤ G × B), which is the number of girls, the number of boys and
the number of pairs of girl and boy who know each other, respectively.
Each of the following M lines contains two integers X and Y (1 ≤ X≤ G,1 ≤ Y ≤ B), which indicates that girl X and boy Y know each other.
The girls are numbered from 1 to G and the boys are numbered from 1 to B.

The last test case is followed by a line containing three zeros.

Output

For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the maximum number of kids the teacher can pick.

Sample Input

2 3 31 11 22 32 3 51 11 22 12 22 30 0 0

Sample Output

Case 1: 3Case 2: 4

Source

2008 Asia Hefei Regional Contest Online by USTC


POINT:

女生为图的左部分,男生为右部分。他们都各自认识(即各自连了线)。

男女分别认识的也各自连线。问你可以选出几个人来,他们各自都互相认识。

也就是求最大点集,每个点都和其他点有线连着。

构造图的补图,这样互相没线的就代表认识。可构造一个二分图。

求这个二分图的最大独立集,也就是这个点集的点都没有线连。(也就是全部都互相认识)


可以用最大流,或者匈牙利算法。

dinic:

#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <queue>using namespace std;const int maxn = 444;const int inf = 0x3f3f3f3f;struct node{    int from,to,flow,cap;    node(int u,int v,int f,int c):    from(u),to(v),flow(f),cap(c){};    };int s,t;vector<int>G[maxn];vector<node> len;int cur[maxn],d[maxn],vis[maxn];void add(int u,int v,int c){    len.push_back(node(u,v,0,c));    len.push_back(node(v,u,0,0));    G[u].push_back(len.size()-2);    G[v].push_back(len.size()-1);}bool bfs(){    memset(d,0,sizeof d);    memset(vis,0,sizeof vis);    queue<int> q;    q.push(s);    vis[s]=1,d[s]=0;    while(!q.empty())    {        int u=q.front();q.pop();        for(int i=0;i<G[u].size();i++)        {            node e=len[G[u][i]];            if(e.cap>e.flow&&vis[e.to]==0)            {                d[e.to]=d[u]+1;                vis[e.to]=1;                q.push(e.to);            }        }    }    return vis[t];}int dfs(int u,int a){    if(u==t||a==0) return a;    int flow=0,f;    for(int &i = cur[u];i<G[u].size();i++)    {        node &e=len[G[u][i]];        if(d[e.to]-1==d[u]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)        {            flow+=f;            e.flow+=f;            a-=f;            len[G[u][i]^1].flow-=f;        }        if(a==0) break;    }    if(flow==0) d[u]=-1;    return flow;}int maxflow(){    int ans=0;    while(bfs())    {        memset(cur,0,sizeof cur);        ans+=dfs(s,inf);    }    return ans;}int mp[maxn][maxn];void init(){    memset(mp,0,sizeof mp);    for(int i=0;i<maxn;i++) G[i].clear();    len.clear();}int main(){    int g,b,m;    int p=0;    while(~scanf("%d %d %d",&g,&b,&m))    {        if(g==b&&b==m&&g==0) break;        init();        for(int i=1;i<=m;i++)        {            int u,v;scanf("%d %d",&u,&v);            mp[u][v]=1;        }        for(int i=1;i<=g;i++)        {            for(int j=1;j<=b;j++)            {                if(mp[i][j]==0)                {                    add(i,j+g,inf);                }            }        }        s=0;t=b+g+1;        for(int i=1;i<=g;i++) add(0,i,1);        for(int i=1;i<=b;i++) add(i+g,t,1);        printf("Case %d: ",++p);        printf("%d\n",b+g-maxflow());    }    }


匈牙利:

#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <queue>using namespace std;const int maxn = 444;const int inf = 0x3f3f3f3f;int mp[maxn][maxn];int use[maxn],aim[maxn]; int g,b,m;bool dfs(int now){    for(int i=1;i<=b;i++)    {        if(use[i]==0&&mp[now][i]==0)        {            use[i]=1;            if(aim[i]==0||dfs(aim[i]))            {                aim[i]=now;                return true;            }        }    }    return false;}int main(){    int p=0;    while(~scanf("%d %d %d",&g,&b,&m))    {        if(g==b&&b==m&&g==0) break;        memset(mp,0,sizeof mp);        memset(aim,0,sizeof aim);        for(int i=1;i<=m;i++)        {            int u,v;scanf("%d %d",&u,&v);            mp[u][v]=1;        }        int dui=0;        for(int i=1;i<=g;i++)        {            memset(use,0,sizeof use);            if(dfs(i)) dui++;        }        printf("Case %d: ",++p);        printf("%d\n",g+b-dui);            }    }




原创粉丝点击