Codeforces Round #369 (Div. 2) -- D. Directed Roads (DFS找环)

来源:互联网 发布:openwrt 阿里云ddns 编辑:程序博客网 时间:2024/05/22 23:51

大体题意:

给你一个有向图,可能会有环,你的操作是反向一条路,求得使得图中没有环所有方案数?

思路:

假如图中没有环的话,有n条边,答案就是2^n

如果有个m边的环,间接法考虑,总方案是2^m,  操作不改变的环的方法有2种,  什么也不动,和  每一个边都反向。

所有破坏环有2^m - 2种操作!

所以这个问题转换为 我们求出图中有几个环,求出操作数,在求剩下不成环的操作!

详细见代码:

#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 200000 + 10;const int mod = 1e9+7;int sum = 0,vis[maxn],cnt = 0,dis[maxn],a[maxn];ll ans = 1ll;int POW[maxn];ll num = 0ll;void init(){POW[0] = 1;for (int i = 1; i < maxn; ++i)POW[i] = (POW[i-1] % mod * 2 % mod)%mod;}void dfs(int cur,int val){vis[val] = cnt;dis[val] = cur;if (vis[a[val]] != 0 && vis[a[val]] == cnt){sum = (cur - dis[a[val]] + 1);num += sum;ans = (ans % mod * (POW[sum]-2) % mod) % mod;}else if (!vis[a[val]])dfs(cur+1,a[val]);}int main(){int n;init();scanf("%d",&n);for (int i = 1; i <= n; ++i){scanf("%d",&a[i]);}for (int i = 1; i <= n; ++i){if (!vis[i]){++cnt;dfs(0,i);}}ans = (ans % mod * POW[n-num] % mod) % mod; printf("%I64d\n",ans);return 0;}

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 ofn 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 townB 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 every1 ≤ i < k there is a road from townAi to townAi + 1 and another road from townAk to townA1. In other words, the roads are confusing ifsome 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 willnot 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 towni 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 modulo109 + 7.

Examples
Input
32 3 1
Output
6
Input
42 1 1 1
Output
8
Input
52 4 2 5 3
Output
28
Note

Consider the first sample case. There are 3 towns and3 roads. The towns are numbered from 1 to 3 and the roads are ,, initially. Number the roads1 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 towns1, 2, 3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of6 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 isnot confusing.


0 0