计数,模拟。

首先观察一下给出的图的特点:

$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的更多相关文章

  1. Codeforces 711D Directed Roads - 组合数学

    ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...

  2. codeforces 711D Directed Roads(DFS)

    题目链接:http://codeforces.com/problemset/problem/711/D 思路:由于每个点出度都为1,所以没有复杂的环中带环.DFS遍历,若为环则有2^k-2种,若为链则 ...

  3. 【图论】Codeforces 711D Directed Roads

    题目链接: http://codeforces.com/problemset/problem/711/D 题目大意: 给一张N个点N条有向边的图,边可以逆向.问任意逆向若干条边使得这张图无环的方案数( ...

  4. CodeForces 711D Directed Roads (DFS判环+计数)

    题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...

  5. CodeForces 711D Directed Roads (DFS找环+组合数)

    <题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...

  6. Code Forces 711D Directed Roads

    D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. codeforces 711D D. Directed Roads(dfs)

    题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. 【34.40%】【codeforces 711D】Directed Roads

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. Directed Roads CodeForces - 711D (基环外向树 )

    ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...

随机推荐

  1. IOS内存泄漏

    1. . - (void)viewDidLoad { [superviewDidLoad]; self.view.frame=CGRectMake(, , , ); NSArray *title1=[ ...

  2. MongoDB学习之--增删改查(1)

    本文是对mongodb学习的一点笔记,主要介绍最简单的增删改操作,初学,看着API,有什么错误,希望大家指正:(使用官方驱动) 1.增 增加操作是最简单的,构造bsonDcument插入即可: 方式1 ...

  3. 关于arcengine权限的设置

    对于AE开发时候,如果调用arctoolbox中的部分功能,权限是arcview或者arceditor是无法执行的,因为权限不够. arcgis9.3的有3个权限arcview. arceditor. ...

  4. python打包成window可执行程序

    python程序可以通过python hello.py执行,但是需要安装python的解释器,并配置环境变量,打包成exe程序之后可以直接执行. 使用setup工具和py2exe可以做到这一点. 最简 ...

  5. AOP in Spring

    AOP in Spring 是不是已经对包裹在每个业务周围的异常处理.事务管理.性能监控.日志记录等重复出现的代码感到厌倦,那么是时候轮到AOP出场了.不得不承认程序员的惰性有时候会是一件好事(毕竟提 ...

  6. DateTimePicker.Text不靠谱

    DateTimePicker.Text不靠谱 获取时:在DateTimePicker.ValueChanged事件中,获取到的Text有可能是string.Empty!!!,特别当ValueChang ...

  7. Arduino 各种模块篇 蓝牙模块 手机蓝牙控制Arduino LED灯

    解决方案. 条件: 1.手机android 商店下载 blueTerm 2.向arduino中载入如下代码: char val; ; void setup() { Serial.begin(); pi ...

  8. 常用PHP正则表达式

    获取所有图片网址preg_match_all(“/ src=(\”|\’){0,}(http:\/\/(.+?))(\”|\’|\s|>)/is”,$text,$img); 匹配中文字符的正则表 ...

  9. 通过Jasmine和Guard自动测试JavaScript

    原文标题:Autotesting JavaScript with Jasmine and Guard 原文地址:http://edspencer.net/2013/06/15/autotesting- ...

  10. sql查询语句优化需要注意的几点

    为了获得稳定的执行性能,SQL语句越简单越好.对复杂的SQL语句,要设法对之进行简化. 常见的简化规则如下:   1)不要有超过5个以上的表连接(JOIN) 2)考虑使用临时表或表变量存放中间结果. ...