现在的位置: 首页 > 综合 > 正文

poj 1466 Girls and Boys 最大独立集

2013年05月26日 ⁄ 综合 ⁄ 共 667字 ⁄ 字号 评论关闭

       根据性别分为两个集合,然后求最大独立集。由于题目没有给出性别,可以不划分集合,每个集合都为n个,求最大匹配数,最后除2即可。

 

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

const int N = 505;

int maze[N][N];
bool isvisit[N];
int match[N];
int n;

bool find(int u)
{
	for (int i = 0; i < n; i++)
	{
		if (maze[u][i] && !isvisit[i])
		{
			isvisit[i] = true;
			if (match[i] == -1 || find(match[i]))
			{
				match[i] = u;
				return true;
			}
		}
	}

	return false;
}

int main()
{
	int num;
	int j, s;

	while (scanf("%d", &n) != EOF)
	{
		memset(maze, false, sizeof(maze));
		for (int i = 0; i < n; i++)
		{		
			scanf("%d: (%d)", &s, &num);
			while (num--)
			{
				scanf("%d", &j);
				maze[i][j] = true;
			}
		}

		int ans = 0;
		memset(match, -1, sizeof(match));

		for (int i = 0; i < n; i++)
		{
			memset(isvisit, false, sizeof(isvisit));
			if (find(i))
				ans++;
		}

		cout << n - ans/2 << endl;
	}
	return 0;
}

抱歉!评论已关闭.