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. 蓦然回首,Java 已经 24 岁了!

    01.蓦然 真没想到,Java 竟然 24 岁了(算是 90 后)! 提起 Java,印象最深刻的当然就是: class Cmower {  public static void main(Strin ...

  2. MySQL Study之--Percona Server版本号

    MySQL Study之--Percona Server版本号 1.简单介绍      Percona 为 MySQL 数据库server进行了改进.在功能和性能上较 MySQL 有着非常显著的提升. ...

  3. Java中正则Matcher类的matches()、lookAt()和find()的差别

    參考博文地址:http://www.oseye.net/user/kevin/blog/170 1.matcher():仅仅有在整个字符串全然匹配才返回true,否则返回false. 可是假设部分匹配 ...

  4. HDU 5288(OO’s Sequence-区间互质情况统计)

    OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  5. [LeetCode][Java] Roman to Integer

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  6. FileWriter简单用法并记录日志

    import java.io.*; import java.util.Date; /** * Created by Administrator on 2018/4/6. */ public class ...

  7. Python开发【第2节】【Python运算符】

    Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 成员运算符 身份运算符 运算符优先级 1.算术运算符 假设变量a = 10,变量b = 21: ...

  8. Ubuntu x86 64 settup nginx rtmp server

    常常搭建nginxserver,可是好像每次的情况都不同,这次具体记录这个过程: 平台:unbutu 10.04 内核:2.6.32-33-generic 1, 编译环境搭建. sudo apt-ge ...

  9. [办公自动化]EXCEL不大,但是保存很慢

    今天同事有一个excel文件.office 2007格式的. 折腾了半天.按照以往的经验,定位-对象,应该可以删除. 后来在“编辑”窗格的“查找和选择”里面,单击“选择窗格“.可以看到很多”pictu ...

  10. 建立自己的私有docker(ssl&login auth)

    建立私有docker需要先建立ssl证书,然后建立htpass的登陆证书 最后使用nginx配置docker-compose.yml 参考: https://www.digitalocean.com/ ...