题意:给定一个n*n的图,求所有点集的最小染色数。
分析:如果一个点集是独立集那么这些点可以只染一个颜色,那么我们就能直接枚举独立集子集然后从另一半子集+1继承过来最小染色数啦。O(3^n)
代码:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=20;
const int mod=998244353;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1ll<<55;
const double eps=1e-5;
const double inf=~0u>>1;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
char s[N][N];
int e[N],d[1<<N],q[1<<N];
uint g[1<<N],dp[1<<N];
int main()
{
int i,j,n,t,u,v,mx;
uint ans;
scanf("%d", &t);
e[0]=1;g[0]=1;
for (i=1;i<N;i++) e[i]=e[i-1]*2;
for (i=0;i<N;i++) d[e[i]]=i;
for (i=1;i<e[18];i++) g[i]=g[i-1]*233;
while(t--) {
scanf("%d", &n);
for (i=0;i<n;i++) scanf("%s", s[i]);
q[0]=1;mx=1<<n;
for (i=1;i<mx;i++) {
q[i]=1;j=i&-i;
u=i-j;v=d[j];
if (!q[u]) { q[i]=0;continue ; }
for (j=0;j<n;j++)
if ((u&e[j])&&s[v][j]=='1') {
q[i]=0;break ;
}
}
dp[0]=0;ans=0;
for (i=1;i<mx;i++) {
dp[i]=20;
for (j=i;j;j=(j-1)&i)
if (q[j]) dp[i]=min(dp[i],dp[i^j]+1);
ans+=dp[i]*g[i];
}
printf("%u\n", ans);
}
return 0;
}