Arrange the Bulls
Time Limit: 4000MS |
|
Memory Limit: 65536K |
Total Submissions: 3709 |
|
Accepted: 1422 |
Description
Farmer Johnson ' s Bulls love playing basketball very much. But none of them would like to play basketball with the other bulls because they believe that the others is all very weak . Farmer Johnson has N cows (we number the cows from 1 to N) and M-Barns (We number the barns from 1 to M), which was his bul ls ' Basketball fields. However, his bulls is all very captious, they is like to play in some specific barns, and don ' t want to share a barn WI Th the others.
So it's difficult for Farmer Johnson to arrange him bulls, he wants you to help him. Of course, find one solution is easy, but your task was to find what many solutions there are.
You should know, a solution is a situation that every bull can play basketball in a barn he likes and no both Bulls Sha Re a barn.
The problem a little easy, it's assumed that number of solutions would not exceed 10000000.
Input
In the first line of input contains the integers n and M (1 <= n <=, 1 <= M <= 20). Then come N lines. The i-th line first contains a integer p (1 <= p <= M) referring to the number of barns cow I likes to play in. Then follow P integers, which give the number of there P barns.
Output
Print a single integer in a line, which is the number of solutions.
Sample Input
3 42 1 42 1 32 2 4
Sample Output
4
Source
POJ monthly,tn Test instructions: N cow m tent, N cow into m tent scheme number
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib > #include <algorithm> #include <string>using namespace Std;int n,m,mp[21][21];int dp[(1<<20) +2]; int main () {scanf ("%d%d", &n,&m); for (int i=0;i<n;i++) {int x, y; scanf ("%d", &x); while (x--) {scanf ("%d", &y); y--; Mp[i+1][y]=1; }} if (N>m) {printf ("0\n"); return 0; } dp[0]=1; for (int i=0;i<n;i++) {for (int j= (1<<m) -1;j>=0;j--) {if (dp[j]==0) Continue for (int k=0;k<m;k++) {if ((j& (1<<k))!=0) continue; if (mp[i+1][k]==0) continue; Dp[j| ( 1<<k)]+=dp[j]; } dp[j]=0; }} int ans=0; for (int i=0;i< (1<<m); i++) ans+=Dp[i]; printf ("%d\n", ans); return 0;}
(State compression DP) POJ 2441