http://codeforces.com/problemset/problem/11/D

题意 给定一个简单图,输出其中的简单环的数目。简单环的含义是,不包含重复顶点、重复边的环。

1 <= n <= 19

这题看数据范围很显然用状压dp来做,但是和寻常的状压dp由很大的差别,一开始想用记忆化搜索的方法,dp[i]表示这个状态下的最大简单环数量,但是在状态转移的过程中发现并不容易去转移。

事实上可以换一个思路,用dp来记录图中一个起点到一个重点的简单路径的数量,当终点返回到起点的时候, ans += dp

但是三维的dp空间复杂度不允许,我们可以降低起点的维度,将起点变为状态中最小的点即可。

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
const double eps = 1e-;
const int maxn = ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,tmp,K;
LL dp[ << ][maxn];
bool vis[ << ];
struct Edge{
int v,next;
}edge[maxn * maxn];
int head[maxn],tot;
bool MAP[maxn][maxn];
void add(int u,int v){
MAP[u][v] = ;
edge[++tot].v = v;
edge[tot].next = head[u];
head[u] = tot;
}
int findfirst(int x){
int cnt = ;
while(x){
if(x & ) break;
cnt++;
x >>= ;
}
return cnt;
}
int main()
{
scanf("%d%d",&N,&M);
For(i,,M){
int u,v; Sca2(u,v);u--,v--;
add(u,v); add(v,u);
}
For(i,,N - ){
dp[ << i][i] = ;
}
LL ans = ;
for(int t = ; t < ( << N) ; t ++){
int st = findfirst(t);
for(int x = ;x < N ; x ++){
if(t & ( << x) && dp[t][x]){
for(int i = st; i < N ; i ++){
if(MAP[x][i]){
if(t & ( << i)){
if(t == (( << i) | ( << x))) continue;
if(i == st) ans += dp[t][x];
}else dp[t | ( << i)][i] += dp[t][x];
}
}
}
}
}
Prl(ans / );
#ifdef VSCode
system("pause");
#endif
return ;
}

CodeForces11D 状压dp的更多相关文章

  1. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  2. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  3. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  4. [NOIP2016]愤怒的小鸟 D2 T3 状压DP

    [NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...

  5. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  6. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  7. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

  8. 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP

    [BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...

  9. 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP

    [BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...

随机推荐

  1. struts2 的struts.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...

  2. 使用RestTemplate测试视频上传的Post请求

    以往多用RestTemplate处理接口的调用以及与Ribbon/Feign配合使用调用微服务接口,近日写了一个处理Post文件上传的解决方案,其实就是将后台所需的MultipartFile,在请求P ...

  3. BZOJ2809 dispatching 【可并堆】

    题目分析: yy一下就知道了,合并用可并堆少个log. 代码: #include<bits/stdc++.h> using namespace std; ; int n,m; int b[ ...

  4. ☆ [POJ2559] Largest Rectangle in a Histogram 「单调栈」

    类型:单调栈 传送门:>Here< 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题思路 单调栈的经典题 显然,最终的子矩形高度一定和某一个矩形相等(反证).因此一 ...

  5. 【XSY1905】【XSY2761】新访问计划 二分 树型DP

    题目描述 给你一棵树,你要从\(1\)号点出发,经过这棵树的每条边至少一次,最后回到\(1\)号点,经过一条边要花费\(w_i\)的时间. 你还可以乘车,从一个点取另一个点,需要花费\(c\)的时间. ...

  6. nswl 收集日志

    nswl 收集日志 参考链接:https://docs.citrix.com/en-us/citrix-adc/12-1/system/web-server-logging.html PS C:\Us ...

  7. 爬虫_微信小程序社区教程(crawlspider)

    照着敲了一遍,,, 需要使用"LinkExtrator"和"Rule",这两个东西决定爬虫的走向. 1.allow设置规则的方法:要能够限制在我们想要的url上 ...

  8. nmap 常用命令

    10.1.1.0/24 = 10.1.1.1-10.1.1.255       #c段扫描10.1.1.0/16 = 10.1.1.1-10.1.255.255   #b段扫描10.1.1.0/8 = ...

  9. Write less code

    If you find yourself writing a lot of code to do something simple, you're probably doing it wrong. A ...

  10. sscanf、sprintf、stringstream常见用法

    转载自:https://blog.csdn.net/jllongbell/article/details/79092891 前言: 以前没有接触过stringstream这个类的时候,常用的字符串和数 ...