状态压缩/Bitmask

在动态规划问题中,我们会遇到需要记录一个节点是否被占用/是否到达过的情况。而对于一个节点数有多个甚至十几个的问题,开一个巨型的[0/1]数组显然不现实。于是就引入了状态压缩,用一个整数的不同二进制位来表示该节点的状态。

Description

  • Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no repeated vertices or edges.

Input&Output

Input

  • The first line of input contains two integers n and m (1 ≤ n ≤ 19, 0 ≤ m) – respectively the number of vertices and edges of the graph. Each of the subsequent m lines contains two integers a and b, (1 ≤ a, b ≤ n, a ≠ b) indicating that vertices a and b are connected by an undirected edge. There is no more than one edge connecting any pair of vertices.

Output

  • Output the number of cycles in the given graph.

Sample

Input

4 6
1 2
1 3
1 4
2 3
2 4
3 4

Output

7

Solution

  • 大意是求简单无向图的环数,暴搜遍历必然会TLE,重复环的处理也十分复杂。
  • 考虑状态压缩,用二进制位来表示当前状态是否经过了特定的点。为了减轻重复环的处理难度,我们约定只计算起点序小于当前节点的状态(在代码中会有解释)。若节点i与当前节点y之间有边,状态的转移有以下几种条件:
  1. 若当前状态的起点序大于当前节点 (k&-k>(1<<y)) ,不转移。
  2. 若当前状态经过了当前节点 (k&(1<<y)) ,判断起点是否就是当前节点,若是,意味着我们找到了环,更新答案。
  3. 若当前状态没有经过当前节点,则更新经过当前节点的状态 f[k|(1<<y)][y] ,由 f[k][i] 贡献。
  • 遍历以每个节点为起点的所有状态,我们可以得到一个ans。但需要注意的是,这种计算方式会将两点间连一条边的路径(为什么?)和一个环的双向都计算在内,输出时需要将答案减去边数再除以2.
    细节与边界处理
  • 由于二进制位需要从第0位开始,我们不妨在建图时同一将点的编号减1,方便计算。节点的遍历也要从0到n-1。
  • 初始状态下,以节点i为起点,只经过i的状态,f值为1。
  • 代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define maxn 20
    #define maxe 400
    using namespace std;
    typedef long long ll;
    struct edge{
    int to,nxt;
    }e[maxe];
    int n,m,x,y,edgenum,lnk[maxn];
    ll ans,f[1<<maxn][maxn];
    void add(int bgn,int end)//事实上,节点比较少,邻接矩阵也可以存下
    {
    edgenum++;
    e[edgenum].to=end;
    e[edgenum].nxt=lnk[bgn];
    lnk[bgn]=edgenum;
    }
    int main()
    {
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i)
    {
        scanf("%d%d",&x,&y);
        add(x-1,y-1);
        add(y-1,x-1);
    }
    for(int i=0;i<n;++i)f[1<<i][i]=1;
    for(int k=0;k<(1<<n);++k){
        for(int i=0;i<n;++i){
            if(!f[k][i])continue;
            for(int p=lnk[i];p;p=e[p].nxt){
                int y=e[p].to;
                if((k&-k)>(1<<y))continue;//判断起点序
                if(k&(1<<y)){
                    if((k&-k)==(1<<y))//判断环
                        ans+=f[k][i];
                }
                else f[k|(1<<y)][y]+=f[k][i];
            }
        }
    }
    ans=(ans-m)/2;
    printf("%I64d",ans);
    return 0;
    }

[CodeForces 11D] A Simple Task - 状态压缩入门的更多相关文章

  1. CodeForces - 11D A Simple Task

    Discription Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycl ...

  2. Codeforces 11D A Simple Task 统计简单无向图中环的个数(非原创)

    太难了,学不会.看了两天都会背了,但是感觉题目稍微变下就不会了.dp还是摸不到路子. 附ac代码: 1 #include<iostream> 2 #include<cstdio> ...

  3. Codeforces C. A Simple Task(状态压缩dp)

    题目描述:  A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. 计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task

    E. A Simple Task Problem's Link: http://codeforces.com/problemset/problem/558/E Mean: 给定一个字符串,有q次操作, ...

  5. Codeforces 558E A Simple Task (计数排序&&线段树优化)

    题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...

  6. Codeforces 558E A Simple Task(权值线段树)

    题目链接  A Simple Task 题意  给出一个小写字母序列和若干操作.每个操作为对给定区间进行升序排序或降序排序. 考虑权值线段树. 建立26棵权值线段树.每次操作的时候先把26棵线段树上的 ...

  7. Codeforces 580D Kefa and Dishes(状态压缩DP)

    题目链接:http://codeforces.com/problemset/problem/580/D 题目大意:有n盘菜每个菜都有一个满意度,k个规则,每个规则由x y c组成,表示如果再y之前吃x ...

  8. CodeForces 588E A Simple Task(线段树)

    This task is very simple. Given a string S of length n and q queries each query is on the format i j ...

  9. Codeforces J. A Simple Task(多棵线段树)

    题目描述: Description This task is very simple. Given a string S of length n and q queries each query is ...

随机推荐

  1. Maven打包的三种方式(转)

    Maven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.jar执行运行jar文件,会出现"no main manifest attribute, in ...

  2. python趣味——与MS系列编译器一样强大的Unicode变量名支持

    中文变量名,中文函数名,中文类名等,可惜Python2不支持,但在Python3时代,这些都可以完美支持了. def 中文函数(): return 1

  3. nodejs批量导入数据eventproxy(回调函数嵌套解决方案)使用实例

    回调函数嵌套解决方案——eventProxy API地址:https://github.com/JacksonTian/eventproxy 1.安装eventproxy 执行npm install ...

  4. linux scp 命令

    scp 命令 scp 命令 意思是 secure copy 即安全拷贝,可以把它看做是 cp 命令的高级版,可以跨主机拷贝. 经常用来在局域网内不同主机之间分享文件,或者在本机与远程主机中分享文件. ...

  5. WCF跨域解决方法及一些零碎的东西。

    之前发过一篇随笔,说的WCF配置文件配置问题.里面也配了跨域支持,但是jsoncollback只支持Get请求,Post请求是解决不了,所以这里把真正的WCF跨域问题贴出来. 话不多说,直接帖配置文件 ...

  6. windows+CMake+mingw 搭建c c++开发环境

    layout: post title: "windows+CMake+mingw 搭建c c++开发环境" date: 2018-03-30 22:23:06 tags: wind ...

  7. 『开源重编译』System.Data.SQLite.dll 自适应 x86 x64 AnyCPU 重编译

    背景: > System.Data.SQLite.dll 程序集 不能良好的支持 AngCPU 格式 System.Data.SQLite.dll 在 适应 x86 和 x64 有三个方案: & ...

  8. C第十八次课

    总结知识点: 指针 1.指针变量 指针变量的定义:例8.1 指针变量的引用:例8.2: 指针变量作为函数参数:例8.3 swap函数,例8.4 比较排序函数 2.指针数组 数组元素的指针:int *p ...

  9. 铜齿铁牙UP计划

    铜齿铁牙UP计划 我在""做教练"之好声音训练"给出了老师.播音主持学习者,声乐学习者科学用声三要点: 用气发声 共鸣发声 虚实结合 用气发声首先要学会腹式呼吸 ...

  10. 简单的C语言编译器--语义制导翻译

      语法分析是最难写的,而这部分确实最伤脑的.大量的语义动作分析差点把我逼疯.   简而言之,这部分的作用就是在每次归约之后,都进行一些语义动作,最终让我们得到测试程序的三地址码,即中间代码. 1. ...