hdu 3091 Necklace(状态压缩类似于TSP问题)
2024-10-15 02:26:20
Necklace
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others)
Total Submission(s): 709 Accepted Submission(s): 245
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.
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
1 2
1 3
2 3
Sample Output
2
Source
/*
已经给了时间看题解了,正式练习一道题三天之内不准再看题解!
*/
#include<iostream>
#include<string.h>
#include<stdio.h>
#define N (1<<18)+5
#define M 20
#define INF 0x3f3f3f3f
using namespace std;
long long dp[N][M],g[M][M],n,m;//dp[i][j]表示在i个状态下,你到达j这个点的方案数
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++)//枚举你要经过的中间点
{
if(dp[i][j]==0) continue;//上一个状态没有方案数的讨论没意义
//cout<<"ok"<<endl;
for(int k=1;k<n;k++)//枚举你这个状态最终要到达的终点
{
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;
}
最新文章
- 【荐】怎么用PHP发送HTTP请求(POST请求、GET请求)?
- 无阻塞加载js,防止因js加载不了影响页面显示
- Java数据库ResultSet转json实现
- ASP.NET缓存全解析6:数据库缓存依赖 转自网络原文作者李天平
- poj 2533 Longest Ordered Subsequence(LIS)
- 【MySQL 读书笔记】全局锁 | 表锁 | 行锁
- Java编程题:&#160;写一个Singleton出来
- 记一次laravel-jwt修改黑名单所用redis数据库
- Java 银联支付官网demo测试及项目整合代码
- 什么是Unicode
- JS传值中文乱码解决方案
- Java 整体测试重点题 错题积累
- 20145333茹翔 Exp5 MS11_050
- bootstrap table的展开行问题
- Maven中央仓库——你可能不知道的细节
- java线程操作
- [AngularJS] Angular 1.3 ng-model-options - getterSetter
- java Calender类
- 2013年未之wpf项目乱述
- 3*0.1 == 0.3 将会返回什么?true 还是 false?
热门文章
- NameError: name &#39;messagebox&#39; is not defined 错误处理
- LINUX通过PXE自动部署系统
- Java客户端调用.NET的WebService
- Springmvc学习笔记(一)
- UI自动化测试(四)AutoIT工具使用和robot对象模拟键盘按键操作
- Gradient Boost 算法流程分析
- form表单中enctype=";multipart/form-data";的传值问题
- .Neter玩转Linux系列之五:crontab使用详解和Linux的进程管理以及网络状态监控
- 无向图广度优先遍历及其matlab实现
- PE文件格式详解,第二讲,NT头文件格式,以及文件头格式