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. spring mvc 整理

    spring mvc 整理

  2. MIT 操作系统实验 MIT JOS lab1

    JOS lab1 首先向MIT还有K&R致敬! 没有非常好的开源环境我不可能拿到这么好的东西. 向每个与我一起交流讨论的programmer致谢!没有道友一起死磕.我也可能会中途放弃. 跟丫死 ...

  3. webservice0基础

    在学习webservice的时候,常常将ns和url花了好久时间才理解过来,这里备份下. 首先定义接口: @WebService public interface IService { @WebRes ...

  4. Asp.net MVC 简单分页 自做简单分页

    Asp.net MVC 简单分页:   public static string Pager(int page,int pageSize,int total)         {           ...

  5. 每天复习Shell—ls

    ls命令是linux下最经常使用的命令.ls命令就是list的缩写缺省下ls用来打印出当前文件夹的清单假设ls指定其它文件夹那么就会显示指定文件夹里的文件及文件夹清单. 通过ls 命令不仅能够查 ...

  6. [办公自动化]企业网IE多版本引发的网页无法访问

    今天同事的某个网页无法打开,但是在我的计算机上该网站确实又能打开. 去看了一下,他的其他网站都正常.确认网络本身没有问题. 最后,看了一下IE版本,IE11. 只好尝试一下兼容性视图的设置. 设置了一 ...

  7. CodeSmith连Oracle

    据说CodeSmith连Oracle特别麻烦,什么WIN7下不行,64位下不行.之前有个同事为了用上CodeSmith,还特地装了个XP虚拟机. 其实,还是那个连接串的问题. 操作系统64位,就要用6 ...

  8. Vue实例及生命周期

    1,Vue实例生命周期. 有时候,我们需要在实例创建过程中进行一些初始化的工作,以帮助我们完成项目中更复杂更丰富的需求,开发,针对这样的需求,Vue提供给我们一系列的钩子函数 2,Vue生命周期的阶段 ...

  9. Ubuntu18开启redis服务自启动

    设置redis服务开机自启动. 1.创建配置文件夹 sudo mkdir /etc/redis sudo cp /usr/local/redis/redis.conf /etc/redis sudo ...

  10. 源代码管理工具GIT

    01.GIT简介 svn是集中式的源代码管理工具,必须联网才能操作 git是分布式的. 有两中:一个是本地代码仓库,一个是远程代码仓库 分布式源代码管理工具 02.GIT - 本地代码仓库使用流程 1 ...