【二分图+最大独立集】北大 poj 2771 Guardian of Decency


/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2011 panyanyany All rights reserved. URL : http://poj.org/problem?id=2771 Name : 2771 Guardian of Decency Date : Monday, November 21, 2011 Time Stage : one hour and a half Result: 9585300 panyanyany 2771 Accepted 524K 2391MS C++ 2084B 2011-11-21 21:10:58 Test Data : Review : 一开始以为是最大匹配,就是可以去的同学两两匹配,然后找最大的匹配…… 看了人家的代码才知道,原来是求所谓的最大独立集,要匹配会发生感情的同学 然后用总数减去这些同学,就是完全不会谈恋爱的同学了,然后再把那些会恋爱的同学两两拆散, 加到完全不会恋爱的同学中,就是最大独立集了。感觉这招够毒的…… 用时花了2千多毫秒,不知道人家7十多的怎么实现的…… //----------------------------------------------------------------------------*/ #include <stdio.h> #include <string.h> #include <vector> #include <math.h> using std::vector ; #define MAXSIZE 508 int tcase, n ; int link[MAXSIZE], cover[MAXSIZE] ; bool map[MAXSIZE][MAXSIZE] ; struct characteristic { int age ; char sex ; char music_style[101] ; char fav_sport[101] ; bool IsCouple (characteristic other) { if ((abs (age - other.age) > 40) || (sex == other.sex) || (strcmp (music_style, other.music_style)) || (!strcmp (fav_sport, other.fav_sport))) return false ; else return true ; } }; characteristic pupils[MAXSIZE] ; bool find (int cur) { int i, j ; for (i = 1 ; i <= n ; ++i) { if (cover[i] == false && map[cur][i] == true) { cover[i] = true ; if (link[i] == 0 || find (link[i])) { link[i] = cur ; return true ; } } } return false ; } int main () { int i, j ; int sum ; while (~scanf ("%d", &tcase)) { while (tcase--) { scanf ("%d", &n) ; for (i = 1 ; i <= n ; ++i) { scanf ("%d %c %s %s", &pupils[i].age, &pupils[i].sex, pupils[i].music_style, pupils[i].fav_sport) ; // printf ("[%d], [%c], [%s], [%s]\n", pupils[i].age, pupils[i].sex, // pupils[i].music_style, pupils[i].fav_sport) ; } memset (map, 0, sizeof (map)) ; for (i = 1 ; i <= n ; ++i) { for (j = 1 ; j <= n ; ++j) { map[i][j] = pupils[i].IsCouple (pupils[j]) ; // printf ("%d ", map[i][j]) ; } // puts ("") ; } sum = 0 ; memset (link, 0, sizeof (link)) ; for (i = 1 ; i <= n ; ++i) { memset (cover, 0, sizeof (cover)) ; sum += find (i) ; } printf ("%d\n", n - sum/2) ; } } return 0 ; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值