CF711D.Directed Roads

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 a__i (a__i ≠ 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 _A_1, _A_2, ..., A__k (k > 1) such that for every 1 ≤ i < k there is a road from town A__i to town A__i + 1 and another road from town A__k to town _A_1. 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 2_n_ 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.

ZS 编码员与 Chris 猿猴已在乌代兰(Udayland)探索了相当长一段时间。他们发现乌代兰由 nn 座城镇组成,编号从 11 到 nn。

乌代兰中有 nn 条有向道路。其中第 ii 条道路从城镇 ii 指向另一座城镇 aia_i(满足 ai≠ia_i \ne i)。ZS 编码员可以翻转乌代兰中任意一条道路的方向:即若某条道路在翻转前从城镇 AA 指向城镇 BB,则翻转后它将从城镇 BB 指向城镇 AA。

ZS 编码员认为,当乌代兰的道路构成“令人困惑的”结构时,存在一个由互不相同的城镇组成的序列 A1, A2, …, AkA_1,\,A_2,\,\dots,\,A_k(其中 k>1k > 1),使得对每个 1≤i<k1 \le i < k,均存在一条从城镇 AiA_i 到城镇 Ai+1A_{i+1} 的道路,且还存在一条从城镇 AkA_k 到城镇 A1A_1 的道路。换言之,当部分道路构成某个城镇集合上的有向环时,这些道路即被视为“令人困惑的”。

现在,ZS 编码员想知道:在初始配置下,共有多少种道路翻转方案(共 2n2^n 种可能)——即选择一个道路子集进行翻转(每条被选中的道路恰好翻转一次)——使得翻转后的道路网络不再“令人困惑”。

注意:翻转后,允许某些城镇拥有不止一条出边,也允许某些城镇没有出边,甚至允许任意一对城市之间存在多条道路。

输入格式

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 _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ n, a__i ≠ i), a__i denotes a road going from town i to town a__i.

输入的第一行包含一个整数 nn(2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5)—— 表示 Udayland 中城镇的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \leq a_i \leq n,且 ai≠ia_i \neq i),其中 aia_i 表示一条从城镇 ii 指向城镇 aia_i 的道路。

输出格式

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.

输出一个整数——即翻转某些道路使得最终所有道路的集合不产生混淆的方案数。由于该数可能过大,请对 109+710^9 + 7 取模后输出答案。

输入输出样例

  • 输入#1

    3
    2 3 1

    输出#1

    6
  • 输入#2

    4
    2 1 1 1

    输出#2

    8
  • 输入#3

    5
    2 4 2 5 3

    输出#3

    28

说明/提示

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.

考虑第一个样例。共有 3 座城镇和 3 条道路。城镇编号为 1 到 3,初始道路为 、、。按此顺序将这三条道路编号为 1 至 3。

ZS the Coder 可以翻转(即改变方向)以使整个网络不再“令人困惑”的道路集合有:{1}、{2}、{3}、{1, 2}、{1, 3}、{2, 3}。注意,空集是无效的,因为若不翻转任何道路,则城镇 1、2、3 构成一个有向环,因此是“令人困惑”的;同理,翻转所有道路也会导致“令人困惑”。因此,ZS the Coder 总共可选择 6 种可能的道路集合进行翻转。

样例图片展示了第一个样例中所有可能的道路定向方式,使得该网络不“令人困惑”。

输入解题思路,AI测评打分。不知道怎么写?

首页