Test instructions
n Cattle and M-barn, each cow has its own favorite P-Barn (1<=p<=m), asking how many ways to arrange for the N-cow.
Analysis:
DP[I][J] Represents the number of methods of the I cow in J state.
DP[I][J] = Sigma (Dp[i-1][k]) (k from 0 to 1<<m).
To be optimized with a scrolling array, otherwise dp[20][1<<20] will mle!
Note the emptying of the scrolling array = =!
1#include <stdio.h>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cstdlib>6#include <cmath>7#include <vector>8#include <queue>9#include <map>Ten#include <Set> One A using namespacestd; - -typedefLong Longll; the -vector<int> p[ -]; - intdp[2][1<< -],n; - + intMain () - { + intm,x,y; A while(~SCANF ("%d%d",&n,&m)) at { - for(intI=1; i<=n;i++) - { - p[i].clear (); -scanf"%d",&x); - while(x--) in { -scanf"%d",&y); to p[i].push_back (y); + } - } theMemset (DP,0,sizeof(DP)); * $ intAll = (1<<M)-1;Panax Notoginsengdp[0][0] =1; - intT; the + for(intI=1; i<=n;i++) A { the for(intk=0; k<=all;k++)//every state access once + { - if(!dp[(i1) &1][K])Continue;//It is only when the K-state of the former I-1 cow has a value that it is possible to determine the method of the first cow $ for(intj=0; J<p[i].size (); j + +) $ { -t = p[i][j]-1; - if((k>>t) &1)Continue;//If there is a cow in the K state, it cannot be placed . thedp[i&1][k| (1<<T)] + = dp[(i-1) &1][k]; - }Wuyi } thememset (dp[(i-1) &1],0,sizeof(Dp[(i1) &1]));//clear the next dimension to use for the scroll array - } Wu - intAns =0; About for(intI=1; i<=all;i++) $ { -ans+=dp[n&1][i]; - } -printf"%d\n", ans); A } + return 0; the}
View Code
POJ 2441 Arrange The Bulls (state compression dp+ scrolling array)