A Simple Task CodeForces - 11D

题意:输出一个无向图的简单环数量。简单环指无重复边的环。保证图无重边自环。

ans[i][j]表示"包含i中的点,以i中第一个点为起点,以j为终点"的路径条数。

对于某个i,枚举当前终点j(显然不能是首个点),产生一个状态。再枚举上一次终点k,如果能转移就转移。

如果i中点数大于2且j到i中第一个点有路,就认为产生了环。最后每个环记录了两遍,要除以2。

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
LL n,m;
bool ok[][];
LL ans[][];
LL anss;
int main()
{
LL a,b,i,j,k,fi,p,pp;
scanf("%lld%lld",&n,&m);
for(i=;i<=m;i++)
{
scanf("%lld%lld",&a,&b);
ok[a][b]=ok[b][a]=true;
}
for(i=;i<(<<n);i++)
{
pp=__builtin_popcountll(i);
if(pp==)
{
//for(j=1;j<=n;j++)
ans[i][__builtin_ffsll(i)]=;
}
else
{
fi=__builtin_ffsll(i);
for(j=;j<=n;j++)
if((i&(<<(j-)))&&j!=fi)
{
p=i^(<<(j-));
for(k=;k<=n;k++)
if((p&(<<(k-)))&&ok[k][j])
{
ans[i][j]+=ans[p][k];
}
if(ok[j][fi]&&pp>) anss+=ans[i][j];
} }
}
printf("%lld",anss/);
return ;
}
/*
http://blog.csdn.net/fangzhenpeng/article/details/49078233
http://blog.csdn.net/tobewhatyouwanttobe/article/details/38036129
http://blog.csdn.net/kk303/article/details/9621933
http://blog.csdn.net/dreamon3/article/details/51347151
*/

稍稍改进了

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
LL n,m;
bool ok[][];
LL ans[][];
LL anss;
int main()
{
LL a,b,i,j,k,fi,p,pp;
scanf("%lld%lld",&n,&m);
for(i=;i<=m;i++)
{
scanf("%lld%lld",&a,&b);
ok[a][b]=ok[b][a]=true;
}
for(i=;i<(<<n);i++)
{
pp=__builtin_popcountll(i);
fi=__builtin_ffsll(i);
if(pp==)
ans[i][fi]=;
else
{
for(j=fi+;j<=n;j++)
if((i&(<<(j-))))
{
p=i^(<<(j-));
for(k=;k<=n;k++)
if((p&(<<(k-)))&&ok[k][j])
ans[i][j]+=ans[p][k];
if(ok[j][fi]&&pp>) anss+=ans[i][j];
} }
}
printf("%lld",anss/);
return ;
}
/*
http://blog.csdn.net/fangzhenpeng/article/details/49078233
http://blog.csdn.net/tobewhatyouwanttobe/article/details/38036129
http://blog.csdn.net/kk303/article/details/9621933
http://blog.csdn.net/dreamon3/article/details/51347151
*/

A Simple Task CodeForces - 11D的更多相关文章

  1. 计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task

    E. A Simple Task Problem's Link: http://codeforces.com/problemset/problem/558/E Mean: 给定一个字符串,有q次操作, ...

  2. Codeforces 558E A Simple Task (计数排序&&线段树优化)

    题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...

  3. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树

    E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...

  4. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树+计数排序

    题目链接: http://codeforces.com/problemset/problem/558/E E. A Simple Task time limit per test5 secondsme ...

  5. Codeforces C. A Simple Task(状态压缩dp)

    题目描述:  A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. Codeforces 558E A Simple Task(权值线段树)

    题目链接  A Simple Task 题意  给出一个小写字母序列和若干操作.每个操作为对给定区间进行升序排序或降序排序. 考虑权值线段树. 建立26棵权值线段树.每次操作的时候先把26棵线段树上的 ...

  7. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树 延时标记

    E. A Simple Task time limit per test5 seconds memory limit per test512 megabytes inputstandard input ...

  8. HDU-1339 A Simple Task

    http://acm.hdu.edu.cn/showproblem.php?pid=1339 正常做法超时,要有点小技巧存在. A Simple Task Time Limit: 2000/1000 ...

  9. A Simple Task

    A Simple Task Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. 【scrapy】Item及Spider

    Items Item objects are simple containers used to collect the scraped data.They provide a dictionary- ...

  2. elasticsarch5.4集群安装

    越来越多的企业已经采用ELK解决方案来对其公司产生的日志进行分析,笔者最近着手在生产环境部署自己的ELK stack,本文介绍ELK中elasticsearch5.2集群的实现. 一.环境准备 1.系 ...

  3. Windows7系统下优化固态硬盘

    一.AHCI硬盘模式可提高硬盘性能,确定你的固态硬盘是运行在AHCI模式下,打开“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servicesmsahci” ...

  4. vi和vim上查找字符串

    方法/步骤 1 我们以samba的配置文件为例,搜索一个user的字符串. vim /etc/samba/smb.conf 打开smb.conf 2 命令模式下,输入/user "/&quo ...

  5. MySQL基础笔记(五) 视图

    一.什么是视图 视图是一个虚拟表.也就是说,视图在外观和行为上都类似于表,但它不需要实际的物理存储,只保存了视图定义(查询语句). 视图由select查询所定义 -- 当创建一个视图时,实际上是在数据 ...

  6. 解决pycharm下安装reportLab报错的问题

    在利用pycharm中自带的第三方安装工具安装reportLab时提示安装失败.失败的原因是缺失第三方扩展包.经过查阅查阅资料了解到一些python的第三方扩展包是需要python-dev支持的.我装 ...

  7. java里int类型转byte类型

    今天在做书上的一个例子的时候, 要使用byte类型,首先我很直接的就写到了byte b = 0XAA, 结果报错, 说从int转换到byte可能会有损失. 我当时就很奇怪, 为什么会出现这种情况呢? ...

  8. ASP.NET for WebApi

    WebApi,听说过吧?呵呵. 感觉比WebService,WCF要强.尤其是那个啥WCF,啥鬼东西,真难懂.真难搞.真难用. 说比WebService要强,是因为不用在本地先生成个代理.而且XML也 ...

  9. SSH常见错误

    错误一: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml] ...

  10. JavaScript 在浏览器环境中的模块管理

    如果需要,请自行复制下或下载列代码清单到本地运行(如果不修改源码,这些文件需要在同一目录 ,并且以下列文件名对应) 我只在Chrome浏览器中调试过(现在也没去处理浏览器兼容方面的问题)​1. 代码/ ...