CodeForces

来源:互联网 发布:广告录音制作软件 编辑:程序博客网 时间:2024/05/18 00:42

Directed Roads

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.

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条边的有向图,然后你可以任意改变每一条边使这个图无环,问你最多有多少种改变的方式

首先通过题意可以知道,每一个点的出度都为1,所以题中肯定不会有复杂的环(比如环交环),所以最多只会有若干个环加一条链

那很容易可以想到,对于每一条边都有两种放法,
那么我们假设现在有一个图,图中只有1个环,x是整个图的边数,y是环中的边数,那么就一共有2^(x-y)*(2^y-2)种放法,2是环中一条也不翻和翻转所有的边

假如有n个环,那有多少种呢?

通过推算我们可以得知,一共有2^(x-y1,y2-….-yn)×(2^y1-2)×(2^y2-2)×(2^yn-2)种;
也就是说ans=2^(非环上的边的边数)×(2^(环上的边数)-2)

接下来的问题就是该怎样计算呢?
我们可以用dfs判环,用vis记录一下开始的父节点,用cnt记录每一个点到父节点的距离
如果产生环了,我们就可以通过距离算出这个环的边数,然后用sum记录一下总的环上的边数

最后n减去环中的边数,剩下的就是非环的边数了

代码:

#include<stdio.h>#include<string.h>#define mem(a,b) memset(a,b,sizeof(a))#define LL long long int#define maxn 2*100000+10#define mod (1000000000+7)LL to[maxn],cnt[maxn],vis[maxn];//cnt记录到每一个点的深度,vis记录开始的父节点LL ans,sum,n;LL quick_pow(LL a,LL b)//快速幂{    LL num=1;    a=a%mod;    while(b)    {        if(b&1)            num=(num*a)%mod;        a=(a*a)%mod;        b>>=1;    }    return num;}void dfs(LL step,LL u,LL fa){    vis[u]=fa;    cnt[u]=step;    if(!vis[to[u]])        dfs(step+1,to[u],fa);    else if(vis[to[u]]==fa)//找到环    {        LL y=cnt[u]-cnt[to[u]]+1;//此环中有多少条边        sum+=y;        ans=(ans*(quick_pow((LL)2,y)-2))%mod;//此环中有2^y-2种情况    }}int main(){    mem(vis,0);    mem(cnt,0);    scanf("%lld",&n);    for(LL i=1; i<=n; i++)        scanf("%lld",&to[i]);    ans=1,sum=0;//sum记录环中总共有多少条边    for(LL i=1; i<=n; i++)    {        if(!vis[i])            dfs(0,i,i);    }    ans=(ans*quick_pow((LL)2,n-sum))%mod;    printf("%lld\n",ans);    return 0;}

总结:
开始对这道题简直是毫无头绪,因为有太多的改变的方法了,不知道该怎样判断,看了大牛的博客才知道要先找环,然后再计算,
也学到了dfs判环加确定环中结点数的方法

1 0
原创粉丝点击