CF11D A Simple Task 状压DP
\(N \leq 19\)……
不难想到一个状压:设\(f_{i,j,k}\)表示开头为\(i\)、结尾为\(j\)、经过的点数二进制下为\(k\)的简单路总数,贡献答案就看\(i,j\)之间有没有边。
当然,会有一些问题:①路会算重;②\(2^NN^2\)的数组开不下(当然②才是重点),所以考虑优化算法
考虑类似最小环的优化
设\(f_{i,j}\)表示开头为\(log_2lowbit(j)\),结尾为\(i\),经过的点数二进制下为\(j\)的简单路总数,转移跟上面类似,值得注意的是对于\(f_{k,j | 2^k} \leftarrow f_{i,j}\)还需要保证\(k > log_2lowbit(j)\),否则状态中一条简单路的开头会变
当然这样子每一条路还是会被算\(2\)遍,每一条边也会产生\(1\)的贡献,最后减掉就可以了。
#include<bits/stdc++.h>
#define lowbit(x) ((x) & -(x))
#define low(x) (int)(log2(lowbit(x)) + 0.1)
//This code is written by Itst
using namespace std;
inline int read(){
int a = 0;
char c = getchar();
bool f = 0;
while(!isdigit(c)){
if(c == '-')
f = 1;
c = getchar();
}
while(isdigit(c)){
a = (a << 3) + (a << 1) + (c ^ '0');
c = getchar();
}
return f ? -a : a;
}
bool Edge[19][19];
int head[19] , ind[1 << 19];
int N , M;
long long dp[19][1 << 19] , ans;
int main(){
#ifndef ONLINE_JUDGE
//freopen("in" , "r" , stdin);
//freopen("out" , "w" , stdout);
#endif
N = read();
M = read();
for(int i = 1 ; i <= M ; ++i){
int a = read() - 1 , b = read() - 1;
Edge[a][b] = Edge[b][a] = dp[max(a , b)][(1 << a) + (1 << b)] = 1;
}
for(int i = 1 ; i < 1 << N ; ++i)
for(int j = 0 ; j < N ; ++j)
if(dp[j][i] && i & (1 << j)){
ans += Edge[low(i)][j] * dp[j][i];
for(int k = low(i) + 1 ; k < N ; ++k)
if(Edge[j][k] && !(i & (1 << k)))
dp[k][i | (1 << k)] += dp[j][i];
}
cout << (ans - M) / 2;
return 0;
}
CF11D A Simple Task 状压DP的更多相关文章
- CF11D A Simple Task(状压DP)
\(solution:\) 思路大家应该都懂: 状压DP:\(f[i][j]\),其中 \(i\) 这一维是需要状压的,用来记录19个节点每一个是否已经走过(走过为 \(1\) ,没走为 \(0\) ...
- 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 ...
随机推荐
- 排错-lr回放错误Vuser failed to initialize extensi...解决方法
lr回放错误:Vuser failed to initialize extension LrXml.dll解决方法 by:授客 QQ:1033553122 步骤1:找到LR安装位置,打开协议目录 ...
- Angular 2基础(一) 环境搭建
Angular2是一款开源JavaScript库,由Google维护,用来创建页面应用程序.正式发布于2016年9月,基于ES6开发. 一.准备工作 使用Angular2开发,需要预先做一些配置上的配 ...
- (网页)JS去掉字符串前后空格或去掉所有空格的用法(转)
转自脚本之家: 这篇文章主要介绍了JS去掉字符串前后空格或去掉所有空格的用法,需要的朋友可以参考下: 代码如下: function Trim(str) { return str.replace(/(^ ...
- css,响应鼠标事件,文字变色
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 洗礼灵魂,修炼python(33)--面向对象编程(3)—特殊类方法__init__,公有属性,私有属性
在上一篇博文里,传入参数时,是在实例化对象后且在调用方法时才传入参数,感觉是不是有点繁琐对吧?可以在实例化的时候就传入参数吗?可以的,这就是本篇博文的要讲到的构造器——__init__(两边双下划线) ...
- python第七十一天---堡垒机
堡垒机的表结构图:
- 【PAT】B1076 Wifi密码(15 分)
注意接收字符时缓冲区的换行要接受掉 #include<stdio.h> int main() { int n; scanf("%d", &n); n *= 4; ...
- Numbers
Encoding style, data structure, more content about the list, use the list as a stack, use the list a ...
- Appium1.9.1 之 Desired Capabilities 释疑
服务关键字 Desired Capabilities在启动session的时候是必须提供的. Desired Capabilities本质上是以key value字典的方式存放,客户端将这些键值对发给 ...
- Pycharm 安装 idea VIM
直接在线安装 1.File->Settings->Plugins->Install JetBrains Plugins 2.点击install安装ideavim 3.也许需要的切换v ...