`
txf2004
  • 浏览: 6855156 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

【二分图+最大独立集】北大 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 ;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics