time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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 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条边。

那就保证了一张图里面不会有复杂的环

最多只有这样



也就是说每个子图里面最坏的情况就是一个环带着几条链。

先考虑最简单的情况。即一条链。

那么设链的边数为x,则有2^x种翻转的方法(每条边都可以选择翻转或不翻转);最后都没有环。

那对于一个环里面的x条边呢?

环一定是简单的换。即1->2->3->1类似这样的

可以看到每条边如果翻转一下都可以破坏这个环。

那么x条边总共有2^x种方法破坏它。

而全部都翻转或者全部都不翻转所形成依然是个环。所以要减去2

即(2^X)-2

然后每张子图都是一个环带几条链。

我们先处理出所有子图的链上的边的个数cnt;

(链的边的个数可用拓扑排序求得);

ans = 2^cnt;

然后对于第i个子图上的环的边的个数Xi

根据乘法原理

ans = ans*∏((2^xi)-2);

(∏代表连乘);

在做的过程中取余就好

#include <cstdio>
#include <queue>
#include <iostream> using namespace std; const int MAXN = 3e5;
const int MOD = 1e9 + 7; int n;
long long re[MAXN];
queue <int> dl;
int to[MAXN],du[MAXN];
long long ans;
bool vis[MAXN]; void input(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input(n);
re[0] = 1;
for (int i = 1; i <= n; i++)
re[i] = (re[i - 1] * 2) % MOD;
for (int i = 1; i <= n; i++)
{
input(to[i]);
du[to[i]]++;
}
int cnt = 0;
for (int i = 1;i <= n;i++)
if (!du[i])
{
dl.push(i);
cnt++;
}
while (!dl.empty())
{
int x = dl.front();
vis[x] = true;
dl.pop();
du[to[x]]--;
if (du[to[x]] == 0)
{
dl.push(to[x]);
cnt++;
}
}
ans = re[cnt];//cnt是所有子图上链的边的个数
for (int i = 1; i <= n; i++)
if (!vis[i])//每个子图的环的个数要单独算
{
int x = i, now = 0;
while (!vis[x])
{
now++;
vis[x] = true;
x = to[x];
}
ans = (ans*(re[now] - 2 + MOD)) % MOD;
}
printf("%I64d\n", ans);
return 0;
}

【34.40%】【codeforces 711D】Directed Roads的更多相关文章

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

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

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. Codeforces 711 D. Directed Roads (DFS判环)

    题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...

  4. codeforces 711 D.Directed Roads(tarjan 强连通分量 )

    题目链接:http://codeforces.com/contest/711/problem/D 题目大意:Udayland有一些小镇,小镇和小镇之间连接着路,在某些区域内,如果从小镇Ai开始,找到一 ...

  5. 【34.88%】【codeforces 569C】Primes or Palindromes?

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

  6. 【34.57%】【codeforces 557D】Vitaly and Cycle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【47.40%】【codeforces 743B】Chloe and the sequence

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【24.34%】【codeforces 560D】Equivalent Strings

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

  9. 【40.17%】【codeforces 569B】Inventory

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. oled屏幕模块

    oled屏幕模块似乎是厂家提供的 也许可以根据屏幕驱动芯片去写 根据现在了解的芯片一般有两个:SH1106和SSD1306 不过这次我们用的是SSD1306芯片驱动的屏幕 下面是从裸屏到模块的pcb: ...

  2. POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题

    两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...

  3. Loadrunner--Analysis网页细分图

    续LR实战之Discuz开源论坛项目,之前一直是创建虚拟用户脚本(Virtual User Generator)和场景(Controller),现在,终于到了LoadRunner性能测试结果分析(An ...

  4. GO语言学习(二)Windows 平台下 LiteIDE 的安装和使用

    1. 安装 Go 语言并设置环境变量 参考GO语言学习(一) 2. MinGW 的下载和安装 Windows 下的 Go 调试还需要安装 MinGW. 2.1 下载安装工具的安装 最新版本下载安装工具 ...

  5. 【】maze

    [链接]点击打开链接 [题意] 小 T 被放到了一个迷宫之中,这个迷宫由 n 个节点构成,两个节点之间可能存在多条无 向边,小 T 的起点为 1 号节点,终点为 n 号节点.有 m 条无向边,对于每一 ...

  6. vim编辑器经常使用命令

    高级一些的编辑器,都会包括宏功能,vim当然不能缺少了.在vim中使用宏是很方便的: :qx     開始记录宏,并将结果存入寄存器xq     退出记录模式@x     播放记录在x寄存器中的宏命令 ...

  7. 为什么通过空指针(NULL)能够正确调用类的部分成员函数

    #include <iostream> using namespace std; class B { public: void foo() { cout << "B ...

  8. java学习笔记之基础语法(二)

    1.数组: 概念:同一种类型数据的集合,其实,数组就是一个容器 优点:可以方便的对其进行操作,编号从0开始,方便操作这些元素. 2,数组的格式 元素类型[]数组名=new 元素类型[数组元素个数]: ...

  9. Android系统开发(1)——GCC编译器的编译和安装过程

    GCC编译器介绍 GCC编译器(GNG C Compiler)是GNU项目中符合ANSI C标准的编译系统,能够编译C  C++  Object C等语言编写的程序,同时GCC也是一个交叉编译器,特别 ...

  10. php xml转数组,数组转xml,array转xml,xml转array

    //数组转XML function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key=>$val) ...