hiho Hamiltonian Cycle

来源:互联网 发布:怎样抢到淘宝秒杀产品 编辑:程序博客网 时间:2024/05/22 16:59

#1087 : Hamiltonian Cycle

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us how many different Hamiltonian Cycles are there in this graph?

A Hamiltonian Cycle is a cycle that starts from some vertex, visits each vertex (except for the start vertex) exactly once, and finally ends at the start vertex.

Two Hamiltonian Cycles C1, C2 are different if and only if there exists some vertex i that, the next vertex of vertex i in C1 is different from the next vertex of vertex i in C2.

输入

The first line contains two integers n and m. 2 <= n <= 12, 1 <= m <= 200.

Then follows m line. Each line contains two different integers a and b, indicating there is an directed edge from vertex a to vertex b.

输出

Output an integer in a single line -- the number of different Hamiltonian Cycles in this graph.

提示

额外的样例:

样例输入样例输出3 3
1 2               
2 1              
1 30




样例输入
4 71 22 33 44 11 34 22 1
样例输出

2

这道题让求有向图的哈密顿回路数,数据不大,直接搜。但是正常方法可能会超时,这事就要介绍一种在搜索中可以运用的黑科技:位运算。

由于点不多,所以把每个点所连得边以及已经加入回路中的点用位运算的形式进行表达与传递。减小常数。

rest&(-rest):可以得到最靠右的二次幂的数。比如rest=5(101)时,5&(-5)=1;rest=4(100)时,4&(-4)=4(100)。真是66666

#include<cstdio>#include<cstring>using namespace std;int edge[14],p[1<<12],n,m,ans;void dfs(int u,int sta){    if(!sta){        ans+=(edge[u]&1);return;    }    int rest=sta&edge[u],tp;    while(rest){        tp=rest&(-rest);        dfs(p[tp],sta^tp);        rest-=tp;    }}int main(){    int u,v;    for(int i=0;i<12;++i) p[1<<i]=i+1;    while(~scanf("%d%d",&n,&m)){        memset(edge,0,sizeof(edge));        for(int i=0;i<m;++i){            scanf("%d%d",&u,&v);            edge[u]|=(1<<(v-1));        }        ans=0;dfs(1,(1<<n)-2);        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 百度网盘密码重置不了怎么办 sap密码输入被锁怎么办 dnf二级密码错10怎么办 大网卡网速慢了怎么办 小米路由器无线速度慢怎么办 小米手机无线速度慢怎么办 电脑网卡驱动没了怎么办 电脑显示网卡驱动不正常怎么办 微信别人拒收消息怎么办 电脑无线网卡速度慢怎么办 网吧吃鸡更新慢怎么办 手机号注册不了微信怎么办 小米账号密码忘了怎么办 小米手机账号密码忘了怎么办 华为手机账号密码忘记了怎么办 老年机开不了机怎么办 天谕没有顺网登陆怎么办 苹果密保问题忘了怎么办 密保手机没用了怎么办 qq密保手机没用了怎么办 手机开机按钮坏了怎么办 改了账号游戏角色消失怎么办 华为开机键坏了怎么办 抖音账号已重置怎么办 抖音账号被重置怎么办 吃鸡账号密码忘了怎么办 微信只记得账号忘了手机号怎么办 红米3开机键失灵怎么办 晚自习教室有许多虫子怎么办 泰迪吃草又呕吐怎么办 手机不断收到验证码信息怎么办 樱桃吃多了上火怎么办 过年不想回婆婆家过怎么办 旅行箱提手坏了怎么办 影棚人物后面有影子怎么办 微信运动图标不见了怎么办 逆光拍摄人黑了怎么办 单反镜头刮花了怎么办 股东各50股份不同意退股怎么办 退股没有协议他不愿意退钱怎么办 s7刷机有三星帐号id怎么办