传送门


\(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. Sql Server 向上取整、向下取整、四舍五入取整

    ==================================================== [四舍五入取整截取] select round(55.56,0) ============== ...

  2. 去除git版本控制

    命令:find . -name ".git" | xargs rm –Rf linux $ find . -type d -iname '__pycache__' -exec rm ...

  3. git下载、安装、连接github

    0.下载git 官网下载速度慢,下载不下来阿里云下载地址:https://npm.taobao.org/mirrors/git-for-windows/ 1.安装git linux:在命令行直接输入: ...

  4. SQL注入介绍

    一.SQL注入概念   1.sql注入是一种将sql代码添加到输入参数中   2.传递到sql服务器解析并执行的一种攻击手法   举例:某个网站的用户名为name=‘admin’.执行时为select ...

  5. CSS| 框模型-margin

    CSS margin 属性 设置外边距的最简单的方法就是使用 margin 属性. margin 属性接受任何长度单位,可以是像素.英寸.毫米或 em. margin 可以设置为 auto.更常见的做 ...

  6. npm与yarn常用命令对比

    最近在用yarn,但是命令老是记不住,在此记录,方便日后翻看 图片截取自:http://yuanhehe.cn/2017/06/11/npm-%E4%B8%8E-Yarn-%E5%B8%B8%E7%9 ...

  7. 将爬取的数据保存到mysql中

    为了把数据保存到mysql费了很多周折,早上再来折腾,终于折腾好了 安装数据库 1.pip install pymysql(根据版本来装) 2.创建数据 打开终端 键入mysql -u root -p ...

  8. VsCode 的使用

    一.简介 VsCode(Visual Studio Code),官网地址:https://code.visualstudio.com/ Visual Studio Code is a lightwei ...

  9. UGUI 文字效果实现(Shadow\Gradient\Outline)

    NGUI直接在UILabel组件中接入了Shadow.Gradient和outline选项,但在UGUI中是通过另外的组件单独提供,比如outline.shader等.这篇文章主要记录这几个文字效果实 ...

  10. 机器C盘临时区

    系统的临时区里:C:\Documents and Settings\Administrator(用户名)\Local Settings\Temporary Internet Files 这是临时文件夹 ...