Codeforces 11D A Simple Task (状态压缩DP)

Codeforces 11D A Simple Task 统计简单无向图中环的个数 状态压缩动态规划计算简单无向图中简单环的个数。 阅读详情

给你N个点和M条边,求当中包含的简单路径的个数。

网上看的题解,其实就是求哈密顿回路数,但是是无向图而且至少由三个点组成。

http://codeforces.com/blog/entry/337

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const ll maxm=1<<19;
ll dp[maxm][20],sum;
int lowbit[maxm];
bool map[20][20];
int n,m,u,v,start;
inline bool bt(int i,int mask)
{
    return mask&(1<<i);
}
inline int cnt(int mask)
{
    int ret=0;
    while(mask) ret+=(mask&1),mask>>=1;
    return ret;
}
void getfirst()
{
    int i;
    for(i=1; i<maxm; i++)
    {
        int mask=i,ret=0;
        while(mask&1^1) ret++,mask>>=1;
        lowbit[i]=ret;
    }
}
ll dfs(int mask,int end)
{
    int j,tmp=cnt(mask);
    ll ans=0;
    if(dp[mask][end]>=0) return dp[mask][end];
    for(j=start; j<n; j++)
        if(j!=end&&bt(j,mask)&&map[j][end]&&(tmp==2||j!=start))
            ans+=dfs(mask^(1<<end),j);
    if(tmp>2&&map[end][start])//当前搜索的end点和起始点相连并且含有3个点以上
        sum+=ans;
    return dp[mask][end]=ans;
}
int main()
{
    int i;
    getfirst();
    memset(dp,-1,sizeof(dp));
    memset(map,0,sizeof(map));
    cin>>n>>m;
    sum=0;
    while(m--)
    {
        cin>>u>>v;
        u--,v--;
        map[u][v]=map[v][u]=1;
    }
    for(i=0; i<n; i++) dp[1<<i][i]=1;
    int maxmask=1<<n;
    for(int mask=0; mask<maxmask; mask++)
        if(cnt(mask)>1)
        {
            start=lowbit[mask];
            for(i=start; i<n; i++)
                if(bt(i,mask)&&map[start][i])
                    dfs(mask,i);
        }
    cout<<sum/2<<endl;
    return 0;
}


状态DP求哈密顿回路个数 CodeForces 11D——A Simple Task 统计图中哈密顿回路的数目 阅读详情

相关推荐

deepfacelab下载安装及训练(小白篇),以及导出SAEHD为dfm文件,解决broken pipe问题

其中如果用的是RTX30系列显卡的话,需要把WIN10升级到20H4或更新版本,然后打开硬件加速,不然会出现训练时一直卡在第一个迭代不动了,我的服务器版本为22H2,符合。支持WIN10,win11,WIN7,LINUX,不支持mac(因为mac不支持NVIDIA显卡)

caoyongsheng的博客 3888

CodeForces - 11D 【状压DP+无向图找环】

CF11D 链接     题意:求出一个n个点m个边的图,求简单环有多少(没有重复点和边)。(n   分析:用S能记录状态(有多少个点在路径中),那么如何找环能确保不重不漏啦,对于一个环,找出他的特异性记录,一个环,如果以最小点为环的“起始点”,则每个环都被记录啦两次。用dp[s][i]表示路径s的当前点为i的次数,枚举下一点,如果形成环就加上答案,否则记录下一边。由于题目求的是超过三个

tystrwor的博客 961

来!简单了解一下计算机发展史!

​ 1936年,英国数学家阿兰・麦席森・图灵提出了一种抽象的计算模型——图灵机( Turing machine)。图灵机,又称图灵计算机,即将人们使用纸笔进行数学运算的过程进行抽象,由一个虚拟的机器替代人类进行数学运算。​ 图灵机是一个抽象的机器,它有一条无限长的纸带,纸带分成了一个一个的小方格,每个方格有不同的颜色。有一个机器头在纸带上移来移去。机器头有一组内部状态,还有一些固定的程序。

m0_57290404的博客 2905

A Simple Task CodeForces - 11D

给定一个简单无向图,求里面简单环的个数。 注:简单环是顶点和边不重复的环。 输入 第一行两个整数n, m (1 \le n \le 19, 0 \le m \le 400)n,m(1≤n≤19,0≤m≤400)。 接下来m行,每行给出两个整数a, b (1 \le a, b \le n)a,b(1≤a,b≤n),表示a和b之间有一条无向边。 输入保证没有重边。 输出 输出答案占一行。 样例 输入 复制 4 6 1 2 1 3 1 4 2 3 2 4 3 4 输出 复制 7

wan1314mum的专栏 366

Jetson Nano系统重装的艺术:从格式化到烧录的深度解析

本文详细解析了Jetson Nano系统重装的完整流程,从SD卡格式化到系统镜像烧录的进阶技巧,包括硬件选择、软件准备、常见问题处理及深度学习环境配置。特别推荐使用三星EVO Plus SD卡和官方电源以确保稳定性,适合开发者快速掌握嵌入式设备系统重装的核心技术。

weixin_28273593的博客 610

AutoSAR RTE调度机制揭秘:从定时器事件到Runnable执行的完整流程

本文深入解析了AutoSAR RTE(运行时环境)的核心调度机制,详细阐述了从定时器事件触发到Runnable(可运行实体)执行的完整流程。通过剖析Rte_Start()函数、OS任务与警报的交互,以及事件驱动循环,揭示了RTE如何作为幕后指挥家,高效、可靠地协调软件组件间的周期性任务执行,是打通AutoSAR应用开发的关键。

weixin_29169899的博客 226

A Simple Task(CodeForces-11D)(状压DP,剖析讲解)

前言 题目 思路 代码 前言 这道题一开始思路错了,用了什么最小生成树搞了后数圈…..结果是状压DP…(没有观察啊!) 题目 传送门 给定一个简单图,输出其中的简单环的数目。简单环的含义是,不包含重复顶点、重复边的环。 输入 输入的第一行包含了两个整数 n 和 m (1 ≤ n ≤ 19, 0 ≤ m) – 分别表示图的顶点数目、边数目。以下 m 行的每行包含了两...

Mouse_liang的博客 1313

树莓派安装snowboy

1、树莓派换源 参考链接:https://blog.youkuaiyun.com/qq_43556844/article/details/113615915 2、更新pip3 参考链接:https://blog.youkuaiyun.com/qq_43556844/article/details/113616214 3、存取麦克风 我们使用PortAudio作为音频输入/输出的跨平台支持。我们还使用sox作为快速实用程序来检查麦克风设置是否正确。 sudo apt-get install python-pyaudio python

qq_43556844的博客 1923

CodeForces - 11D

有意思 和之前的项链异曲同工,但之前就是入门,半抄半做的。 one more time 再跑一遍吧 环?怎么处理环? 都是通过记录路径,若是首末可相连,那么就是个环,同时,这样也可以用作之后的状态转移,妙啊 dp[i][s]: 路径的最后一个点是i且状态是s,且有隐含条件开头是是状态的第一个1。 #include&lt;iostream&gt; #include&lt;stdio.h...

wyxxzsy的博客 648

ZooKeeper 实战:使用 Docker Compose 部署 ZooKeeper 集群

ZooKeeper是一个开源的分布式协调服务,用于管理大型分布式系统中的数据。它由Apache软件基金会提供,最初是Hadoop的一个子项目,但后来发展成为一个独立的顶级项目。ZooKeeper提供的一致性协调服务对于构建分布式应用和服务非常重要,特别是在需要精确的领导选举、配置管理、命名服务、分布式同步和提供分布式锁等功能时。

SmartSi 1541

Transformer 跨界物理建模:探索模拟动态物理系统方法与实践(PINN、流体力学、有限元)

是一个由 Nicholas Zabaras 团队开发的开源项目,托管于 GitHub,旨在将 Transformer 模型(最初在自然语言处理领域大获成功)应用于物理系统的建模与预测。该项目基于论文 ​*“Transformers for Modeling Physical Systems”*​(Neural Networks, 2022),探索了深度学习中的 Transformer 架构如何通过自注意力机制和 Koopman 嵌入来模拟动态物理系统。

源代码杀手的博客 1191

Codeforces11D(状压dp)

好题! 题意: 给你一个图,没有重边,求这个图简单环的个数; PS :简单环:在这个环中各个边只出现一次,各个点也只出现一次。 好难想的状态。dp[st][e]  状态st 中序数最小的点记为s并且以e为终点的环的个数。 每次枚举状态时找到最小的点,然后枚举终点,在枚举中间节点,如果中间节点和起点相同那么就可以累加到总和里面。注意计算时已经重复计算了,比如说 1- 2 - 3 - 1和

ACM之梦 682

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

CodeForces 11D - A Simple Task 状态压缩DP

dis[ k ] [ i ] 代表是以状态 k 中序最小h的为起点,i 点为终点的简单路径有多少条...在更新的过程中若路径能够回到h..则ans+=dp[k][i]     值得注意的是为了统计方便...保证一条简单路径起点序最小即可...也就是说在拓展一条简单路径时保证拓展的点的序不会大于以有最小的....     比如:          从1开始拓展..得到1-2-3-1 找

zzyzzy12 1880

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Codeforces Beta Round #11, problem: (D) A Simple Task 状态压缩DP+记忆化搜素DP

题意:在图中找简单回路 /************* 看了大神的博客才有所感悟啊,记忆化搜索+状态压缩。。。。太神了... 这种复杂度,近百万的DFS复杂度居然没有TLE,果然经验不足,菜鸟一只。 用状态压缩枚举起点和可能经过的点。 可以判定的简单通路 i->j,存在的条数为sum(i->k) 其中k,j之间有边。 当然,每次计算通路个数的时候,可以借每一个k来判断回路的条数,当然只能算一次。

uiiの技术随笔 767

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Codeforces Beta Round #11 D. A Simple Task

题意:有N(最多19个)个点,M条边,问有多少个简单环。 状态压缩,dp[st][i],终点是i,st表示这个环走过哪几个,定义起点为这些走过的点里最小的。 #pragma comment(linker, "/STACK:102400000,102400000") #include #include #include #include #include #include #incl

3058

CodeForces - 11D A Simple Task【状压DP】

题目链接:https://codeforces.com/contest/11/problem/D #include <iostream> using namespace std; static const int MAXN=20; long long dp[1<<MAXN][MAXN]; //dp[i][j] i表示经过的点的状态 j表示结尾的点 int d[MAXN][MAXN]; int n,m; int main() { scanf("%d%d",&n,&amp

inv0ker的博客 200

Codeforces - A Simple Task

题目链接:Codeforces - A Simple Task 这个数据范围不难想到状压dp。 我们令dp[x][s]为当前在点x,走过的状态为s的方案数。 因为一个环会重复计数,所以我们规定计数时,起点是环的最小点。最后除2即可。 因为是双向边,我们要防止一条边走过去又走回来,所以我们添加一个计数变量。 AC代码: #include<bits/stdc++.h> using na...

青烟绕指柔的博客 446

Codeforces 11D - A Simple Task (状压DP)

题意求出一个n个点m个边的图,求简单环有多少(没有重复点和边)。思路这是个不错的题,这个状压dp保存的状态不是直接的环,而是路径的个数。s表示的状态为一条路径,则dp[s][i]表示以s的最小编号为起点,以i为终点的环的个数。那么我们就可以通过枚举状态,枚举状态中的起点和枚举路径外的终点,然后判断终点和起点是否相连来判断是否成环。代码 #include <stdio.h> #include <str

Winterfell30' Blog 1531

CodeForces 11D A Simple Task (DP解哈密顿路径数目)

D. A Simple Task time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Given a simple graph, output th...

adr5970的博客 306

Codeforces11 D. A Simple Task(统计环个数,状压DP)

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 The first line of input contains two integers n and m (1 ≤ n ≤ 19, 0 ≤ m) – respectively the number of vertices and edges of

tomjobs的博客 630

A Simple Task CodeForces - 11D【状压dp】

参考博客: CodeForces - 11D A Simple Task 好久没写状压dp了,对于集合状态的表示都有些生疏: 空集.............................0 只含有第i个元素的集合{i}............1&lt;&lt;i 含有全部n个元素的集合{0,1...n-1}....(1&lt;&lt;n)-1//含有n个元素的全集 判断第i个元素是否属...

岂曰无衣,与子同袍 297

Codeforces Beta Round #11 D. A Simple Task(状压DP)

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 The first line of input contains two integers n and m (1 ≤ n ≤ 19,

WZJRJ28 617

Codeforces Beta Round #11 D. A Simple Task(状压dp/无向图简单环的个数)

题目 给定n(n<=19)个点,m(0<=m)的无向图,求简单环的个数 简单环是指一个没有经过重复的点,也没有经过重复的边的环 思路来源 https://www.luogu.com.cn/problem/solution/CF11D https://blog.youkuaiyun.com/fangzhenpeng/article/details/49078233(非常详细) 题解 洛...

小衣同学的博客 524

D. A Simple Task

D. A Simple Task

zzzyyzz_的博客 217

CodeForces 11D :A Simple Task 状压DP

传送门 题目描述 求简单无向图的环数 分析 状压DP 我们用f[i][j]f[i][j]f[i][j]表示起点为lowbit(i)lowbit(i)lowbit(i),终点为jjj的简单路线数量,如果jjj的下一个点是lowbit(i)lowbit(i)lowbit(i),表示存在数量为f[i][j]f[i][j]f[i][j]的环 最后需要注意一个任意一个长度大于2的环都会被计算进去两次,任意一条边因为是无向边也会被计算进去一次,所以最后去要去掉 代码 #pragma GCC optimize(3) #i

tlyzxc的博客 184
上一篇: Codeforces 20C Dijkstra?
下一篇: POJ3211Washing Clothes
BehappyXiang
博客等级 码龄14年 11粉丝 113原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值