D. Directed Roads

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of n towns numbered from 1to 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.

Examples

input
3
2 3 1
output
6
input
4
2 1 1 1
output
8
input
5
2 4 2 5 3
output
28

Note

Consider the first sample case. There are 3 towns and 3 roads. The towns are numbered from 1 to 3 and the roads are  initially. Number the roads 1 to 3 in this order.

The sets of roads that ZS the Coder can flip (to make them not confusing) are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}. Note that the empty set is invalid because if no roads are flipped, then towns 1, 2, 3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of 6 possible sets ZS the Coder can flip.

The sample image shows all possible ways of orienting the roads from the first sample such that the network is not confusing.

题意:给定N个城市,给定N条有向边,然后你可以对任意条边进行翻转(改变方向),求出所有翻转后不含有环的图的个数?

题解:首先假设原给的图中不含有环,那么方法数就是2^N。如果原图中存在环,那么对一个环进行怎样的操作可以把环消掉呢?假设环由x条边组成,那你会发现除了2种操作会继续保持环外:

对环内的边什么都不操作,对环内的所有边全部翻转。那么结果就很明显了,破坏一个环的方法数就是(2^x-2)。所以问题就转化成为找到原图中所有的环,以及每个环的边数。

代码如下:

#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mod 1000000007
#define mt(A,B) memset(A,B,sizeof(A))
using namespace std;
typedef long long LL;
const int N=200000+10;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
LL to[N],vis[N],pre[N],ans=1,dis[N],T=0,sum=0;
void init()
{
pre[0]=1;
mt(vis,0);mt(dis,0);
for(int i=1;i<=N;i++)pre[i]=(pre[i-1]*2)%mod;//预处理2^i
}
void dfs(int x,int cur)//找环
{
vis[x]=T;//T代表的是第几次的dfs,
dis[x]=cur;//dis[x]代表第T次的dfs从起点走到x用了多少步
if(vis[to[x]])
{
if(vis[to[x]]==vis[x])//如果to[x]被访问且,dfs序相同,那么说明有环
{
int num=dis[x]-dis[to[x]]+1;
sum+=num;
ans=ans*(pre[num]-2+mod)%mod;
}
}
else dfs(to[x],cur+1);
}
int main()
{
#ifdef Local
freopen("data.txt","r",stdin);
#endif
int i,j,k,n;
cin>>n;
init();
for(i=1;i<=n;i++)
{
scanf("%I64d",&to[i]);
}
for(i=1;i<=n;i++)
{
if(!vis[i])
{
T++;
dfs(i,1);
}
}
ans=ans*(pre[n-sum])%mod;
cout<<ans<<endl;
}

  

Code Forces 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 (DFS判环+计数)

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

  4. 【图论】Codeforces 711D Directed Roads

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

  5. CodeForces 711D Directed Roads

    计数,模拟. 首先观察一下给出的图的特点: $1.$一定存在环. $2.$可能存在多个环. 我们对每个环计算方案数,假设环$C$上包含$x$条边,那么把环$C$破坏掉的方案数有${2^x} - 2$种 ...

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

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

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

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

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

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

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

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

随机推荐

  1. 使用Map辅助拼装树状结构,消除递归调用

    目前菜单或其他树状结构在数据库中的存储,多数是以一个parentid作为关联字段,以一维形式存储.使用时全部查询出来,然后在内存中拼装成树状结构.现在主要涉及的是拼装方法的问题. 一般可以进行 递归调 ...

  2. JavaScript入门介绍(二)

    JavaScript入门介绍 [函数] 函数function 是Javascript的基础模块单元,用于代码的复用.信息影藏和组合调用. function a(){} 函数对象Function Lit ...

  3. yii2源码学习笔记(十八)

    View继承了component,用于渲染视图文件:yii2\base\View.php <?php /** * @link http://www.yiiframework.com/ * @co ...

  4. android中listview的item滑动删除效果(已解决listview点击问题)

    领导看到iphone上tableview有个滑动删除的效果,要求在android上也实现,搜了下资料,实现起来比较简单,可弄到后面,居然不能点击了,把一篇文章中的代码修改了一下,捣鼓了一番,搞定,下面 ...

  5. python入门 第二天笔记

    程序主文件标志if __name__=="__main__": 在程序执行python 1.py 时候 程序1.py __name__ 为 main调用其他文件是,__name__ ...

  6. C# 拷贝数组的几种方式

    突然学到了,所以就放到博客上来共享一下,权当是学习日记吧. 首先说明一下,数组是引用类型的,所以注意不要在复制时复制了地址而没有复制数值哦! 其实在复制数组的时候,一定要用new在堆中开辟一块新的空间 ...

  7. poj 2318 TOYS

    TOYS 题意:给定一个如上的长方形箱子,中间有n条线段,将其分为n+1个区域,给定m个玩具的坐标,统计每个区域中的玩具个数. 思路:这道题很水,只是要知道会使用叉乘来表示点在线的上面还是下面: 当a ...

  8. Java按正则提取字符串

    在Java开发中,有时会遇到一些比较别扭的规则从字符串中提取子字符串,规则无疑是写正则表达式来表达了,那按照正则来提取子字符串就会用到java.util.regex包. java.util.regex ...

  9. canvas仿黑客帝国的字符下落

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  10. python 处理cookie简单很多啊 httpclient版本是4.3.3

    模拟登录流程: 1 请求host_url 2 从host_url中解析出 隐藏表单 的值 添加到POST_DATA中 3 添加账户,密码到POST_DATA中 4 编码后,发送POST请求    要点 ...