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. springboot(五):springboot整合shiro-登录认证和权限管理

    http://z77z.oschina.io/ http://www.cnblogs.com/aqsunkai/category/982003.html https://www.cnblogs.com ...

  2. html5 canvas 对角线渐变

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Hive笔记之严格模式(strict mode)

    Hive有一个严格模式,在严格模式下会对可能产生较大查询结果的语句做限制,禁止其提交执行. 一.切换严格模式 查看当前的模式: hive> set hive.mapred.mode; hive. ...

  4. B - C Looooops POJ - 2115 (扩展欧几里得)

    题目链接:https://cn.vjudge.net/contest/276376#problem/B 题目大意:for( int  i= A ; i != B; i+ = c ),然后给你A,B,C ...

  5. linux服务器如何添加sudo用户

    1. 编辑 vi /etc/ssh/sshd_config 文件,修改默认端口:默认Port为22,并且已经注释掉了,修改是把注释去掉,并修改成其它的端口. 原来用默认端口:22修改为:8975 (这 ...

  6. casperjs批量执行多个url

    var fs=require("fs"); ////-------sample.js-------// //casperオブジェクトを生成var casper = require( ...

  7. 排序算法的JS实现

    排序算法是基础算法,虽然关键在于算法的思想而不是语言,但还是决定借助算法可视化工具结合自己常用的语言实现一下 1.冒泡排序 基本思路:依次比较两两相邻的两个数,前面数比后面数小,不变.前面数比后面数大 ...

  8. Network Principle Course Summary 001

    1.物理层 物理层 协议:RJ45.CLOCK.IEEE802.3 (中继器,集线器) 作用:通过媒介传输比特,确定机械及电气规范(比特Bit) 1.1 通信基础 数据 (data) —— 运送消息的 ...

  9. linux进程的一些日常处理

    linux 下查看一个进程运行路径的方法 在linux下查看进程大家都会想到用 ps -ef|grep XXX 可是看到的不是全路径,怎么看全路径呢? 每个进程启动之后在 /proc下面有一个于pid ...

  10. sed的一些使用技巧

    一.当一个文件里有两行相同的内容,但这时只想修改第一行的内容或者第二行的内容,而不是全部修改,以下例子说明下: 1.修改匹配到第一行为port的内容(若要真修改前面记得-i): [root suppo ...