color II
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1126 Accepted Submission(s): 511
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.
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.
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.
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
2 4 0110 1010 1101 0010 4 0111 1010 1101 1010
Sample Output
1022423354 2538351020HintFor the first test case, ans[1..15]= {1, 1, 2, 1, 2, 2, 3, 1, 1, 1, 2, 2, 2, 2, 3}
题意:
给出一个图,由 n 个点组成 1 <= n <= 18 ,图不大
然后要给图上的点上色,要求相连的两个点颜色不同,且每个子图所用颜色数最少
要你求出所有子图的上色方案数
答案值 = 第 1个子图 的 颜色数 * 233的1 次方 + ... + 第 2^n - 1 个子图的颜色数 * 233 的 2^n - 1 次方
例如 :
n 为 2 ,两个点互不相连,那么有三个子图
1. 点1 颜色数 = 1
2. 点2 颜色数 = 1
3. 点 1 和 点 2 ,1 2 不相连,所以 颜色数 = 1
所以答案是 233 + 233^2 + 233^3 = 12703859
思路:我们可以先找出所有独立子图(就是整个子图里的所有点都是互不相连的)
然后对于所有一般子图而言,他们可由若干个独立子图结合而成
那么这个一般子图的颜色数 = 刨去某个独立子图后剩下的子图颜色数 + 1
因为它需要一种颜色把两个子图连接起来,然后枚举它所有的子图找最小值就可以了
具体的图的实现方法可以用二进制表示,先遍历一遍图找出所有的独立子图
然后就枚举所有的子图,用上述方法记录最小值
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
ll dp[1 << 18];
int mapp[25][25],n;
int vis[1 << 18];
int main(){
int t;
scanf("%d",&t);
while(t--){
mem(vis,0);
scanf("%d",&n);
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
scanf("%1d",&mapp[i][j]);
}
}
for(int i = 1;i <= (1 << n);i++){
for(int j = 0;j < n;j++){
if(i & (1 << j)){
for(int k = 0;k < n;k++){
if(i & (1 << k) && mapp[j + 1][k + 1] == 1){
vis[i] = 1; //标记其不是独立集
break;
}
}
}
if(vis[i])
break;
}
}
dp[0] = 0;
for(int i = 1;i < (1 << n);i++){ // 枚举每一个子图
dp[i] = inf;
for(int j = i;j != 0;j = (j - 1) & i){ // 枚举这个子图里的所有子集
if(!vis[j]){ //如果这个子集是一个独立集
dp[i] = min(dp[i],dp[i ^ j] + 1); //那么要子图里的对立集加一钟颜色作为连接
}
}
}
ll ans = 0;
ll tmp = 1;
for(int i = 1;i < (1 << n);i++){
tmp *= 233;
ans += (dp[i] * tmp);
}
printf("%u\n",(int)ans); // %u 就是以 2^23 取模取整数
}
return 0;
}