【HDU】2458 Kindergarten 二分匹配
来源:互联网 发布:新手开淘宝 编辑:程序博客网 时间:2024/04/29 20:08
Kindergarten
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 652 Accepted Submission(s): 369
Problem 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.
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 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
传送门:【HDU】2458 Kindergarten
题目大意:一个花园中有G个女孩,B个男孩,其中女孩之间相互认识,男孩之间也相互认识,现在给你M对关系(x,y),表示女孩x和男孩y认识,现在要从这些孩子中挑选出尽量多的孩子做游戏,但是必须保证这些孩子相互认识。
题目分析:我们假设如果存在关系(x,y)则存在边(x,y),通过这个我们得到原图,然后对原图构造补图,这样,如果女孩x'和男孩y'不认识,我们建边(x',y')。然后对补图求最大匹配,得到的是补图的最小点覆盖数ANS,也就是原图的最大独立集数ANS,那么结果就是G+B-ANS。为什么这样可以?因为把最大独立集从原图中删去,剩下的必定是两两相互认识的。
代码如下:
传送门:【HDU】2458 Kindergarten
题目大意:一个花园中有G个女孩,B个男孩,其中女孩之间相互认识,男孩之间也相互认识,现在给你M对关系(x,y),表示女孩x和男孩y认识,现在要从这些孩子中挑选出尽量多的孩子做游戏,但是必须保证这些孩子相互认识。
题目分析:我们假设如果存在关系(x,y)则存在边(x,y),通过这个我们得到原图,然后对原图构造补图,这样,如果女孩x'和男孩y'不认识,我们建边(x',y')。然后对补图求最大匹配,得到的是补图的最小点覆盖数ANS,也就是原图的最大独立集数ANS,那么结果就是G+B-ANS。为什么这样可以?因为把最大独立集从原图中删去,剩下的必定是两两相互认识的。
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REPV( i , a , b ) for ( int i = a ; i >= b ; -- i )#define clear( a , x ) memset ( a , x , sizeof a )const int MAXN = 205 ;const int MAXE = 40000 ;struct Edge {int v , n ;} ;Edge edge[MAXE] ;int adj[MAXN] , cntE ;int G[MAXN][MAXN] ;bool vis[MAXN] ;int link[MAXN] ;int g , b , m ;void addedge ( int u , int v ) {edge[cntE].v = v ; edge[cntE].n = adj[u] ; adj[u] = cntE ++ ;}int find ( int u ) {for ( int i = adj[u] ; ~i ; i = edge[i].n )if ( !vis[edge[i].v] ) {int v = edge[i].v ;vis[v] = 1 ;if ( !~link[v] || find ( link[v] ) ) {link[v] = u ;return 1 ;}}return 0 ;}int match () {int ans = 0 ;clear ( link , -1 ) ;REPF ( i , 1 , g ) {clear ( vis , 0 ) ;ans += find ( i ) ;}return ans ;}void work () {int cas = 0 ;int u , v ;while ( ~scanf ( "%d%d%d" , &g , &b , &m ) && ( g || b || m ) ) {printf ( "Case %d: " , ++ cas ) ;cntE = 0 ;clear ( G , 0 ) ;clear ( adj , -1 ) ;REP ( i , m ) {scanf ( "%d%d" , &u , &v ) ;G[u][v] = 1 ;}REPF ( i , 1 , g )REPF ( j , 1 , b )if ( !G[i][j] )addedge ( i , j ) ;printf ( "%d\n" , g + b - match () ) ;}}int main () {work () ;return 0 ;}
0 0
- 【HDU】2458 Kindergarten 二分匹配
- hdu 2458 二分图匹配 Kindergarten
- hdu 2458 Kindergarten 匹配
- 【二分匹配】 hdu2458 Kindergarten
- HDU 2458 Kindergarten【二分图之反建边】
- poj 3692 Kindergarten 二分匹配
- 杭电2458 Kindergarten(二分图最大匹配)
- HDU 2458Kindergarten(二分图 最大团点数)
- POJ - 3692 Kindergarten 二分图 最大匹配
- poj 3692Kindergarten(二分匹配模板)
- 【图论】【二分图匹配】[POJ 3692]Kindergarten
- HDU 2458 Kindergarten
- 最大匹配-HDOJ 2458 Kindergarten
- poj 3041Asteroids+3692Kindergarten(二分图匹配+公式补充)
- poj 3692 Kindergarten(二分图匹配,最大独立集)
- poj 3692 Kindergarten(二分匹配,最大独立集)
- POJ3692——Kindergarten(最大二分匹配)
- poj 3692 Kindergarten(最大团,二分图匹配)
- int和NSInteger区别
- poj 2442 堆的应用(m组数取前n小)
- Android下实现静默安装指定APK
- test for this new blog~
- 软件顾问可视设计的得力助手——PowerMockup
- 【HDU】2458 Kindergarten 二分匹配
- UVA10651- Pebble Solitaire(位运算+记忆化搜索)
- hdu 2870
- Android之Goldfish
- hdu 1058 The sum problem
- 【DirectX 11 SDK 学习笔记】3D Transformation
- 经纬财富:沧州黄金连续暴跌谜团、谁为巨额卖单
- RMAN备份FORMAT格式中%的含义
- LocalPi