poj2441状压dp

liylho liylho     2022-10-13     397

关键词:

 

Arrange the Bulls
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 5289   Accepted: 2033

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 are 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 is his bulls‘ basketball fields. However, his bulls are all very captious, they only like to play in some specific barns, and don’t want to share a barn with the others. 

So it is difficult for Farmer Johnson to arrange his bulls, he wants you to help him. Of course, find one solution is easy, but your task is to find how many solutions there are. 

You should know that a solution is a situation that every bull can play basketball in a barn he likes and no two bulls share a barn. 

To make the problem a little easy, it is assumed that the number of solutions will not exceed 10000000.

Input

In the first line of input contains two integers N and M (1 <= N <= 20, 1 <= M <= 20). Then come N lines. The i-th line first contains an 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 4
2 1 4
2 1 3
2 2 4

Sample Output

4

Source

题意:每只牛只想在特定的房间里玩,每个房间只能有一只牛,问你能够满足以上条件的情况有几种。

代码:

 1 //#include "bits/stdc++.h"
 2 #include "cstdio"
 3 #include "map"
 4 #include "set"
 5 #include "cmath"
 6 #include "queue"
 7 #include "vector"
 8 #include "string"
 9 #include "cstring"
10 #include "time.h"
11 #include "iostream"
12 #include "stdlib.h"
13 #include "algorithm"
14 #define db double
15 #define ll long long
16 //#define vec vector<ll>
17 #define Mt  vector<vec>
18 #define ci(x) scanf("%d",&x)
19 #define cd(x) scanf("%lf",&x)
20 #define cl(x) scanf("%lld",&x)
21 #define pi(x) printf("%d
",x)
22 #define pd(x) printf("%f
",x)
23 #define pl(x) printf("%lld
",x)
24 #define inf 0x3f3f3f3f
25 #define rep(i, x, y) for(int i=x;i<=y;i++)
26 const int N   = 1e5 + 5;
27 const int mod = 1e9 + 7;
28 const int MOD = mod - 1;
29 const db  eps = 1e-10;
30 const db  PI  = acos(-1.0);
31 using namespace std;
32 int f[1<<21],a[21][21];
33 int n,m;
34 int main()
35 {
36     while(scanf("%d%d",&n,&m)==2)
37     {
38         int x;
39         memset(a,0, sizeof(a));
40         memset(f,0, sizeof(f));
41         for(int i=0;i<n;i++){
42             ci(x);
43             for(int j=0;j<x;j++){
44                 int y;
45                 ci(y);
46                 a[i][y-1]=1;
47             }
48         }
49         if(n>m) {
50             puts("0");
51             continue;
52         }
53         f[0]=1;
54         for(int i=0;i<n;i++){//枚举n只牛
55             for(int j=(1<<m)-1;j>=0;j--){//枚举每一个集合
56                 if(f[j])//如果集合存在
57                 {
58                     for(int k=0;k<m;k++){//枚举每个房间
59                         if((j&(1<<k))!=0||!a[i][k]) continue;//集合已用此房间或牛不喜欢此房间
60                         int S=(1<<k)|j;//牛进入房间得到新的集合
61                         f[S]+=f[j];
62                     }
63                 }
64                 f[j]=0;//枚举的集合变为新集合,旧集合需要消除
65             }
66         }
67         ll ans=0;
68         for(int i=0;i<(1<<m);i++) ans+=f[i];//留下满足条件的
69         pl(ans);
70     }
71     return 0;
72 }

 

 

poj2411mondriaan'sdream(状压dp)

【POJ2411】Mondriaan‘sDream(状压dp)TimeLimit:3000MS MemoryLimit:65536KTotalSubmissions:14107 Accepted:8152DescriptionSquaresandrectanglesfascinatedthefamousDutchpainterPietMondriaan.Onenight,aft 查看详情

poj2411状压dp经典

Mondriaan‘sDreamTimeLimit:3000MS MemoryLimit:65536KTotalSubmissions:16771 Accepted:9683DescriptionSquaresandrectanglesfascinatedthefamousDutchpainterPietMondriaan.Onenight,afterproducingthed 查看详情

poj3311---hiewiththepie(状压dp)

题目链接 DescriptionThePizazzPizzeriapridesitselfindeliveringpizzastoitscustomersasfastaspossible.Unfortunately,duetocutbacks,theycanaffordtohireonlyonedrivertodothedeliveries.Hewillwaitfor1ormore(up 查看详情

poj3254&poj1185(状压dp入门)

CornFieldsTimeLimit: 2000MS MemoryLimit: 65536KTotalSubmissions: 16773 Accepted: 8860DescriptionFarmerJohnhaspurchasedalushnewrectangularpasturecomposedof M by& 查看详情

poj3254(状压dp)(代码片段)

CornFieldsTimeLimit:2000MS MemoryLimit:65536KTotalSubmissions:19518 Accepted:10243DescriptionFarmerJohnhaspurchasedalushnewrectangularpasturecomposedofMbyN(1≤M≤12;1≤N≤12)squareparcels.Hewant 查看详情

poj3254(状压dp)(代码片段)

CornFieldsTimeLimit: 2000MS MemoryLimit: 65536KTotalSubmissions: 20975 Accepted: 10998DescriptionFarmerJohnhaspurchasedalushnewrectangularpasturecomposedof M by 查看详情

poj3254cornfields(状压dp)

题目链接:http://poj.org/problem?id=3254DescriptionFarmerJohnhaspurchasedalushnewrectangularpasturecomposedof M by N (1≤ M ≤12;1≤ N ≤12)squareparcels.Hewantstogrowso 查看详情

[poj3254]cornfields状压dp

DescriptionFarmerJohnhaspurchasedalushnewrectangularpasturecomposedof M by N (1≤ M ≤12;1≤ N ≤12)squareparcels.Hewantstogrowsomeyummycornforthecowsonanumberofsqu 查看详情

poj3254cornfields(状压dp)

...可以种菜的,而菜与菜之间不能相邻。问有多少种情况。状压dp入门题,将可以种菜的状态用一个数的二进制表示。第i行的状态只与上一行有关。此blog讲的很清楚:传送门1//#pragmacomment(linker,"/STACK:102400000,102400000")2#include<algor 查看详情

poj3254-cornfields-[状压dp水题]

 题目链接:http://poj.org/problem?id=3254 TimeLimit:2000MSMemoryLimit:65536KDescriptionFarmerJohnhaspurchasedalushnewrectangularpasturecomposedof M by N (1≤ M ≤12;1≤ 查看详情

hiewiththepie(poj3311)状压dp(代码片段)

DescriptionThePizazzPizzeriapridesitselfindeliveringpizzastoitscustomersasfastaspossible.Unfortunately,duetocutbacks,theycanaffordtohireonlyonedrivertodothedeliveries.Hewillwaitfor1ormore(upto10)order 查看详情

poj3254cornfields(状压dp)

...后求有多少种方式,在1上并且1不相邻。析:一个简单的状压DP,dp[i][s]表示第i行状态为s时有多少种,然后只要处理不相邻就行了,比赛进位运算写错了一个地方。。。。。代码如下:#pragmacomment(linker,"/STACK:1024000000,1024000000")#incl... 查看详情

poj2411mondriaan'sdream--状压dp

...少种填法。思路:  很久很久以前便做过的一道题目,状压DP,当时写得估计挺艰辛的,今天搜插头DP又搜到它,就先用状压DP写了下,顺利多了,没一会就出来了,可惜因为longlong没有1A。  思路挺简单,一行一行解决,每... 查看详情

poj292301背包+状态压缩/状压dp(代码片段)

题目链接EmmaandEricaremovingtotheirnewhousetheyboughtafterreturningfromtheirhoneymoon.Fortunately,theyhaveafewfriendshelpingthemrelocate.Tomovethefurniture,theyonlyhavetwocompactcars,whichcomplicatesevery 查看详情

[poj2411]mondriaan'sdream(状压dp)(插头dp)

Mondriaan‘sDreamTimeLimit: 3000MS MemoryLimit: 65536KTotalSubmissions: 18096 Accepted: 10357Description SquaresandrectanglesfascinatedthefamousDutchpainterPietMondri 查看详情

poj2288islandsandbridges——状压dp(代码片段)

题目:http://poj.org/problem?id=2288状压挺明显的;一开始写了(记忆化)搜索,但一直T;#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>usingnamespacestd;typedeflonglongll;intconstinf= 查看详情

poj3254cornfields状压dp(代码片段)

CornFieldsTimeLimit: 2000MS MemoryLimit: 65536KTotalSubmissions: 21931 Accepted: 11470DescriptionFarmerJohnhaspurchasedalushnewrectangularpasturecomposedof M by 查看详情

poj1185状压dp+优化

http://poj.org/problem?id=1185炮兵阵地TimeLimit:2000MS MemoryLimit:65536KTotalSubmissions:29176 Accepted:11303Description司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队。一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H"表... 查看详情