Hacker Crackdown

数学模型: P1,P2,P3,...Pn 分成尽量多的组,使每组中所有集合的并集等于全集,其中集合P[i]表示计算机i及其相邻的计算机  状态转移方程为table[S] = max(table[S-S0]+1),(其中集合S0要满足S0是S的子集且,∑P[i] = U,( i∈S0))


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>
//#pragma comment(linker, "/STACK:1024000000,1024000000") 

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::getline;
using std::make_pair;
using std::greater; 

const int MAXN(17);
int cover[1 << MAXN];
int table[1 << MAXN];
int P[MAXN];

int main()
{
	int n, m, temp, n_case(0);
	while(scanf("%d", &n), n)
	{
		for(int i = 0; i < n; ++i)
		{
			P[i] = 1 << i;
			scanf("%d", &m);
			for(int j = 0; j < m; ++j)
			{
				scanf("%d", &temp);
				P[i] |= 1 << temp;
			}
		}
		
		int ALL = (1 << n)-1;
		for(int S = 0; S <= ALL; ++S)
		{
			cover[S] = 0;
			for(int i = 0; i < n; ++i)
				if(S&(1 << i))
					cover[S] |= P[i];
		}

		for(int S = 0; S <= ALL; ++S)
		{
			table[S] = 0;
			for(int S0 = S; S0; S0 = (S0-1)&S)      //枚举子集的方法
				if(cover[S0] == ALL)
					table[S] = max(table[S], table[S^S0]+1);
		}
		printf("Case %d: %d\n", ++n_case, table[ALL]);
	}
	return 0;
}


你可能感兴趣的:(Hacker Crackdown)