CodeForces 11D(状压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 the graph. Each of the subsequent mlines contains two integers a and b, (1 ≤ a, b ≤ n, a ≠ b) indicating that vertices aand 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.
Example
4 6
1 2
1 3
1 4
2 3
2 4
3 4
7
Note
The example graph is a clique and contains four cycles of length 3 and three cycles of length 4.
1-2-3 2-3-4 1-3-4 1-2-4 1-2-3-4 1-2-4-3 1-4-2-3
题意:给出一个图的点数和边数输出这个图中有几个环.
题解:这道题可以很容易想到状压,因为数据也只有19(orz).用sta的二进制表示已经有几个点在这个状态中,那怎么表示形成环呢?只需要找到一个当前状态中的点,它神奇的与前面的某一个点有一条边连着,那么说明肯定能构成环,可以为总答案做贡献.如果不能,那么就为下一个状态提供个数.不过由于是无向图.所以两个点也会被认为成环(两个点构成环的个数为边数),并且每个更大的环都会被计算两次,所以最后要减掉.然后就搞定了.
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std; long long dp[<<][],ans=;
vector<int> e[]; int lowbit(int x)
{
return x&(-x);
} int main()
{
int n,m,f,t;
scanf("%d%d",&n,&m); for(int i=;i<m;i++)
{
scanf("%d%d",&f,&t);
e[f-].push_back(t-);
e[t-].push_back(f-);
} for(int i=;i<n;i++)
{
dp[<<i][i]=;
} for(int sta=;sta<(<<n);sta++)
{
for(int i=;i<n;i++)
{
if(dp[sta][i])
{
for(int k=;k<e[i].size();k++)
{
int j=e[i][k];
if(lowbit(sta)>(<<j))
{
continue;
}
if(sta&(<<j))
{
if(lowbit(sta)==(<<j))
{
ans+=dp[sta][i];
}
}
else
{
dp[sta|(<<j)][j]+=dp[sta][i];
}
}
}
} ans=(ans-m)/;
printf("%lld\n",ans);
return ;
}
每天刷题,身体棒棒!
CodeForces 11D(状压DP 求图中环的个数)的更多相关文章
- Codeforces 678E 状压DP
题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...
- Codeforces 8C 状压DP
题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...
- Codeforces 1215E 状压DP
题意:给你一个序列,你可以交换序列中的相邻的两个元素,问最少需要交换多少次可以让这个序列变成若干个极大的颜色相同的子段. 思路:由于题目中的颜色种类很少,考虑状压DP.设dp[mask]为把mask为 ...
- Codeforces 1155F 状压DP
题意:给你一张图,问最少保留多少条边,使得这张图是边双联通分量. 思路:如果一个点集中的点已经是边双联通分量,那么从这个点集中的点x出发,经过若干个不是点集中的点,回到点集中的点y(x可能等于y),那 ...
- codeforces 1185G1 状压dp
codeforces 1185G1. Playlist for Polycarp (easy version)(动态规划) 传送门:https://codeforces.com/contest/118 ...
- Codeforces - 71E 状压DP
参考官方题解 #include<bits/stdc++.h> #define rep(i,j,k) for(register int i=j;i<=k;i++) #define rr ...
- HDU3247 Resource Archiver (AC自动机+spfa+状压DP)
Great! Your new software is almost finished! The only thing left to do is archiving all your n resou ...
- BZOJ 4042 Luogu P4757 [CERC2014]Parades (树形DP、状压DP)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=4042 (Luogu) https://www.luogu.org/prob ...
- HDU - 4284 Travel(floyd+状压dp)
Travel PP loves travel. Her dream is to travel around country A which consists of N cities and M roa ...
随机推荐
- getOutputStream() has already been called for this response
错误日志里偶尔会有getOutputStream() has already been called for this response这个错误 最近发现了高概率复现条件,所以顺手解决了一下: 首先根 ...
- .NetCore之下载文件
本篇将和大家分享的丝.NetCore下载文件,常见的下载有两种:A标签直接指向下载文件地址和post或get请求后台输出文件流的方式,本篇也将围绕这两种来分享:如果对您有好的帮助,请多多支持. 允许站 ...
- TomCat系统架构
1.TomCat总体结构 TomCat有两大核心组件:Connector和Container.Connector组件是可以被替换的,一个Container可以对应多个Connector. 多个Conn ...
- 【重点突破】——Canvas技术绘制随机改变的验证码
一.引言 本文主要是我在学习Canvas技术绘图时的一个小练习,绘制随机改变的验证码图片,虽然真正的项目里不这么做,但这个练习是一个掌握Canvas技术很好的综合练习.(真正的项目中验证码图片使用服务 ...
- Problem 2144 Shooting Game fzu
Problem 2144 Shooting Game Accept: 99 Submit: 465Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- 关于如何获取iframe中的元素
今天研究了一下iframe中元素的获取,发现有些地方还是有点坑的. 首先:如果使用纯前端手段,是没有办法获取非同源的iframe中的元素的,后面会提到后端手段 一.同源环境 1.首先在父页面获取ifr ...
- C# Async/await 异步多线程编程
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- wpf 制作必输项的*标记
直接引用帮助文档上的话吧,以免下次忘记! AdornedElementPlaceholder 类 .NET Framework 3.5 其他版本 此主题尚未评级 - 评价此主题 更新:20 ...
- break和continue 的区别
区别 break和continue都可在循环语句里面使用,也都可以控制外层的循环.但是continue只能在循环语句里面使用,break也可以使用在switch语句里面. break具体作用在循环语句 ...
- 使用vsftpd+nginx搭建一个文件服务器
一:安装vsftpd 1.安装 [root@localhost jack]# yum -y install vsftpd 安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp ...