看了下别人的实现感觉不科学写的那么复杂然而速度是我的1/2= =
我们看到这个很容易联想到棋盘模型n*m,放置n个车,问方案数.
然后愉快地做就行,最简复杂度O(n*2^m),然而窝写成O(m*2^m)其实也无所谓
Problem: 2441 User: BPM136
Memory: 4812K Time: 235MS
Language: G++ Result: Accepted
#include<iostream>
#include<cstdio>
#include<bitset>
#define LL long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
inline LL read()
{
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
#define N 25
#define M 20
unsigned long f[1<<M]={1};
bool map[N][N];
int n,m;
bitset<N>t;
int main()
{
n=read(),m=read();
if(n>m){puts("0");return 0;}
fo(i,1,n){int k=read();while(k--)map[i][read()]=1;}
LL ans=0;
fo(i,1,(1<<m)-1)
{
t=i;int num=t.count();
if(num>n)continue;
fo(k,0,m-1)
{
if((!(i&(1<<k)))||(!(map[num][k+1])))continue;
f[i]+=f[i-(1<<k)];
}
if(num==n)ans+=f[i];
}
cout<<ans<<endl;
return 0;
}