POJ 3692 Kindergarten

来源:互联网 发布:java 提取html文本 编辑:程序博客网 时间:2024/04/29 19:02

POJ 3692 Kindergarten

题目链接:POJ 3692


题意:

在幼儿园,有g个女生,b个男生,女生之间是互相认识的,男生之间也是互相认识的,然后男生女孩之间有m个关系,代表是互相认识的。

从他们之中选取一些出来,使得大家互相认识而且总人数最多。


分析:

转化为补图,补图中有边的说明是不为识的,那么想要剩下的人都认识,就得把边去掉,也就是去掉最少的点使所有的边都去掉,

就是最小路径覆盖,最小路径覆盖数等于最大二分配匹数。


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>using namespace std;// BFSconst int maxn=512;int g[maxn][maxn];int L,R;int x[maxn],y[maxn];int Q[maxn]; //模拟队列int pre[maxn];int MaxMatch(){    int res=0,temp;memset(x,-1,sizeof(x));memset(y,-1,sizeof(y));for(int i=1;i<=L;i++){if(x[i]==-1){int cur=0,tail=0;for(int j=1;j<=R;j++){if(g[i][j])pre[j]=-1,Q[tail++]=j;else pre[j]=-2;}//BFSwhile(cur<tail){temp=Q[cur];if(y[temp]==-1)break;cur++;for(int j=1;j<=R;j++){if(pre[j]==-2&&g[y[temp]][j]){pre[j]=temp;Q[tail++]=j;}}}//end of BFSif(cur==tail)  //没有找到交错轨continue;while(pre[temp]>-1)  //更改交错轨上匹配状态{x[ y[pre[temp]] ]=temp;y[temp]=y[pre[temp]];temp=pre[temp];}y[temp]=i;x[i]=temp;res++;}}return res;}int main(){// freopen("in.txt","r",stdin);int n,m,k,xx,yy,Case=1;while(scanf("%d %d %d",&n,&m,&k)!=EOF&&(n||m||k)){memset(g,0,sizeof(g));for(int i=0;i<k;i++){scanf("%d %d",&xx,&yy);g[xx][yy]=1;}for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)g[i][j]=1-g[i][j];L=n;R=m;printf("Case %d: %d\n",Case++,n+m-MaxMatch());}return 0;}



0 0
原创粉丝点击