CodeForces 711D Directed Roads
计数,模拟。
首先观察一下给出的图的特点:
$1.$一定存在环。
$2.$可能存在多个环。
我们对每个环计算方案数,假设环$C$上包含$x$条边,那么把环$C$破坏掉的方案数有${2^x} - 2$种。
那么答案就是每个环的方案数乘起来,再乘上${2^p}$,$p$表示不在环上的边的条数。
找环的话模拟一下就可以了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-; const int maxn=;
int n,a[maxn],cnt;
int f[maxn],g[maxn],sz;
LL b[maxn],ans=;
LL mod=1e9+; int main()
{
b[]=;
for(int i=;i<=;i++) b[i]=(*b[i-])%mod; scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]); for(int i=;i<=n;i++)
{
if(f[i]!=) continue; int now=i,p=; f[i]=p; sz++; g[i]=sz;
while()
{
if(f[a[now]]==)
{
p++; f[a[now]]=p;
g[a[now]]=sz;
now=a[now];
}
else
{
if(g[a[now]]==sz)
{
cnt=cnt+f[now]-f[a[now]]+;
ans=(ans*((b[f[now]-f[a[now]]+]-+mod)%mod))%mod;
}
break;
}
}
} ans=ans*b[n-cnt]%mod;
printf("%lld\n",ans); return ;
}
CodeForces 711D Directed Roads的更多相关文章
- Codeforces 711D Directed Roads - 组合数学
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...
- codeforces 711D Directed Roads(DFS)
题目链接:http://codeforces.com/problemset/problem/711/D 思路:由于每个点出度都为1,所以没有复杂的环中带环.DFS遍历,若为环则有2^k-2种,若为链则 ...
- 【图论】Codeforces 711D Directed Roads
题目链接: http://codeforces.com/problemset/problem/711/D 题目大意: 给一张N个点N条有向边的图,边可以逆向.问任意逆向若干条边使得这张图无环的方案数( ...
- CodeForces 711D Directed Roads (DFS判环+计数)
题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...
- CodeForces 711D Directed Roads (DFS找环+组合数)
<题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...
- Code Forces 711D Directed Roads
D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforces 711D D. Directed Roads(dfs)
题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 【34.40%】【codeforces 711D】Directed Roads
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Directed Roads CodeForces - 711D (基环外向树 )
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...
随机推荐
- facebook .net sdk 应用
浅谈 facebook .net sdk 应用 今天看了一篇非常好的文章,就放在这里与大家分享一下,顺便也给自己留一份.这段时间一直在学习MVC,另外如果大家有什么好的建议或者学习的地方,也请告知 ...
- CentOS5、6 NFS的安装配置及mount方法
一.环境介绍: 服务器:centos 192.168.1.225 客户端:centos 192.168.1.226 二.安装: NFS的安装配置:centos 5 : yum -y install n ...
- 如何用程序删除win 7下SYSTEM权限的目录
win7系统由于安装程序等操作,可能会在系统中留下一些所有权限是SYSTEM角色的目录,例如我的系统以前在C盘的QQ卸载后的遗留文件(下图). System是Windows系统中最高权限角色(组),比 ...
- phantomjs初次认识
phantomjs初次认识 最近没什么重要的任务,就抽空看了看项目组爬虫小组的代码,因为我们的爬虫主要是以python的scrapy框架为主,看起来比较方便.在看代码的时候看到一个叫phantomjs ...
- vs2008 试用版评估到期 vs2008试用版 升级正式版
心得: 解决Vs2008 试用版升级正式版 1.在控制面板里面找到vs2008的程序. 2.点击 更改删除按钮, 3.出现Vs2008的维护模式. 4.在此维护模式下,如果没有出现填写正版密匙的地方, ...
- Drupal与大型网站架构(译)- Large-Scale Web Site Infrastructure and Drupal
Drupal与大型网站架构(译)- Large-Scale Web Site Infrastructure and Drupal Linuxjournal 网站经典文章翻译,原文地址: Large-S ...
- Android 短信模块分析(三) MMS入口分析
MMS入口分析: 在Mms中最重要的两个Activity,一个是conversationList(短信列表) ,另一个就是ComposeMessageActivity(单个对话或者短信).每 ...
- 捕鱼达人代码例子下载地址 mac版
捕鱼达人代码例子下载地址 mac版: http://pan.baidu.com/share/link?shareid=1431898404&uk=3189484501
- 用indexOf获取字符窜某个字符的所有位置
indexOf方法可以传两个参数,第二个参数是传位置的参数,利用这点获取所有的位置.例如: var str = "hello world,welcome!"; var arr = ...
- java Integer 源码学习
转载自http://www.hollischuang.com/archives/1058 Integer 类在对象中包装了一个基本类型 int 的值.Integer 类型的对象包含一个 int 类型的 ...