color IITime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 512 Accepted Submission(s): 205
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)=∑v∈S2v . Let the chromatic number of S be fid(S) . You need to output ∑1<=id(S)<=2n−1fid(S)×233id(S)mod232 .
Sample Input
Sample Output
|
题意:理解关键一点求就是用最少的颜色去染张图,保证边的两个端点颜色不同。
思路:看了题解才知道原来这也能过。。
dp[i]表示i这个集合所需要的最少颜色。我们知道一个独立集就是两两之间不连边的子集,染一个独立集意味着只需要一种颜色。
那么枚举i的每一个独立集,dp[i]=min(dp[i],dp[i^j]),i^j意味着,以i为全集的j的补集。
也算是学到了些新知识。
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;
const int MAXN =20;
const long long INF =0x3f3f3f3f ;
const long long MOD =(1ll<<32);
long long dp[1100000];
char d[MAXN][MAXN];
bool ok[1100000];
int main()
{
//freopen("in.txt","r+",stdin);
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++){
scanf("%s",d[i]);
for(int j=0;j<n;j++)
d[i][j]-='0';
}
for(int k=1;k<1<<n;k++){
ok[k]=false;
for(int i=0;i<n&&!ok[k];i++){
if((k>>i)&1){
for(int j=0;j<n;j++){
if(((k>>j)&1)&&d[i][j]){
ok[k]=true;
break;
}
}
}
}
}
dp[0]=0;
for(int i=1;i<(1<<n);i++){
dp[i]=INF;
for(int j=i;j;j=(j-1)&i){
if(!ok[j])
dp[i]=min(dp[i],dp[i^j]+1);
}
}
long long ans=1;
long long res=0;
for(int i=1;i<(1<<n);i++){
ans*=233;
ans%=MOD;
res+=ans*dp[i];
res%=MOD;
}
printf("%I64d\n",res);
}
return 0;
}
标程是预处理出所有的独立集,再做n次卷积。
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63-__builtin_clzll(x))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
#define pw(x) ((ll(1))<<(x))
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))
void getre(){int x=0;printf("%d\n",1/x);}
void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);}
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
#define lld "%I64d"
#else
#define lld "%lld"
#endif
inline void gn(long long&x){
int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double db;
inline ll sqr(ll a){return a*a;}
inline db sqrf(db a){return a*a;}
const int inf=0x3f3f3f3f;
const db pi=3.14159265358979323846264338327950288L;
const db eps=1e-6;
const int mo=1000000007;
int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}
int n;
char s[22][22];
int b[22];
int du[1<<18];
int ans[1<<18];
int pro[1<<18];
int ret[1<<18];
void fwt(int *a,int n){
rep(i,0,n)
rep(j,0,pw(n))if(pw(i)&j)
if((a[j]+=a[j^pw(i)])>=mo)a[j]-=mo;
}
void ifwt(int *a,int n){
rep(i,0,n)
rep(j,0,pw(n))if(pw(i)&j)
if((a[j]-=a[j^pw(i)])<0)a[j]+=mo;
}
int main()
{
#ifdef JCVB
freopen("1.in","r",stdin);
freopen("2.out","w",stdout);
int _time_jc=clock();
#endif
int te;gn(te);
while(te--){
gn(n);
rep(i,0,n)gs(s[i]);
cl(b);
rep(i,0,n)
rep(j,0,n)if(s[i][j]=='1')b[i]|=pw(j);
pro[0]=1;
rep(i,1,pw(n)){
int bo=1;
rep(j,0,n)if(pw(j)&i){
if(b[j]&i){
bo=0;
break;
}
}
du[i]=bo;
ans[i]=0;
pro[i]=1;
}
fwt(du,n);
rep(i,1,n+1){
rep(j,0,pw(n))mmo(pro[j],du[j]);
rep(j,0,pw(n))ret[j]=pro[j];
ifwt(ret,n);
rep(j,0,pw(n))if(ans[j]==0 && ret[j])ans[j]=i;
}
ans[0]=0;
int r=0;
per(i,0,pw(n)){
r=int(r*233ll);
r+=ans[i];
}
printf("%u\n",r);
}
#ifdef JCVB
debug("time: %d\n",int(clock()-_time_jc));
#endif
return 0;
}