CodeForces11D 状压dp
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的更多相关文章
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
- 【BZOJ2073】[POI2004]PRZ 状压DP
[BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
- 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP
[BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...
- 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP
[BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...
随机推荐
- HttpWebRequest using Basic authentication
System.Net.CredentialCache credentialCache = new System.Net.CredentialCache(); credentialCache.Add( ...
- 在js文件中通过jquery定位到某个dom时候设置事件时候 相当于直接在dom里面添加事件
在js文件中通过jquery定位到某个dom时候设置事件时候 相当于直接在dom里面添加事件 当触发事件时候 会把当前的dom传给该方法
- Maven添加Web.xml的方法
当创建maven工厂时没有web.xml文件1.点击你的项目名称,进入到Myeclipse的-- Project Facets上,2.点击Dynamic Web Module 和下面的Java,将两个 ...
- Hdoj 4540.威威猫系列故事——打地鼠 题解
Problem Description 威威猫最近不务正业,每天沉迷于游戏"打地鼠". 每当朋友们劝他别太着迷游戏,应该好好工作的时候,他总是说,我是威威猫,猫打老鼠就是我的工作! ...
- linux中shell脚本引用另一shell脚本
调用有三种方法: 1.fork:不同的shell,调用后返回父shell,子shell从父shell中继承变量,但子shell的变量不会带回父shell,直接用path/to/file.sh调用: 2 ...
- Entity Framework 问题集锦
作者:疯吻IT 出处:http://fengwenit.cnblogs.com 1. No Entity Framework provider found for the ADO.NET provid ...
- hexo从零开始
部署Hexo Hexo官方文档 新建一个文件夹,比如,Blog,然后进入该文件夹下: npm install hexo-cli -g hexo version 安装依赖包 npm install 配置 ...
- NOIp2018爆零记
Day-2~Day0 考前抱佛脚,赶紧刷刷各种模板 Day 1 在开考之前打好了拍子模板,然后试题密码就发下来了(这是我前面的神仙打了\(100\)多行\(emacs\)的配置\(QAQ\)). 先按 ...
- LinkedBlockingQueue中put源码分析
查看源码得知: LinkedBlockingQueue采用是锁分离的技术 //取出锁 private final ReentrantLock takeLock = new ReentrantLock( ...
- 【【洛谷P2678 跳石头】——%%%ShawnZhou大佬】
{dalao传送门} 这道题如果要使用暴力搜索直接求解会严重超时.实际上,我们可以发现,这个所谓的最短跳跃距离显然不能超过一个范围,而这个范围题目上已经给了出来.也就是说,答案是有一个确定的范围限制的 ...