【hiho1087】Hamiltonian Cycle
题目大意:给定一个 N 个点的有向图,计数图上哈密顿回路的条数。
题解:哈密顿回路需要经过除了初始位置,每个点恰好一次。如果已知一条哈密顿回路的方向,那么从这条路上任意一个点出发,得到的都是同样的结果。因此,不妨设从 0 号节点出发,最后回到 0 号节点。统计答案只需要枚举最后一个点在哪个位置即可。
代码如下
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,m,G[12][12];
LL f[1<<12][12],ans;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
G[--x][--y]=1;
}
f[1][0]=1;
for(int s=0;s<1<<n;s++){
for(int i=0;i<n;i++){
if((s>>i&1)==0)continue;
for(int j=0;j<n;j++){
if((s>>j&1)||!G[i][j])continue;
f[s|(1<<j)][j]+=f[s][i];
}
}
}
for(int i=0;i<n;i++){
if(G[i][0]==0)continue;
ans+=f[(1<<n)-1][i];
}
printf("%lld\n",ans);
return 0;
}
【hiho1087】Hamiltonian Cycle的更多相关文章
- 【题解】Shortest Cycle
原题链接:CF1205B 题目大意 给定\(n\)个整数\(a_1,a_2,a_3, \dots ,a_n\),若\(i \neq j\)且\(a_i \land a_j \neq 0\),则 ...
- PAT甲级——A1122 Hamiltonian Cycle【25】
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- 【LeetCode】142. Linked List Cycle II
Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- 【题解】cycle
[题解]cycle 题目描述 给定一个无向图,求一个环,使得环内边权\(\div\)环内点数最大. 数据范围 \(n \le 5000\) \(m\le 10000\) \(Solution\) 考虑 ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) C】 Permutation Cycle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] p[i] = p[p[i]]一直进行下去 在1..n的排列下肯定会回到原位置的. 即最后会形成若干个环. g[i]显然等于那个环的大 ...
- 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 关于JavaScript实例化的理解
要理解这个,我们首先要理解一个概念“类”,所谓类,指的是对象的模版.对象就是类的实例.由前面我们知道,对象是单个实物的抽象,所以通常需要一个模版,表示某一类实物的共同特征,然后对象根据这个模版生成,这 ...
- 修改linux内核启动顺序
修改linux内核启动顺序 # 修改内核启动顺序x86_64 centos:cat /boot/grub2/grub.cfg |grep "menuentry" grub2-set ...
- 5分钟快速安装Redmine项目管理软件
公司还在使用Excel.project.word来管理项目吗?时间一长.项目参与的人多.就出现了断断续续无法连续跟踪的问题.终于忍受不了公司这种陈旧的项目管理手段了,于是花了一些时间研究了市面上常见的 ...
- Object中有哪些方法及其作用
你知道Object中有哪些方法及其作用吗? 一.引言 二.Object方法详解 1.1.registerNatives() 1.2.getClass() 1.2.1.反射三种方式: 1.3.hashC ...
- window启动目录
启动文件目录 C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
- python函数 -- 作用域,异常处理
1.def语句和参数 python定义函数的关键词为def,格式如下: def 函数名([变元],[变元],....) #保存在变元中的值,在函数返回后该变元就会被销毁了. 2.返回 ...
- Storm消费Kafka提交集群运行
1.创建拓扑,配置KafkaSpout.Bolt KafkaTopologyBasic.java: package org.mort.storm.kafka; import org.apache.ka ...
- python中的并发执行
一. Gevent实例 import gevent import requests from gevent import monkey # socket发送请求以后就会进入等待状态,gevent更改了 ...
- expect批量分发密钥对
vim shell.exp #!/usr/bin/expect set timeout 10 set hostname [lindex $argv 0] set username [lindex $a ...
- Codeforces 1190D. Tokitsukaze and Strange Rectangle
传送门 注意到矩形往上是无限的,考虑把点按 $y$ 从大到小考虑 对于枚举到高度为 $h$ 的点,设当前高度大于等于 $h$ 的点的所有点的不同的 $x$ 坐标数量为 $cnt$ 那么对于这一层高度 ...