2016多校训练Contest8: 1003 color II hdu5823

Problem Description
You are given an undirected graph with n vertices numbered 0 through n-1.

Obviously, the vertices have 2^n - 1 non-empty subsets. For a non-empty subset S, we define a proper coloring of S is a way to assign each vertex in S a color, so that no two vertices in S with the same color are directly connected by an edge. Assume we've used k different kinds of colors in a proper coloring. We define the chromatic number of subset S is the minimum possible k among all the proper colorings of S.

Now your task is to compute the chromatic number of every non-empty subset of the n vertices.
 

Input
First line contains an integer t. Then t testcases follow. 

In each testcase: First line contains an integer n. Next n lines each contains a string consisting of '0' and '1'. For 0<=i<=n-1 and 0<=j<=n-1, if the j-th character of the i-th line is '1', then vertices i and j are directly connected by an edge, otherwise they are not directly connected.

The i-th character of the i-th line is always '0'. The i-th character of the j-th line is always the same as the j-th character of the i-th line.

For all testcases, 1<=n<=18. There are no more than 100 testcases with 1<=n<=10, no more than 3 testcases with 11<=n<=15, and no more than 2 testcases with 16<=n<=18.
 

Output
For each testcase, only print an integer as your answer in a line.

This integer is determined as follows:
We define the identity number of a subset S is  id(S)=vS2v . Let the chromatic number of S be  fid(S) .

You need to output  1<=id(S)<=2n1fid(S)×233id(S)mod232 .
 

Sample Input
  
  
2 4 0110 1010 1101 0010 4 0111 1010 1101 1010
 

Sample Output
  
  
1022423354 2538351020
Hint
For the first test case, ans[1..15]= {1, 1, 2, 1, 2, 2, 3, 1, 1, 1, 2, 2, 2, 2, 3}


n<=18,考虑状态压缩

对于某个点集S,如果它是独立集,则只需要一种颜色即可完成染色

如果它不是独立集,我们枚举它的独立集子集,那么它的染色方案数=除了当前子集的染色方案数+1

直接DP即可

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int m[21][21];
int v[300001];
int f[300001],fx[300001];
inline int min(int x,int y)
{
	if(x<y)
		return x;
	return y;
}
int main()
{
//	freopen("1003.in","r",stdin);
//	freopen("1003.ans","w",stdout);
	int T;
	scanf("%d",&T);
	while(T>0)
	{
		T--;
		int n;
		scanf("%d",&n);
		string x;
		int i,j,k;
		for(i=1;i<=n;i++)
		{
			cin>>x;
			for(j=0;j<=n-1;j++)
				m[i][j+1]=x[j]-'0';
		}
		memset(f,127/3,sizeof(f));
		memset(v,0,sizeof(v));
		int p=0;
		int lim=(1<<n)-1;
		for(i=1;i<=lim;i++)
		{
			bool flag=true;
			for(j=1;j<=n;j++)
			{
				if(!((1<<(j-1))&i))
					continue;
				for(k=j+1;k<=n;k++)
				{
					if(!((1<<(k-1))&i))
						continue;
					if(m[j][k])
					{
						flag=false;
						break;
					}
				}
				if(!flag)
					break;
			}
			if(flag)
			{
				p++;
				fx[p]=i;
				v[i]=p;
				f[i]=1;
			}
		}
		for(i=1;i<=lim;i++)
		{
			if(f[i]==1)
				continue;
			for(j=1;j<=p;j++)
			{
				if((i&fx[j])==fx[j])
				{
					f[i]=min(f[i],f[i^fx[j]]+1);
					if(f[i]==2)
						break;
				}
			}
		}
//		for(i=1;i<=(1<<n)-1;i++)
//			printf("%d ",f[i]);
//		printf("\n");
		unsigned int ans=0;
		for(i=lim;i>=1;i--)
			ans=ans*(unsigned int)233+(unsigned int)f[i];
		ans=ans*(unsigned int)233;
		printf("%u\n",ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值