大体题意:
给你一个图,要求用最少的颜色染这个图,使得这个图任意直接相连的点颜色不一样!
思路:
典型的状压dp--图的染色问题!
令dp[s] 为染 s 这个状态图 最小的颜色数,其中 s 里 1代表要染色的 0 代表不需要染色!
那么显然dp[0] = 0
dp[s] = min{dp[s^s0] + 1}
其中s0是s 的子集并且 里面的1 在图中不直接相连!
判断 状态s 里面的1 是否不直接相连可以预处理!
找s 的子集 可以减1 进行&运算! (想一想就知道)
详细见代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define min(a,b) (a) > (b) ? (b) : (a)
using namespace std;
const int maxn = 19;
typedef long long ll;
const ll mod = 1ll << 32;
const int inf = 0x3f3f3f3f;
int n;
char g[maxn][maxn];
bool ok[1 << maxn];
int dp[1<<maxn];
bool judge(int s){
for (int i = 0; i < n; ++i){
if (s & (1 << i)){
for (int j = 0; j < n; ++j){
if (i != j && (s & (1 << j))){
if (g[i][j] == '1') return false;
}
}
}
}
return true;
}
void init(){
memset(ok,0,sizeof ok);
for (int s = 0; s < (1 << n); ++s){
if (judge(s))ok[s] = 1;
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for (int i = 0; i < n; ++i) scanf("%s",g[i]);
init();
dp[0] = 0;
for (int s = 1; s < (1 << n); ++s){
dp[s] = inf;
for (int s0 = s; s0; s0 = (s0-1)&s){
if (ok[s0]) dp[s] = min(dp[s],dp[s^s0] + 1);
}
}
ll ans = 0;
ll cur = 1ll;
for (int s = 1; s < (1 << n); ++s){
cur = (cur * 233) % mod;
ans = (ans + ((ll)dp[s] * cur) %mod)%mod ;
}
printf("%lld\n",ans);
}
return 0;
}
color II
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 948 Accepted Submission(s): 405
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
wange2014