传送门


\(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的更多相关文章

  1. CF11D A Simple Task(状压DP)

    \(solution:\) 思路大家应该都懂: 状压DP:\(f[i][j]\),其中 \(i\) 这一维是需要状压的,用来记录19个节点每一个是否已经走过(走过为 \(1\) ,没走为 \(0\) ...

  2. FZU - 2218 Simple String Problem(状压dp)

    Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...

  3. FZU - 2218 Simple String Problem 状压dp

    FZU - 2218Simple String Problem 题目大意:给一个长度为n含有k个不同字母的串,从中挑选出两个连续的子串,要求两个子串中含有不同的字符,问这样的两个子串长度乘积最大是多少 ...

  4. codeforces Diagrams & Tableaux1 (状压DP)

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

  5. fzu2188 状压dp

    G - Simple String Problem Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  6. HDU5816 Hearthstone(状压DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5816 Description Hearthstone is an online collec ...

  7. BZOJ-1087 互不侵犯King 状压DP+DFS预处理

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. python同步原语--线程锁

    多线程锁是python多种同步原语中的其中一种.首先解析一下什么是同步原语,python因为GIL(全局解析锁)的缘故,并没有真正的多线性.另外python的多线程存在一个问题,在多线程编程时,会出现 ...

  2. LeetCode题解之Diameter of Binary Tree

    1.题目描述 2.分析 深度优先. 3.代码 int ans; int diameterOfBinaryTree(TreeNode* root) { ans = ; depth(root); ; } ...

  3. 前端测试框架jest 简介

    转自: https://www.cnblogs.com/Wolfmanlq/p/8012847.html 作者:Ken Wang 出处:http://www.cnblogs.com/Wolfmanlq ...

  4. Scala之List,Set及Map基本操作

    package big.data.analyse.dataSet import scala.collection.immutable.{TreeMap, TreeSet} import scala.c ...

  5. redis数据库的简单介绍

    NoSQL:一类新出现的数据库(not only sql) 泛指非关系型的数据库 不支持SQL语法 存储结构跟传统关系型数据库中的那种关系表完全不同,nosql中存储的数据都是KV形式 NoSQL的世 ...

  6. Shell的基础介绍和案例

    一.shell脚本基础 1.第一个脚本 vim  first.sh 分别使用三种方法可以执行脚本:   ./first.sh (需要有可执行的权限)   sh    first.sh   .  fir ...

  7. Java学习笔记--JDK动态代理

    1.JDK动态代理     JDK1.3之后,Java提供了动态代理的技术,允许开发者在运行期创建接口的代理实例.JDK的动态代理主要涉及到java.lang.reflect包中的两个类:Proxy和 ...

  8. (转)web.xml中的contextConfigLocation在spring中的作用

    (转)web.xml中的contextConfigLocation在spring中的作用   一.Spring如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocat ...

  9. 为什么签名前要加"\x19Ethereum Signed Message:\n"

    在以太坊中,可以找到关于上述破损的解释例子.以太坊有两种消息,交易

  10. 如何在自己设计的页面中调用metamask-1

    启发: https://github.com/MetaMask/metamask-extension/issues/714 https://github.com/MetaMask/metamask-e ...