首页 > 其他 > 详细

poj 3692 Kindergarten (最大团模板题)

时间:2014-08-20 21:12:43      阅读:500      评论:0      收藏:0      [点我收藏+]

题目链接:http://poj.org/problem?id=3692

 

Kindergarten
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5156   Accepted: 2512

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

 

 

这个题目是二分匹配模板题,一开始用浙大模板里的最大团模板搞,超时了。。。(至今不明白为什么)

后来利用

最大团=原图节点数-补图的最大二分匹配数

AC代码:

//最大团问题,可以用原图节点数减去其补图的二分匹配
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<cstdlib>
#define CLR(A) memset(A,0,sizeof(A))
using namespace std;
const int MAX = 210;
int b,g,m,link[MAX];
bool mp[MAX][MAX], vis[MAX];
bool dfs(int u){
    for(int i = 1; i <= b; i ++)
        if(!vis[i] && !mp[u][i]){
            vis[i] = true;
            if(link[i] == -1 || dfs(link[i])){
                link[i] = u;
                return true;
            }
        }
    return false;
}
int main(){
    int ncase = 0;
    while(~scanf("%d%d%d",&g,&b,&m)&&g+b+m){
        CLR(mp);
        while(m --){
            int u, v;
            scanf("%d%d",&u,&v);
            mp[u][v]=1;
        }
        int t=0;
        memset(link, -1, sizeof(link));
        for(int i = 1; i <= g; i ++){
            memset(vis, 0, sizeof(vis));
            if(dfs(i)) t++;
        }
        cout<<"Case "<<++ncase<<": "<<g+b-t<< endl;
    }
    return 0;
}


 

TLE代码:

#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
#define V 600
int g[V][V],dp[V],stk[V][V],mx;
int dfs(int n,int ns,int dep){
	if(0==ns){
		if(dep>mx) mx=dep;
		return 1;
	}
	int i,j,k,p,cnt;
	for(i=0;i<ns;i++){
		k=stk[dep][i];cnt=0;
		if(dep+n-k<=mx)    return 0;
		if(dep+dp[k]<=mx)   return 0;
		for(j=i+1;j<ns;j++){
			p=stk[dep][j];
			if(g[k][p]) stk[dep+1][cnt++]=p;
		}
		dfs(n,cnt,dep+1);
	}
	return 1;
}
int clique(int n){
	int i,j,ns;
	for(mx=0,i=n-1;i>=0;i--){
		for(ns=0,j=i+1;j<n;j++){
			if(g[i][j])  stk[1][ns++]=j;
			dfs(n,ns,1); dp[i]=mx;
		}
	}
	return mx;
}
int main(){
	int G,B,M,kase=0;
	while(~scanf("%d%d%d",&G,&B,&M) && (G || B || M)){
		memset(g,0,sizeof(g));
		for(int i=0;i<G;i++)
		    for(int j=0;j<G;j++){
		   // 	if(i==j)    continue;
				g[i][j]=1;
		    }
		for(int i=0;i<B;i++)
		    for(int j=0;j<B;j++){
		//    	if(i==j)    continue;
		    	g[i+G][j+G]=1;
		    }
		for(int i=0;i<M;i++){
			int x,y;
			scanf("%d%d",&x,&y);
			x--;y--;
			g[x][y+G]=1;
			g[y+G][x]=1;
		}
		int res=clique(G+B);
		printf("Case %d: %d\n",++kase,res);
	}
}


 

 

 

 

 

 

poj 3692 Kindergarten (最大团模板题),布布扣,bubuko.com

poj 3692 Kindergarten (最大团模板题)

原文:http://blog.csdn.net/asdfghjkl1993/article/details/38711177

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!