hdu5823 color II(状态压缩DP)
来源:互联网 发布:种子搜索算法代码 编辑:程序博客网 时间:2024/06/10 10:09
题意:给你一个图G,求每个子图的最少染色数
思路:每个子图的染色问题,可以看成是找每个子图的独立集问题(两两顶点互不相邻的图叫独立集),可以先枚举每个状态,然后枚举每个顶点,表示以这个顶点为起始的子图,再枚举每个顶点,表示这个顶点和上面那个顶点所构成的图是一个独立集,预处理出所有非法的状态,即他们之间有边。
令dp[s]为子图S已经染色的最少染色数,那么有dp[s]=min(dp[s],dp[s^s0]+1)其中s0是S的子集,并且内部没有变,也就是s0是一个可以染成同一种颜色的结点集
#include<bits/stdc++.h>using namespace std;#define LL long long#define inf 0x3f3f3fLL dp[1<<18];char mp[20][20];int vis[1<<18];int main(){ int T;scanf("%d",&T);while(T--){memset(vis,0,sizeof(vis));int n;scanf("%d",&n);for(int i= 1;i<=n;i++)scanf(" %s",mp[i]+1);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) && mp[j+1][k+1]=='1'){vis[i]=1;break;}}}if(vis[i])break;}}dp[0]=0;for(int s = 1;s<(1<<n);s++){ dp[s]=inf;for(int s0 = s;s0;s0=(s0-1)&s){if(!vis[s0])dp[s]=min(dp[s],dp[s^s0]+1);}}LL ans = 0;LL f = 1;for(int i = 1;i<(1<<n);i++){f*=233; ans +=(dp[i]*f);}printf("%u\n",(int)ans);}}
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 isid(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
You need to output
Sample Input
24011010101101001040111101011011010
Sample Output
10224233542538351020HintFor the first test case, ans[1..15]= {1, 1, 2, 1, 2, 2, 3, 1, 1, 1, 2, 2, 2, 2, 3}
Author
学军中学
0 0
- hdu5823 color II(状态压缩DP)
- 【HDU5823】color II(状压DP)
- hdu5823 color II 【状压dp(非正解)】
- HDU5823 color II
- hdu5823 color II
- hdu 5823 color II(状态压缩DP) ★
- HDU5823(2016多校第八场)——color II (状压dp,独立集)
- HDU5823(状压DP)
- BNU25359Escape Time II(状态压缩DP)
- 2016多校联赛7C (hdu5823) color II
- 2016多校训练Contest8: 1003 color II hdu5823
- NYOJ 515 完全覆盖 II (状态压缩dp)
- HDU 5823 color II(状压DP)
- hdu1565(状态压缩dp)
- 状态压缩DP(二)
- 状态压缩dp(hdu3406)
- poj2411(状态压缩dp)
- poj1185(状态压缩dp)
- 并发编程Lock
- php 操作mogo常用命令
- hdu 1000 解析
- Solr基本概念
- synchronized详解
- hdu5823 color II(状态压缩DP)
- zookeeper在linux centos7下的集群介绍和配置
- 数据结构—线性表(约瑟夫环)
- Codeforces Round #353 (Div. 2) C. Money Transfers(相邻银行之间转账,求最少的转账次数)
- C++对象的内存分析(2)
- 理解Spring IOC
- Linux配置Tomcat
- java中的对象数组排序,--Comparable接口
- 剑指Offer--编程题参考代码(1)