D. Directed Roads

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

题意:有N个点,每个点都向其他一个点连一条有向边,形成一个N个点,N条有向边的图, 图可能有环。现在选取其中的一些边改变方向,使得图中没有环,求改变的方法数。

思路:可以想到把图按照环分块,把在同一个环中的点染成一个颜色,把其余点,也就是说不在环中的点归为另一类。

假设环中有k条边,那么每个环中有挑选一条,两条……k - 1条边进行改变方向。(1k)+(2k)+……+(k−1k) 种, 为2k−2;剩下不在环中的边不论任意地变化都不会改变图环的数量,假设剩下有N- kk条边,则为2N−kk。乘法原理相乘,取模。每次dfs一个没有跑过的点,就行了。

复杂度O(N)

代码:

/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define sgetmid int m = (l+r)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define pb push_back
#define fi first
#define se second const int INF = 0x3f3f3f3f;
const LL INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 31; /*****************************************************/ const int maxn = 2e5 + 5;
std::vector<int> G[maxn];
int pre[maxn], dfs_clock, block[maxn];
LL qpow(LL a, LL b) {
LL res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int dfs(int u, int c) {
block[u] = c;
if (pre[u]) {
int tmp = dfs_clock + 1 - pre[u];
pre[u] = ++dfs_clock;
return tmp;
}
else {
pre[u] = ++dfs_clock;
for (int i = 0; i < G[u].size(); i ++) {
int v = G[u][i];
if (block[v] && block[v] != c) continue;
return dfs(v, c);
}
return 0;
}
}
void work(int N) {
mem(pre, 0);
mem(block, 0);
dfs_clock = 0;
int ans = 0, color = 0;
LL res = 1;
for (int i = 1; i <= N; i ++) {
if (!pre[i]) {
int loop = dfs(i, ++color);
ans += loop;
if (loop) res = res * (qpow(2, loop) - 2) % mod;
}
}
res = res * qpow(2, N - ans) % mod;
cout<<res<<endl;
}
int main(int argc, char const *argv[]) {
int N;
cin>>N;
for (int i = 1; i <= N; i ++) {
int k;
scanf("%d", &k);
G[i].pb(k);
}
work(N);
return 0;
}

萌新第一次发博客,写得不好见谅。

Codeforces #369 div2 D.Directed Roads的更多相关文章

  1. CodeForces #369 div2 D Directed Roads DFS

    题目链接:D Directed Roads 题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边.这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方 ...

  2. codeforces 369 div2 C dp

    http://codeforces.com/contest/711 C. Coloring Trees time limit per test 2 seconds memory limit per t ...

  3. Codeforces Round #369 (Div. 2) D. Directed Roads 数学

    D. Directed Roads 题目连接: http://www.codeforces.com/contest/711/problem/D Description ZS the Coder and ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces 711D Directed Roads - 组合数学

    ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...

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

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

  9. 【34.40%】【codeforces 711D】Directed Roads

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

随机推荐

  1. RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm)

    RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm) 现在,我们使用.NET快速开发整合框架(RDIFramework.NET)来开发一个应用,此应用皆在说明如何使 ...

  2. 解决SQLSERVER在还原数据时出现的“FILESTREAM功能被禁用”问题

    解决SQLSERVER在还原数据时出现的“FILESTREAM功能被禁用”问题 今天由于测试需要,在网上下载了Adventureworks2008实例数据库的BAK文件,进行还原时出现了这样的错误“F ...

  3. Spring+Mybatis+SpringMVC+Maven+MySql搭建实例

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+SpringMVC+M ...

  4. Java关于md5加密

    package com.mi.util; /** * md5+salt 长度为32的加密 * @author admin * */ public class MD5 { public static v ...

  5. spring aop 中获取 request

    使用aop时需要request 和response 使用方法调用时 HttpServletRequest request = ((ServletRequestAttributes)RequestCon ...

  6. 【svn】一个设置,少写几个字

    以下场景仅适用于修改bug的时候,在提交代码的时候少写几个字,嘿嘿: 1.打开[SVN 属性],在代码目录右键 2.打开BUG跟踪设置窗口 3.输入BUG的URL前缀以及%BUGID%,如 复选框,建 ...

  7. 批处理命令——choice

    [1]choice命令简介 使用此命令可以提示用户输入一个选择项,根据用户输入的选择项再决定执行具体的过程. 使用时应该加/c:参数,c: 后应写提示可输入的字符或数字,之间无空格.冒号是可选项. 使 ...

  8. [课程设计]Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划)

    Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到 ...

  9. iframe显示错误页面

    当系统出现异常时,ifrme中显示的内容为错也页面,而不是罪顶层的框架显示错误内容,此时的解决办法是在错误页面或相关的登录页面中加入 错误页面加载的JS如下 <script type=" ...

  10. testng依赖,顺序,跳过

    依赖测试@test(dependsOnMethods = {"open"})@testpublic static void open{    System.out.println( ...