计数,模拟。

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

$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. 不是技术牛人,如何拿到国内IT巨头的Offer【转】

    转:http://blog.csdn.net/lsldd/article/details/13506263 不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人 ...

  2. [译]反-反汇编 & 混淆 #1: 苹果没有遵循自己制定的Mach-O规范?

    原文地址:http://reverse.put.as/2012/02/02/anti-disassembly-obfuscation-1-apple-doesnt-follow-their-own-m ...

  3. Block formatting context

    不会JS中的OOP,你也太菜了吧!(第一篇)   一.你必须知道的 1) 字面量 2) 原型 3) 原型链 4) 构造函数 5) 稳妥对象(没有公共属性,而且其方法也不引用this的对象.稳妥对象适合 ...

  4. UISearchDisplayController UISearchBar

    分组表+本地搜索 UISearchDisplayController  UISearchBar 的使用 效果图 @interface CityListViewController :UIViewCon ...

  5. Object-c学习之路六(oc字符串文件读写)

    // // main.m // NSString // // Created by WildCat on 13-7-25. // Copyright (c) 2013年 wildcat. All ri ...

  6. java读取properties 文件信息

      src下config/tank.properties文件 initTankCount=10 ReinitTankCount=8 Etmspeed=15 Mtmspeed=15 MTankCount ...

  7. ARP 和 RARP

    ARP 和 RARP 1.ARP      地址解析协议(Address Resolution Protocol,ARP)是在仅知道主机的IP地址时确地址解析协议定其物理地址的一种协议.      在 ...

  8. WCF 学习笔记之异常处理

    WCF 学习笔记之异常处理 1:WCF异常在配置文件 <configuration> <system.serviceModel> <behaviors> <s ...

  9. 公共建筑能耗监测平台的GPRS通讯服务器的开发方法分享

    公共建筑能耗监测平台的GPRS通讯服务器的开发方法分享 在这个文章里面我将用一个实际的案例来分享如何来构建一个能够接受3000+个连接的GPRS通讯服务器软件,这个软件被我认为是一个艺术品,实现周期为 ...

  10. Arduino 不同Arduino衍生板子的问题

    arduino IDE装上的时候,要记得在windows平台安装驱动. 如果不安装驱动的话,烧写程序的时候也许会遇到下面的现象. 原因有如下几种: 1,arduino控制板或者COM口没有选对,这种问 ...