O - Necklace (状态dp)
2017-12-06 21:18
204 查看
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.
InputIt 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.
OutputAn integer , which means the number of kinds that the necklace could be.
Sample Input
Sample Output
题意:
给你几个贝壳,每个贝壳能和特定的贝壳相连,问最多可组成多少种项链?
思路:
状态dp,寻找每个位置
代码;'
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef __int64 ll;
using namespace std;
int n,m;
bool mp[25][25];
ll dp[1<<18][25];
void solve(){
int bit=1<<n;
memset(dp,0,sizeof(dp));
dp[1][0]=1;
for(int i=1;i<bit;++i){
for(int j=0;j<n;j++){
if(dp[i][j]==0)continue;
for(int k=1;k<n;k++){
if(i&(1<<k))continue;
if(!mp[j][k])continue;
dp[i|(1<<k)][k]+=dp[i][j];
}
}
}
ll ans=0;
for(int i=0;i<n;i++)
if(mp[0][i])ans+=dp[bit-1][i];
cout<<ans<<endl;
}
int main(){
ios::sync_with_stdio(false);
int x,y;
int i;
while(cin>>n>>m){
memset(mp,0,sizeof(mp));
for(i=0;i<m;i++){
cin>>x>>y;
--x,--y;
mp[x][y]=mp[y][x]=1;
}
solve();
}
return 0;
}
he can make.
InputIt 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.
OutputAn 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
题意:
给你几个贝壳,每个贝壳能和特定的贝壳相连,问最多可组成多少种项链?
思路:
状态dp,寻找每个位置
代码;'
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef __int64 ll;
using namespace std;
int n,m;
bool mp[25][25];
ll dp[1<<18][25];
void solve(){
int bit=1<<n;
memset(dp,0,sizeof(dp));
dp[1][0]=1;
for(int i=1;i<bit;++i){
for(int j=0;j<n;j++){
if(dp[i][j]==0)continue;
for(int k=1;k<n;k++){
if(i&(1<<k))continue;
if(!mp[j][k])continue;
dp[i|(1<<k)][k]+=dp[i][j];
}
}
}
ll ans=0;
for(int i=0;i<n;i++)
if(mp[0][i])ans+=dp[bit-1][i];
cout<<ans<<endl;
}
int main(){
ios::sync_with_stdio(false);
int x,y;
int i;
while(cin>>n>>m){
memset(mp,0,sizeof(mp));
for(i=0;i<m;i++){
cin>>x>>y;
--x,--y;
mp[x][y]=mp[y][x]=1;
}
solve();
}
return 0;
}
相关文章推荐
- HDU 3091 Necklace (状态压缩dp)
- HDU 5025 状态压缩蛇+bfs+dp
- HDOJ 1074.Doing Homework(状态压缩DP)
- HDU 4778 Gems Fight! 状态压缩dp
- hdu1584(状态压缩DP)
- HDU 5045 Contest(概率dp,状态压缩,2014上海网络赛1004)
- HDOJ4336Card Collector【概率dp求期望+状态压缩】
- CodeForces Round #191 (327E) - Axis Walking 状态DP..
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- (状态压缩DP) poj 2978
- 一道状态压缩DP思想题
- POJ 1038 - Bugs Integrated, Inc. 三进制状态DP
- 20140930 【 DP - 状态压缩 】 2014-上海区域赛-网络预选赛 hdoj 5045 Contest
- HDU5691 状态压缩dp
- P1193扫雷 (DP状态压缩)
- poj 1185 炮兵阵地 (状态压缩DP)
- POJ 2288 Islands and Bridges (TSP 状态压缩DP)
- 状态压缩DP
- SGU 327 Yet Another Palindrome(状态压缩DP)
- POJ2686 状态压缩dp