您的位置:首页 > 其它

HDU 5823 color II

2016-08-11 18:52 232 查看
给定一个n个点的无向图,求每个点集的导出子图的最小染色。

n≤18.

开始不知道怎么状压求最小染色,所以原来是枚举连通块然后试图贪心解最小染色高维前缀和算一下各个子集的最小染色的最大值就好了,然后很快的WA了

后来发现网上有状压求最小染色的方法==

后来写了一个O(3^n)但不太相信能过,之前一些没用的预处理也没删,结果TLE了==

后来比赛结束了==

后来看了题解知道O(3^n)放过去了==,删预处理==,AC==

长点教训啊喂:(

后来听Kyle的话把比赛时的代码重交了一遍==,AC

我理解身为服务器的压力,但还是有些伤心

dp[S]=minS′⊆Sdp[S−S′]+1,其中S′是独立集

集合

#include <set>
#include <ctime>
#include <queue>
#include <cstdio>
#include <bitset>
#include <cctype>
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf (1<<30)
#define INF (1ll<<62)
#define fi first
#define se second
#define rep(x,s,t) for(register int x=s,t_=t;x<t_;++x)
#define per(x,s,t) for(register int x=t-1,s_=s;x>=s_;--x)
#define prt(x) cout<<#x<<":"<<x<<" "
#define prtn(x) cout<<#x<<":"<<x<<endl
#define pb(x) push_back(x)
#define lb(x) ((x)&(-(x)))
#define hash asfmaljkg
#define rank asfjhgskjf
#define y1 asggnja
#define y2 slfvm
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef unsigned int uint;

template<class T>void sc(T &x){
int f=1;char c;x=0;
while(c=getchar(),c<48)if(c=='-')f=-1;
do x=x*10+(c^48);
while(c=getchar(),c>47);
x*=f;
}
template<class T>void nt(T x){
if(!x)return;
nt(x/10);
putchar(x%10+'0');
}
template<class T>void pt(T x){
if(x<0)putchar('-'),x=-x;
if(!x)putchar('0');
else nt(x);
}
template<class T>void ptn(T x){
pt(x);putchar('\n');
}
template<class T>void pts(T x){
pt(x);putchar(' ');
}
template<class T>inline void Max(T &x,T y){if(x<y)x=y;}
template<class T>inline void Min(T &x,T y){if(x>y)x=y;}

int n;
const int maxn=18;
int b[1<<maxn];
int g[1<<maxn];
int col[1<<maxn];
uint pw[1<<maxn];
char ch[maxn];

void solve(){
sc(n);
rep(i,0,n){
scanf("%s",ch);
b[1<<i]=0;rep(j,0,n)if(ch[j]=='1')b[1<<i]|=1<<j;
}
g[0]=true;
rep(i,1,1<<n){
int k=i&-i,p=i^k;
if(!g[p]||b[k]&p)g[i]=0;
else g[i]=1;
}
rep(i,1,1<<n){
if(g[i]){
col[i]=1;
continue;
}
col[i]=inf;
int j=i^(i&-i);
for(int j=i;j;j=(j-1)&i)
if(g[i^j])Min(col[i],col[j]+1);
}

uint ans=0;
rep(i,1,1<<n)
ans+=(uint)col[i]*pw[i];
ptn(ans);
}

int main(){
//  freopen("pro.in","r",stdin);
//  freopen("chk.out","w",stdout);
pw[0]=1;
rep(i,1,1<<maxn)pw[i]=pw[i-1]*233;//prt(pw[i]);

int cas;sc(cas);
while(cas--)solve();
return 0;
}


恭喜你发现了蒟蒻n次or卷积的做法

蒟蒻:数组开小了

每次用着色数为x的集合与独立集做or卷积得到着色数为x+1的集合

#include <set>
#include <ctime>
#include <queue>
#include <cstdio>
#include <bitset>
#include <cctype>
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf (1<<30)
#define INF (1ll<<62)
#define fi first
#define se second
#define rep(x,s,t) for(register int x=s,t_=t;x<t_;++x)
#define per(x,s,t) for(register int x=t-1,s_=s;x>=s_;--x)
#define prt(x) cout<<#x<<":"<<x<<" "
#define prtn(x) cout<<#x<<":"<<x<<endl
#define pb(x) push_back(x)
#define lb(x) ((x)&(-(x)))
#define hash asfmaljkg
#define rank asfjhgskjf
#define y1 asggnja
#define y2 slfvm
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef unsigned int uint;

template<class T>void sc(T &x){
int f=1;char c;x=0;
while(c=getchar(),c<48)if(c=='-')f=-1;
do x=x*10+(c^48);
while(c=getchar(),c>47);
x*=f;
}
template<class T>void nt(T x){
if(!x)return;
nt(x/10);
putchar(x%10+'0');
}
template<class T>void pt(T x){
if(x<0)putchar('-'),x=-x;
if(!x)putchar('0');
else nt(x);
}
template<class T>void ptn(T x){
pt(x);putchar('\n');
}
template<class T>void pts(T x){
pt(x);putchar(' ');
}
template<class T>inline void Max(T &x,T y){if(x<y)x=y;}
template<class T>inline void Min(T &x,T y){if(x>y)x=y;}

int n;
const int maxn=18;
int b[1<<maxn];
int col[1<<maxn];
uint pw[1<<maxn];
char ch[maxn];

int g[1<<maxn];
int x[1<<maxn],y[1<<maxn];

void ormul(int *a,int *b,int *c){
static int a_[1<<maxn],b_[1<<maxn];
rep(i,0,1<<n)a_[i]=a[i],b_[i]=b[i];
rep(i,0,n)rep(j,1,1<<n)
if(j>>i&1){
a_[j]+=a_[j^1<<i];
b_[j]+=b_[j^1<<i];
}
rep(i,0,1<<n)c[i]=a_[i]*b_[i];

rep(i,0,n)rep(j,1,1<<n)
if(j>>i&1)c[j]-=c[j^1<<i];
}

void solve(){
sc(n);
rep(i,0,n){
scanf("%s",ch);
b[1<<i]=0;rep(j,0,n)if(ch[j]=='1')b[1<<i]|=1<<j;
}
g[0]=true;
rep(i,1,1<<n){
int k=i&-i,p=i^k;
if(!g[p]||b[k]&p)g[i]=0;
else g[i]=1;
}
rep(i,0,1<<n)col[i]=g[i]?1:0;

rep(cas,2,n+1){
if(col[(1<<n)-1])break;
rep(i,0,1<<n)x[i]=(col[i]>0);
ormul(x,g,y);
rep(i,0,1<<n)if(y[i]&&!col[i])col[i]=cas;
}

uint ans=0;
rep(i,1,1<<n)
ans+=(uint)col[i]*pw[i];
ptn(ans);
}

int main(){
//  freopen("pro.in","r",stdin);
//  freopen("chk.out","w",stdout);
pw[0]=1;
rep(i,1,1<<maxn)pw[i]=pw[i-1]*233;//prt(pw[i]);

int cas;sc(cas);
while(cas--)solve();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: