color II
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 370 Accepted Submission(s): 157
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}
Author
学军中学
Source
Recommend
题意:有n个点,编号为0~n-1,给你一个矩阵,s[i][j]==1表示i点和j点有一条边,k是一个点集最少染色数,相邻两个点不能是相同颜色,求:
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
.
思路:id(s)很明显用二进制就可以看出是怎么回事,剩下的问题就是k怎么求,这是个dp的过程,首先状态压缩暴力枚举,预处理出两两无边的子集,也就是可以只染一种颜色的子集(即k==1),然后从头dp到尾,每一种状态都有他们的子集,若某状态的子集k==1,那么则有动态转移方程:
dp[i]=min(dp[i],dp[i^j]+1);其中i为当前状态,j为i状态的一个子集并且k==1,如何枚举某状态的所有子集,这里用了巧妙的位运算快速找出(看不懂这一步也不用找我了。。),下面给代码。
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn (1<<18)
#define inf 0x3f3f3f3f
#define MOD (1ll<<32)
typedef long long LL;
int dp[maxn], vis[maxn];
char s[20][20];
int main(){
int t;
scanf("%d", &t);
while (t--){
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++){
scanf("%s", s[i]);
}
memset(vis, 0, sizeof(vis));
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 (s[j][k] == '1'&&i&(1 << k)){
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; j = (j - 1)&i){
if (!vis[j]){
dp[i] = min(dp[i], dp[i^j] + 1);
}
}
}
LL f = 1;
LL ans = 0;
for (int i = 1; i < (1 << n); i++){
f *= 233;
f %= MOD;
ans += f*dp[i];
ans %= MOD;
}
printf("%lld\n", ans);
}
}