D. Directed Roads

题目连接:

http://www.codeforces.com/contest/711/problem/D

Description

ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of n towns numbered from 1 to n.

There are n directed roads in the Udayland. i-th of them goes from town i to some other town ai (ai ≠ i). ZS the Coder can flip the direction of any road in Udayland, i.e. if it goes from town A to town B before the flip, it will go from town B to town A after.

ZS the Coder considers the roads in the Udayland confusing, if there is a sequence of distinct towns A1, A2, ..., Ak (k > 1) such that for every 1 ≤ i < k there is a road from town Ai to town Ai + 1 and another road from town Ak to town A1. In other words, the roads are confusing if some of them form a directed cycle of some towns.

Now ZS the Coder wonders how many sets of roads (there are 2n variants) in initial configuration can he choose to flip such that after flipping each road in the set exactly once, the resulting network will not be confusing.

Note that it is allowed that after the flipping there are more than one directed road from some town and possibly some towns with no roads leading out of it, or multiple roads between any pair of cities.

Input

The first line of the input contains single integer n (2 ≤ n ≤ 2·105) — the number of towns in Udayland.

The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n, ai ≠ i), ai denotes a road going from town i to town ai.

Output

Print a single integer — the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 109 + 7.

Sample Input

3

2 3 1

Sample Output

6

Hint

题意

给你一个\(n\)点\(n\)边的无向图,你可以翻转任意几条边,但是每条边只能翻转一次,问你有多少种方案,使得这个图不存在环。

题解:

由于n点n边,所以不存在环套环的情况。

对于每一个环,我们都有2n-2种方案使得这个环不存在,就是2n减去全部翻转,和全部不翻转的方案。

然后不在环上的边,爱翻翻,乘上去这个答案就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
const int maxn = 2e5+7;
long long f[maxn];
int a[maxn],n,cnt;
int vis[maxn];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
f[0]=1;
for(int i=1;i<=n;i++)
f[i]=f[i-1]*2ll%mod;
int num = n;
long long ans = 1;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
int x=i;
int now=cnt;
while(vis[x]==0)
{
vis[x]=++cnt;
x=a[x];
}
if(vis[x]>now)
{
int pnum=cnt-vis[x]+1;
num=num-pnum;
ans=ans*(f[pnum]-2+mod)%mod;
}
}
}
ans=ans*f[num]%mod;
cout<<ans<<endl;
}

Codeforces Round #369 (Div. 2) D. Directed Roads 数学的更多相关文章

  1. Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂

    题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...

  2. Codeforces Round #369 (Div. 2) D. Directed Roads dfs求某个联通块的在环上的点的数量

    D. Directed Roads   ZS the Coder and Chris the Baboon has explored Udayland for quite some time. The ...

  3. Codeforces Round #369 (Div. 2) D. Directed Roads (DFS)

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

  4. Codeforces Round #369 (Div. 2)-D Directed Roads

    题目大意:给你n个点n条边的有向图,你可以任意地反转一条边的方向,也可以一条都不反转,问你有多少种反转的方法 使图中没有环. 思路:我们先把有向边全部变成无向边,每个连通图中肯定有且只有一个环,如果这 ...

  5. Codeforces Round #302 (Div. 2) D - Destroying Roads 图论,最短路

    D - Destroying Roads Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544 ...

  6. Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)

    题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...

  7. Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)

    Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...

  8. Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路

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

  9. Codeforces Round #369 (Div. 2) C. Coloring Trees(简单dp)

    题目:https://codeforces.com/problemset/problem/711/C 题意:给你n,m,k,代表n个数的序列,有m种颜色可以涂,0代表未涂颜色,其他代表已经涂好了,连着 ...

随机推荐

  1. python 基础知识 列表的 增删改查 以及迭代取值

    """ python 列表 通用方法 元组.数组.字典 取值方法 [] 列表中可以存储不同类型的数据 函数 封装了独立的功能可以直接调用 函数名(参数) 方法 和函数类似 ...

  2. CSS规范 - 代码格式--(来自网易)

    选择器.属性和值都使用小写 在xhtml标准中规定了所有标签.属性和值都小写,CSS也是如此.单行写完一个选择器定义 便于选择器的寻找和阅读,也便于插入新选择器和编辑,便于模块等的识别.去除多余空格, ...

  3. HDU 2056 龟兔赛跑 (DP)

    题意:见题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2059 解题报告:以前一直没看出来这题是个DP题,知道是DP题就简单了 .首先要把起点和终点看成 ...

  4. 第12月第15天 mysqlx boost reswift

    1. INSTALL PLUGIN mysqlx SONAME 'mysqlx.so' https://yq.aliyun.com/articles/38288 2. boost boost::sha ...

  5. 第9月第5天 AVVideoAverageBitRateKey

    1. https://stackoverflow.com/questions/11751883/how-can-i-reduce-the-file-size-of-a-video-created-wi ...

  6. VS 多工程代码编写

    VS工作目录,输出目录 C++项目,解决方案总文件夹下就只包含解决方案配置文件sln和一个项目总文件夹和一个Debug文件夹以及一个Release文件夹(共四个东东,其中Debug和Release文件 ...

  7. Hacking Using Beef-Xss

    1.环境 hacker:192.168.133.128 os:Kali victims:192.168.133.1    os:win8 2.前期配置 首先进入beef-xss主目录,编辑配置文件,将 ...

  8. 搭建RabbitMQ集群(Docker)

    前一篇搭建RabbitMQ集群(通用)只是把笔记直接移动过来了,因为我的机器硬盘已经满了,实在是开不了那么虚拟机. 还好,我的Linux中安装了Docker,这篇文章就简单介绍一下Docker中搭建R ...

  9. PHP取整函数之ceil,floor,round,intval的区别

    我们经常用到的PHP取整函数,主要是:ceil,floor,round,intval. ceil -- 进一法取整 说明 float ceil ( float value ) 返回不小于 value ...

  10. dojo 加载自定义module的路径问题

    因为最近想学学ArcGIS API for JavaScript ,翻了下ESRI的官网guide,发现其是基于dojo框架的,看了两页实在看不懂,于是先来熟悉下dojo框架.人蠢不能怪社会%> ...