poj 3692 Kindergarten (最大团模板题)

79 篇文章 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
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 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);
	}
}


 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值