• codeforces 11D(状压dp)


    传送门: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("*********
    ")
    #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)<<"]
    "
    #define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]
    "
    #define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]
    "
    
    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
    ", 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("*********
    ")
    #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)<<"]
    "
    #define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]
    "
    #define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]
    "
    
    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
    ", ans / 2);
        return 0;
    }
    
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    80年代的兄弟,你会什么?
    设计模式单件模式
    大冒险 这注定是部史诗级的探索。。。
    关于重构JS前端框架的失败经验(顺便怀念那些死去的代码)
    JDBC连接数据库类(主要用于存储过程)
    ActiveRecord学习(六):总结
    ASP.NET中常用的文件上传下载方法
    [整理]ASP.NET2.0新特性概述
    关于NHibernate中one to many 的问题
    关注06德国世界杯:比赛日程表
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/11043477.html
Copyright © 2020-2023  润新知