题解:AT_arc171_d [ARC171D] Rolling Hash
OldDriverTree · · 题解
Solution
令
对于一个限制
对于一个限制,就可以转化为
如果存在一组
那么判断是否存在合法的
考虑把每组限制的
考虑状压
再设
如果 Yes,否则答案为 No,其中
Code
#include<bits/stdc++.h>
const int N=17,M=1<<N;
using namespace std;
int P,n,m,G[N][N];
int g[M],f[M];
int read() {
int x=0; char c=0; while (!isdigit(c) ) c=getchar();
while (isdigit(c) ) x=(x<<3)+(x<<1)+(c&15),c=getchar();
return x;
}
int main()
{
P=read(),read(),n=read(),m=read();
while (m--) {
int l=read()-1,r=read();
G[l][r]=G[r][l]=true;
}
memset(f,0x3f,sizeof f);
int ALL=(1<<n+1)-1; g[0]=true,f[0]=0;
for (int S=1;S<=ALL;S++) if (g[S&(S-1)]) {
int x=__lg(S&-S),s=S&(S-1); g[S]=true;
for (int i=0;i<=n;i++) if (S>>i&1&&G[i][x]) { g[S]=false; break; }
}
for (int S=1;S<=ALL;S++)
for (int s=S;s;s=(s-1)&S)
if (g[s]) f[S]=min(f[S],f[S^s]+1);
printf(f[ALL]<=P?"Yes":"No");
return 0;
}