博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2441 Arrange the Bulls
阅读量:4958 次
发布时间:2019-06-12

本文共 2597 字,大约阅读时间需要 8 分钟。

Arrange the Bulls
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 5427   Accepted: 2069

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 42 1 42 1 32 2 4

Sample Output

4 题意:把n头牛放进m个棚,并且每头牛都要住进自己喜欢的棚,一共有多少放法。 思路:状压dp,dp[i][j]:前i头牛的放法是状态j时的放法数。 转移方程:定义dp[i-1][k],即前i-1头牛放法是状态k,并且设第i头牛要放入位置j,if(k&(1<
<
#define _CRT_SECURE_NO_DEPRECATE#include 
#include
#include
#include
#include
using namespace std;#define N_MAX 21#define M_MAX 21#define MOD 10000000#define INF 0x3f3f3f3fint n, m;vector
can_in[N_MAX];int dp[2][1 << M_MAX];int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { int p; scanf("%d", &p); while (p--) { int a; scanf("%d", &a); a--; can_in[i].push_back(a); } } int allstates = 1 << m; for (int j = 0; j < can_in[0].size(); j++) { dp[0][1 << can_in[0][j]] = 1; } for (int i = 1; i < n; i++) { for (int j = 0; j < can_in[i].size(); j++) { for (int k = 0; k < allstates; k++) { if (dp[(i - 1)&1][k] && !(k >> can_in[i][j] & 1)) { dp[i&1][k | 1 << can_in[i][j]] += dp[(i - 1)&1][k]; } } } } int sum = 0; for (int i = 0; i < allstates; i++) { bitset<32>bit(i); if(bit.count()==n) sum += dp[(n - 1) & 1][i]; } printf("%d\n", sum); return 0;}

 

转载于:https://www.cnblogs.com/ZefengYao/p/8727601.html

你可能感兴趣的文章
【译】Hello Kubernetes快速交互实验手册
查看>>
appium(13)- server config
查看>>
图形学噪声解析
查看>>
IIS负载均衡-Application Request Route详解第六篇:使用失败请求跟踪规则来诊断ARR...
查看>>
管理信息系统 第三部分 作业
查看>>
织梦首页TAG标签页的仿制
查看>>
织梦系统调用点击次数代码优化提高页面打开速度
查看>>
[Leetcode Week13]Search a 2D Matrix
查看>>
通过被调函数改变主调函数的值
查看>>
android 数据存储之SQLite
查看>>
java 对象的序列化与反序列化
查看>>
luogu最长连号
查看>>
二叉树、树、森林
查看>>
查看端口占用cmd命令
查看>>
2019.01.17王苛震作业
查看>>
解决package jdk1.8-2000:1.8.0_171-fcs.x86_64 is already installed问题
查看>>
XPath Helper和XPath语法
查看>>
Halcon学习(八)文本操作
查看>>
MFC电子词典
查看>>
简单工厂(Simple Factory)
查看>>