题解 P8455
前言
最优解。多头说话。
题解
考虑将格子分类。
首先第一种是周围没有雷的格子,这些格子显然对于每个极大连通块考虑即可。
第二种是本身是雷的格子,这部分贡献是独立的,在有雷的时候将答案
第三种是周围有雷的格子,可以分为三类:
- 周围没有第一种格子,那么这部分贡献也是独立的,将答案
\times 2 。 - 周围有第一种格子,且这些第一种格子都在同一个连通块内,那么我们在不选该连通块时将答案
\times 2 。 - 周围有第一种格子,且这些第一种格子在两个不同的连通块里,那么我们在不选这两个连通块时将答案
\times 2 。
不难发现一个周围有雷的格子的周围至多只有两个第一种格子,因此所有格子已经被分类完成。
问题转化成下面这个经典的形式:
- 给定一张
n 个点的平面无向图G 。 - 对于每个点有点权
a_i ,对于每条边有边权b_i 。 - 对于每一张导出子图
G' ,其权重为所有点的点权和所有边的边权的乘积。 - 计算
2^n 个导出子图的权值和。 -
首先我们知道这题铁 NPC。
但是我们发现
考虑将所有点按照点度排序,记录所有边的选择情况转移,在图不连通时将问题分解为若干子问题继续搜索,可以通过,最慢点
代码
征集新注释。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
constexpr bitset<160> Z=(unsigned ll)-1;
#define bs bitset<160>
struct custom_hash {
static inline uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
inline size_t operator()(const bs&x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64((x&Z).to_ullong()
+((x>>64)&Z).to_ullong()+FIXED_RANDOM);
}
};
const int p=998244353;
unordered_map<bs,int,custom_hash> mp[63];
int to[63][63],emul[63][63];
int mul[63],rk[63],id[63];
vector<int> e[63];
vector<int> ee[63];
int n,m;
vector<bs> sg[63];
inline int dfs(int x,const bs&s)
{
if(x==n) return 1;
if(s[id[x]]) return dfs(x+1,s);
if(mp[x].count(s)) return mp[x][s];
bs g=s;
int u=id[x],ex=1;
g[u]=1;
for(int v:e[u])
if(rk[v]<rk[u]&&!g[to[u][v]])
ex=1ll*ex*emul[u][v]%p,
g[to[u][v]]=1;
int res=0;
if(sg[x].size()>1)
{
int z=1;
for(bs &i:sg[x]) z=1ll*z*dfs(x+1,g|i)%p;
res=(res+z)%p;
}
else res=dfs(x+1,g);
res=1ll*mul[u]*ex%p*res%p;
for(int v:ee[u]) g[to[u][v]]=1;
if(sg[x].size()>1)
{
int z=1;
for(bs &i:sg[x]) z=1ll*z*dfs(x+1,g|i)%p;
res=(res+z)%p;
}
else res=(res+dfs(x+1,g))%p;
return mp[x][s]=res;
}
bs tmp;
bool tv[63];
void pre(int x,int lim)
{
tv[x]=1,tmp[x]=0;
for(int y:e[x]) if(rk[y]>=lim)
{
tmp[to[x][y]]=0;
if(!tv[y]) pre(y,lim);
}
else tmp[to[x][y]]=0;
}
char str[503][503];
int num[503][503];
int bel[503][503];
void dfs(int x,int y,int v)
{
bel[x][y]=v;
for(int dx=-1; dx<=1; ++dx)
for(int dy=-1; dy<=1; ++dy)
if(bel[x+dx][y+dy]==-1&&!num[x+dx][y+dy]&&str[x+dx][y+dy]=='.')
dfs(x+dx,y+dy,v);
return ;
}
signed main()
{
read();
int N=read(),M=read(),qwq=0;
for(int i=1; i<=N; ++i) scanf("%s",str[i]+1);
for(int i=1; i<=N; ++i)
for(int j=1; j<=M; ++j)
for(int di=-1; di<=1; ++di)
for(int dj=-1; dj<=1; ++dj)
num[i][j]+=(str[i+di][j+dj]=='*');
for(int i=1; i<=N; ++i)
for(int j=1; j<=M; ++j)
qwq|=(str[i][j]=='*');
memset(bel,-1,sizeof(bel)),
memset(to,-1,sizeof(to)),++qwq;
for(int i=1; i<=N; ++i)
for(int j=1; j<=M; ++j)
if(!num[i][j]&&bel[i][j]==-1)
mul[n]=1,dfs(i,j,n++);
m=n;
for(int i=1; i<=N; ++i)
for(int j=1; j<=M; ++j) if(str[i][j]=='.'&&num[i][j])
{
int fi=-1,se=-1;
for(int di=-1; di<=1; ++di)
for(int dj=-1; dj<=1; ++dj)
if(bel[i+di][j+dj]!=-1)
{
if(fi==-1) fi=bel[i+di][j+dj];
else if(fi!=bel[i+di][j+dj]) se=bel[i+di][j+dj];
}
if(fi==-1&&se==-1) qwq=(qwq<<1)%p;
else if(se==-1||fi==se) mul[fi]=(mul[fi]<<1)%p;
else
{
if(to[fi][se]==-1)
e[fi].push_back(se),
e[se].push_back(fi),
to[fi][se]=to[se][fi]=m++,
emul[fi][se]=emul[se][fi]=1;
emul[fi][se]=emul[se][fi]=(emul[se][fi]<<1)%p;
}
}
for(int i=0; i<n; ++i) id[i]=i;
sort(id,id+n,[&](int x,int y){return e[x].size()>e[y].size();});
for(int i=0; i<n; ++i) rk[id[i]]=i;
for(int u=0; u<n; ++u)
for(int v:e[u])
if(rk[v]>rk[u])
ee[u].push_back(v);
bs emp;
for(int i=0; i<n; ++i)
{
memset(tv,0,sizeof(tv));
for(int j=i+1; j<n; ++j)
if(!tv[id[j]])
{
tmp.set(),pre(id[j],i);
sg[i].push_back(tmp);
}
}
printf("%lld\n",1ll*qwq*dfs(0,emp)%p);
// for(int i=0; i<n; ++i) printf("%d\n",mp[i].size());
return 0;
}