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 ...
随机推荐
- cocos2dx 中触摸事件分发一些解读
触摸事件分发中几个代码解读: 怎么说呢,感觉cocos2dx中的消息分发机制,相对于android中触摸事件分发机制要简单的多.因为android中要做区域判断,过滤器,以及父子组件分发给谁等等的逻辑 ...
- angular $compiler
directive是如何被compiled HTML编译发生在三个阶段: 1.$compile遍历DOM节点匹配directives 如果compiler找到元素上的directive,directi ...
- myeclipse一些快捷键 错了或者没说到补充下
Ctrl + 1 快速修复Ctrl + D 删除当前行 Ctrl + Alt + ↓ 复制当前行到下一行(复制增加)Ctrl + Alt + ↑ 复制当前行到上一行(复制增加)Alt + ↓ 当前行 ...
- Java 网络 IO 模型
在进入主题之前先看个 Java 网络编程的一个简单例子:代码很简单,客户端和服务端进行通信,对于客户端的每次输入,服务端回复 get.注意,服务端可以同时允许多个客户端连接. 服务端端代码: // 创 ...
- Laravel5 控制器
Request 一.取值 1.取值 echo $request->input('name','这是默认值'); 2.取得所有值 $array=$request->all(); 3.判断值是 ...
- 2013 ACM/ICPC Asia Regional Hangzhou Online hdu4739 Zhuge Liang's Mines
Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 【NOIP】OpenJudge - 15:银行利息
#include<stdio.h>//银行利息 int main() { float a,b; int i,c,d; scanf("%f%f%d",&a,&am ...
- OOP编程特性综合项目
package SourceFile; //创建动物类(父类). public abstract class CAnimal { public boolean mammal; //是不是哺乳动物 ...
- python 发送邮件,未完
def send_mail(): try: print "send mail..." # handle = smtplib.SMTP('smtp.163.com', 25) # h ...
- Java面向对象 集合(中)
Java面向对象 集合(中) 知识概要: (1)泛型的体系概念 (2)泛型的特点 (3)自定义泛型类 泛型的体系概念 泛型:JDK1.5版 ...