hdu 3091 Necklace 状态压缩dp *******

Necklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)
Total Submission(s): 522    Accepted Submission(s): 168


Problem Description
One day , Partychen gets several beads , he wants to make these beads a necklace . But not every beads can link to each other, every bead should link to some particular bead(s). Now , Partychen wants to know how many kinds of necklace he can make.
 

 

Input
It consists of multi-case .
Every case start with two integers N,M ( 1<=N<=18,M<=N*N )
The followed M lines contains two integers a,b ( 1<=a,b<=N ) which means the ath bead and the bth bead are able to be linked.
 

 

Output
An integer , which means the number of kinds that the necklace could be.
 

 

Sample Input
3 3
1 2
1 3
2 3
Sample Output
2
 
 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<cstdlib>

 5 #include<vector>

 6 using namespace std;

 7 

 8 int n,m;

 9 vector <int> a[20];

10 long long dp[(1<<18)+2][20];

11 void make_ini()

12 {

13     int i,j;

14     int s,r,u;

15     memset(dp,0,sizeof(dp));

16     dp[1][0]=1;

17     for(i=1;i<(1<<n);i++)

18     {

19         for(j=0;j<n;j++)

20         {

21             if(dp[i][j]==0)continue;

22             for(s=0;s<a[j].size();s++)

23             {

24                 u=a[j][s];

25                 if( (i&(1<<u))>0 ) continue;

26                 r=( i | (1<<u) );

27                 dp[r][u]+=dp[i][j];

28             }

29         }

30     }

31     long long Sum=0;

32     for(i=0;i<a[0].size();i++)

33         Sum+=dp[(1<<n)-1][a[0][i]];

34         printf("%I64d\n",Sum);

35 }

36 int main()

37 {

38     int i,x,y;

39     while(scanf("%d%d",&n,&m)>0)

40     {

41         for(i=0;i<n;i++)

42         {

43             a[i].clear();

44         }

45         for(i=1;i<=m;i++)

46         {

47             scanf("%d%d",&x,&y);

48             x--;

49             y--;

50             a[x].push_back(y);

51             a[y].push_back(x);

52         }

53         make_ini();

54     }

55     return 0;

56 }

 

你可能感兴趣的:(HDU)