考虑容斥,枚举哪些不存在的边选中了,剩下的不管,则可以用组合数计算方案数。
时间复杂度$O(m2^m+nm)$。
#include<cstdio>
const int N=550,B=10000,MAXL=350;
int n,m,S,i,j,e[N][2],g[N],f[N];
inline int max(int a,int b){return a>b?a:b;}
struct Num{int a[MAXL],len,fu;Num(){len=1,fu=a[1]=0;}Num operator+(const Num&b){Num c;c.len=max(len,b.len)+2;int i;for(i=1;i<=c.len;i++)c.a[i]=0;if(fu==b.fu){for(i=1;i<=len;i++)c.a[i]=a[i];for(i=1;i<=b.len;i++)c.a[i]+=b.a[i];for(i=1;i<=c.len;i++)if(c.a[i]>=B)c.a[i+1]++,c.a[i]-=B;while(c.len>1&&!c.a[c.len])c.len--;c.fu=fu;}else{bool flag=0;if(len==b.len){for(i=len;i;i--)if(a[i]!=b.a[i]){if(a[i]>b.a[i])flag=1;break;}}else{if(len>b.len)flag=1;}if(flag){for(i=1;i<=len;i++)c.a[i]=a[i];for(i=1;i<=b.len;i++)c.a[i]-=b.a[i];for(i=1;i<=c.len;i++)if(c.a[i]<0)c.a[i+1]--,c.a[i]+=B;while(c.len>1&&!c.a[c.len])c.len--;c.fu=fu;}else{for(i=1;i<=b.len;i++)c.a[i]=b.a[i];for(i=1;i<=len;i++)c.a[i]-=a[i];for(i=1;i<=c.len;i++)if(c.a[i]<0)c.a[i+1]--,c.a[i]+=B;while(c.len>1&&!c.a[c.len])c.len--;c.fu=b.fu;}}return c;}Num operator*(const Num&b){Num c;c.len=len+b.len+2;c.fu=fu^b.fu;int i,j;for(i=1;i<=c.len;i++)c.a[i]=0;for(i=1;i<=len;i++)for(j=1;j<=b.len;j++){c.a[i+j-1]+=a[i]*b.a[j];if(c.a[i+j-1]>=B){c.a[i+j]+=c.a[i+j-1]/B;c.a[i+j-1]%=B;if(c.a[i+j]>=B)c.a[i+j+1]+=c.a[i+j]/B,c.a[i+j]%=B;}}while(c.len>1&&!c.a[c.len])c.len--;return c;}void operator/=(int b){int i;for(i=len;i;i--)a[i-1]+=a[i]%b*B,a[i]/=b;while(len>1&&!a[len])len--;}bool iszero(){return len==1&&!a[1];}void write(){if(len==1&&!a[1])fu=0;if(fu)putchar('-');printf("%d",a[len]);for(int i=len-1;i;i--)printf("%04d",a[i]);puts("");}void set(int x){fu=0;if(x<0)x*=-1,fu^=1;if(x>=B){len=2;a[1]=x%B;a[2]=x/B;}else{len=1;a[1]=x;}}
}ans[N],tmp,now;
int main(){scanf("%d%d",&n,&m);for(i=0;i<m;i++)scanf("%d%d",&e[i][0],&e[i][1]);for(i=0;i<m;i++)for(j=0;j<m;j++)if(i!=j&&(e[i][0]==e[j][0]||e[i][1]==e[j][1]))g[i]|=1<<j;for(S=0;S<1<<m;S++){for(i=0;i<m;i++)if(S>>i&1)if(S&g[i])break;if(i==m)f[__builtin_popcount(S)]+=__builtin_popcount(S)&1?-1:1;}for(i=0;i<=n;i++)if(f[i]){tmp.set(f[i]);for(j=0;j<=n-i;j++){if(j){now.set((n-i-j+1)*(n-i-j+1));tmp=tmp*now;}if(j>1)tmp/=j;ans[i+j]=ans[i+j]+tmp;}}for(i=0;i<=n;i++)ans[i].write();return 0;
}