CF11D A Simple Task(状压DP)
$ solution: $
思路大家应该都懂:
状压DP: $ f[i][j] $ ,其中 $ i $ 这一维是需要状压的,用来记录19个节点每一个是否已经走过(走过为 $ 1 $ ,没走为 $ 0 $ ,用 $ 2 $ 进制 压缩一下即可)。同时,我们认为状压中已经走过的序号最小的节点为出发节点, $ j $ 即数组第二维是路径终点。(当这两个数相同时,说明找到了一个环)。
注:这种方法因为无向图的存在,会出现(同一条路径出现两次)(一条边和两个端点构成非法环)的情况,这只需要在输出答案时 $ ans=(ans-m)/2 $ 即可!
$ code: $
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define ll long long
#define db double
#define inf 0x7fffffff
#define rg register int
using namespace std;
int n,m,t,u,v;
bool a[19][19]; //存边
ll ans,f[600001][19]; //状压
inline int qr(){ //快读
char ch;
while((ch=getchar())<'0'||ch>'9');
int res=ch^48;
while((ch=getchar())>='0'&&ch<='9')
res=res*10+(ch^48);
return res;
}
int main(){
//freopen("hamilton.in","r",stdin);
//freopen("hamilton.out","w",stdout);
n=qr(),m=qr();t=1<<n;
for(rg i=1;i<=m;++i){
u=qr()-1;v=qr()-1;
a[u][v]=a[v][u]=1;//加边
}
for(rg i=0;i<n;++i)
f[1<<i][i]=1; //初始化(创建以i为起点的路径)
for(rg i=1;i<=t;++i){
for(rg j=0;j<n;++j){
if(!f[i][j])continue; //加速
for(rg k=0;k<n;++k){
if(!a[j][k])continue; //加速
if((i&-i)>1<<k)continue; //起点不能改!!!(去重)
if(1<<k&i){ //这个点走过
if(1<<k==(i&-i)) //起点与终点相同
ans+=f[i][j];
}else f[i|1<<k][k]+=f[i][j]; //没走过就走!
}
}
}printf("%lld",(ans-m)/2); //处理之后再输出!
return 0;
}
CF11D A Simple Task(状压DP)的更多相关文章
- CF11D A Simple Task 状压DP
传送门 \(N \leq 19\)-- 不难想到一个状压:设\(f_{i,j,k}\)表示开头为\(i\).结尾为\(j\).经过的点数二进制下为\(k\)的简单路总数,贡献答案就看\(i,j\)之间 ...
- FZU - 2218 Simple String Problem(状压dp)
Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...
- FZU - 2218 Simple String Problem 状压dp
FZU - 2218Simple String Problem 题目大意:给一个长度为n含有k个不同字母的串,从中挑选出两个连续的子串,要求两个子串中含有不同的字符,问这样的两个子串长度乘积最大是多少 ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
- fzu2188 状压dp
G - Simple String Problem Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- HDU5816 Hearthstone(状压DP)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5816 Description Hearthstone is an online collec ...
- BZOJ-1087 互不侵犯King 状压DP+DFS预处理
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp
题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...
- HDUOJ Clear All of Them I 状压DP
Clear All of Them I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 122768/62768 K (Java/Oth ...
随机推荐
- 实验吧CTF天网管理系统
天网你敢来挑战嘛 格式:ctf{ } 解题链接: http://ctf5.shiyanbar.com/10/web1/ 打开链接后,嗯,光明正大的放出账号密码,肯定是登不进的,查看源代码 看来是和md ...
- python 爬虫学习之路
2016-6-18 --今天实现了第一个用urllib2实现的爬虫程序. --过程中发现 req = urllib2.Request(url,headers = headers) 总是报错: 主要原因 ...
- centos上部署mysql
本文记录整个安装的过程和跳进的坑,大家预祝我能从坑里爬出来吧 当前系统版本 开始我们的安装: 在安装之前查阅了部分资料,大体安装流程 yum -y install mysql yum -y insta ...
- error: Build input file cannot be found: '*******/node_modules/react-native/Libraries/WebSocket/libfishhook.a' 问题解决记录
解决了刚才的'config.h' file not found问题,本以为就可以顺畅的跑起来,谁知道又被恶心到了,Build input file cannot be found!!! 问题: err ...
- Ajax实例OR技术原理 转自 (http://blog.csdn.net/evankaka )
摘要:AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术.AJAX 是一种用于创建快速动态网页的 ...
- SMS
SMS:(Short Messaging Service)手机短信服务 . 一种存储和转发服务,短消息并不是直接从发送人发送到接收人,而始终通过 SMS 中心进行转发的.如果接收人处于未连接状态(可能 ...
- linux 开机直接进入命令行
一.图形界面和命令行切换 linux系统在启动图形化界面后,可以在图形界面和命令行之间来回切换,linux提供了6个命令行终端(terminal或Console),分别为tty1——tty6,使用Ct ...
- OneZero第五周第一次站立会议(2016.4.18)
1. 时间: 13:00--13:15 共计15分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...
- sessionStorage & localStorage in-depth
sessionStorage & localStorage in-depth Web Storage API https://developer.mozilla.org/en-US/docs/ ...
- ql Server 2012完全卸载方法
第一步,在控制面板里面找到程序——卸载程序这一项,打开之后就会是这样的了 第二步,经过第一步打开卸载程序后,在里面找到Microsoft SQLserver 2012 (64-bit)这一项,可以通过 ...