传送门:https://codeforces.com/problemset/problem/11/D

题意:

求n个点m条边的图里面环的个数

题解:

点的范围只有19,很容易想到是状压。

dp[sta][a]表示状态为sta,终点为n时环的个数

转移:

dp[sta | (1 << i)][i] += dp[sta][e];

由于是无向边,所以答案会重复计算一遍,最后要记得除2

代码:

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef long long ll;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n" const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 3e2 + 5;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
int mp[20][20];
LL dp[1 << 20][20];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
mp[u][v] = mp[v][u] = 1;
}
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i++) {
dp[1 << i][i] = 1;
}
LL ans = 0;
for(int s = 1; s < (1 << n); s++) {//枚举状态
int st = 0;
for(int i = 0; i < n; i++) {//枚举起点
if(s & (1 << i)) {
st = i;
break;
}
}
for(int e = st; e < n; e++) {//枚举终点
if(s & (1 << e)) {
for(int i = st; i < n; i++) {//从起点到终点
if(!(s & (1 << i)) && mp[e][i]) {
dp[s | (1 << i)][i] += dp[s][e];
if (mp[i][st] && __builtin_popcount(s | (1 << i)) >= 3)//环中点的个数
ans += dp[s][e];
}
}
}
}
}
printf("%lld\n", ans / 2);
return 0;
}#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef long long ll;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n" const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 3e2 + 5;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
int mp[20][20];
LL dp[1 << 20][20];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
mp[u][v] = mp[v][u] = 1;
}
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i++) {
dp[1 << i][i] = 1;
}
LL ans = 0;
for(int s = 1; s < (1 << n); s++) {//枚举状态
int st = 0;
for(int i = 0; i < n; i++) {//枚举起点
if(s & (1 << i)) {
st = i;
break;
}
}
for(int e = st; e < n; e++) {//枚举终点
if(s & (1 << e)) {
for(int i = st; i < n; i++) {//从起点到终点
if(!(s & (1 << i)) && mp[e][i]) {
dp[s | (1 << i)][i] += dp[s][e];
if (mp[i][st] && __builtin_popcount(s | (1 << i)) >= 3)//环中点的个数
ans += dp[s][e];
}
}
}
}
}
printf("%lld\n", ans / 2);
return 0;
}

codeforces 11D(状压dp)的更多相关文章

  1. CodeForces 11D(状压DP 求图中环的个数)

    Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no re ...

  2. Codeforces 678E 状压DP

    题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...

  3. Codeforces 8C 状压DP

    题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...

  4. Codeforces 1215E 状压DP

    题意:给你一个序列,你可以交换序列中的相邻的两个元素,问最少需要交换多少次可以让这个序列变成若干个极大的颜色相同的子段. 思路:由于题目中的颜色种类很少,考虑状压DP.设dp[mask]为把mask为 ...

  5. codeforces 1185G1 状压dp

    codeforces 1185G1. Playlist for Polycarp (easy version)(动态规划) 传送门:https://codeforces.com/contest/118 ...

  6. Codeforces 1155F 状压DP

    题意:给你一张图,问最少保留多少条边,使得这张图是边双联通分量. 思路:如果一个点集中的点已经是边双联通分量,那么从这个点集中的点x出发,经过若干个不是点集中的点,回到点集中的点y(x可能等于y),那 ...

  7. Codeforces - 71E 状压DP

    参考官方题解 #include<bits/stdc++.h> #define rep(i,j,k) for(register int i=j;i<=k;i++) #define rr ...

  8. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

  9. Codeforces Gym 100015F Fighting for Triangles 状压DP

    Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...

  10. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

随机推荐

  1. QT语言翻译

    QT中多语言的实现方式: 1.代码中tr运用 2.使用工具生成ts文件 3.翻译ts文件 4.生成qm文件 5.程序加载 以下内容程序加载时放入即可. QString appPath = QCoreA ...

  2. linux查看用户组所有成员

    1.grep 'user1' /etc/group //找出用户组的gid user1:x:1004://得出gid=1004 2. awk -F":" '{print $1&qu ...

  3. 序列化方案选型对比 - JSON/ProtocolBuffer/FlatBuffer/DIMBIN

    4千字长文预警!! 背景 JSON/XML不好吗? 好,再没有一种序列化方案能像JSON和XML一样流行,自由.方便,拥有强大的表达力和跨平台能力.是通用数据传输格式的默认首选.不过随着数据量的增加和 ...

  4. 域名拆分 tld

    概念 URL Universal Resource Locator ,统一资源定位符. 用处:用来标识互联网资源的唯一地址. 本质:提供了互联网上任一资源地址的通用表示方法. protocol://h ...

  5. oracle函数 chartorowid(c1)

    [功能]转换varchar2类型为rowid值 [参数]c1,字符串,长度为18的字符串,字符串必须符合rowid格式 [返回]返回rowid值 [示例] SELECT chartorowid('AA ...

  6. LeetCode74 Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  7. ocilib linux编译安装

    1.首先下载ocilib到自己目录 github:https://github.com/vrogier/ocilib 2.在下载instantclient 11.2.2的文件: instantclie ...

  8. TCP/IP 、HTTP和SOCKET

    TCP/IP协议概念 TCP/IP(Transmission Control Protocol/Internet Protocol)的简写,中文译名为传输控制协议/因特网互联协议,又叫网络通讯协议,这 ...

  9. hdu 1434 幸福列车 (Leftist Tree)

    Problem - 1434 网上题解是普通的堆合并,都是用优先队列直接做的.可是正解的堆合并应该是用左偏堆或者斐波那契堆的吧,不然O(X * N ^ 2)的复杂度应该是过不了的.斐波那契堆的实现相对 ...

  10. ACM学习网站、

    转载:http://www.cnblogs.com/zhourongqing/archive/2012/05/24/2516180.html http://61.187.179.132/JudgeOn ...