Necklace
Time limit:2000/1000 MS (java/others) Memory limit:327680/327680 K (java/others)
Total submission (s): 709 Accepted Submission (s): 245
Problem Descriptionone Day, Partychen gets several beads, he wants to make these beads a necklace. But the every beads can link to the other, every bead should link to some particular bead (s). Now, Partychen wants to know what many kinds of necklace he can make.
InputIt consists of multi-case.
Every case start with integers n,m (1<=n<=18,m<=n*n)
The followed M lines contains, integers a, B (1<=a,b<=n) which means the Ath bead and the bth bead is able to b E linked.
Outputan integer, which means the number of kinds, the necklace could be.
Sample INPUT3 31 21 32 3
Sample Output2
Source multi-university Training Contest 18-host by ECNU
/* has given time to see the problem, formally practicing a question within three days no longer see the puzzle! */#include <iostream> #include <string.h> #include <stdio.h> #define N (1<<18) +5#define M 20# Define INF 0x3f3f3f3fusing namespace Std;long long Dp[n][m],g[m][m],n,m;//dp[i][j] indicates the number of scenarios where you reach J at this point in the I state int main () {// Freopen ("In.txt", "R", stdin), while (scanf ("%lld%lld", &n,&m)!=eof) {memset (dp,0,sizeof dp); memset (g,0, sizeof g); int a,b;for (int i=0;i<m;i++) {scanf ("%lld%lld", &a,&b); g[a-1][b-1]=g[b-1][a-1]=1;} int tol= (1<<n);DP [1][0]=1;for (int i=1;i<tol;i++) {for (int j=0;j<n;j++)//Enumerate the intermediate points you want to pass {if (dp[i][j]==0) continue;//the discussion of a State without a scheme number is meaningless//cout<< "OK" <<endl;for (int k=1;k<n;k++)//Enumerate the end of your state to reach {if (! i& (1<<k)) &&g[j][k]) {//cout<< "OK" <<endl;dp[i| ( 1<<k)][k]+=dp[i][j];}}} Long long s=0;for (int i=0;i<n;i++) {//cout<<dp[tol-1][i]<< "", if (G[0][i]) s+=dp[tol-1][i];} cout<<endl;printf ("%lld\n", s);} return 0;}
HDU 3091 Necklace (state compression similar to TSP problem)