POJ 3692 二分图最大点独立集 解题报告

来源:互联网 发布:河南seo技术培训 编辑:程序博客网 时间:2024/05/16 07:59

Kindergarten

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
G, B (1 ≤ G, B ≤ 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 3
1 1
1 2
2 3
2 3 5
1 1
1 2
2 1
2 2
2 3
0 0 0

Sample Output

Case 1: 3
Case 2: 4

【解题报告】

代码如下:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 510int n,m,k,cas=0;int mp[N][N],cnt,head[N];struct Edge{int to,nxt;}e[N*N];int match[N],vis[N];void adde(int u,int v){    e[++cnt].to=v;    e[cnt].nxt=head[u];    head[u]=cnt;}bool dfs(int u,int flag){    for(int i=head[u];~i;i=e[i].nxt)    {        int v=e[i].to;        if(vis[v]==flag) continue;        vis[v]=flag;        if(!match[v]||dfs(match[v],flag))        {            match[v]=u;            return 1;        }    }    return 0;}int main(){    while(scanf("%d%d%d",&n,&m,&k)&&n&&m&&k)    {        cnt=-1;        memset(head,-1,sizeof(head));        memset(vis,0,sizeof(vis));        memset(match,0,sizeof(match));        memset(mp,0x7f,sizeof(mp));        for(int i=1;i<=k;++i)        {            int u,v;scanf("%d%d",&u,&v);            mp[u][v]=0;        }        for(int i=1;i<=n;++i)        for(int j=1;j<=m;++j)             if(mp[i][j]) adde(i,j);        int ans=0;        for(int i=1;i<=n;++i)        {            if(dfs(i,i)) ans++;        }        printf("Case %d: %d\n",++cas,n+m-ans);    }    return 0;       }