# 【Codeforces Round #369 (Div. 2)】Codeforces 711D 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.

2^(n-Σsize)*π 2^(size[i]-2)。

``````#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
using namespace std;
#define LL long long
const int mod=1000000007;
int n,size[200010],tot;
vector<int> to[200010],num[200010];
stack<int> sta;
bool vis[200010];
void dfs(int u,int fa)
{
int i,j,k,v,x,y,z;
vis[u]=1;
sta.push(u);
for (i=0;i<to[u].size();i++)
if ((num[u][i]^1)!=fa)
{
v=to[u][i];
if (vis[v])
{
if (!size[tot])
{
do
{
x=sta.top();
sta.pop();
size[tot]++;
}
while (x!=v);
}
}
else dfs(v,num[u][i]);
}
if (!size[tot]) sta.pop();
}
LL pow(int p)
{
LL ans=1,base=2;
for (;p;p>>=1,base=(base*base)%mod)
if (p&1) ans=(ans*base)%mod;
return ans;
}
int main()
{
int i,j,k,p,q,x,y,z,nn;
LL ans;
scanf("%d",&n);
for (i=1;i<=n;i++)
{
scanf("%d",&x);
to[x].push_back(i);
num[x].push_back(i*2);
to[i].push_back(x);
num[i].push_back(i*2+1);
}
for (i=1;i<=n;i++)
if (!vis[i])
{
tot++;
while (!sta.empty()) sta.pop();
dfs(i,-1);
}
nn=n;
for (i=1;i<=tot;i++)
nn-=size[i];
ans=pow(nn);
for (i=1;i<=tot;i++)
ans=(ans*(pow(size[i])-2+mod))%mod;
printf("%I64d\n",ans);
}``````

• 本文已收录于以下专栏：

## codeforces 711D. Directed Roads 找环

D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standar...
• FTQOOO
• 2016年08月31日 21:09
• 569

## Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

A:Game With Sticks 水题。。。每次操作，都会拿走一个横行，一个竖行。 所以一共会操作min（横行，竖行）次。 #include #include #include #include ...
• rowanhaoa
• 2014年07月25日 14:39
• 1485

## 【解题报告】Codeforces Round #401 (Div. 2)

• TRiddle
• 2017年02月25日 17:55
• 417

## Codeforces Round #460 (Div. 2) D. Substring(图论，记忆化DFS)

• riba2534
• 2018年02月01日 10:03
• 110

## Codeforces Round #460 (Div. 2) D. Substring（拓扑排序）

• cyf199775
• 2018年02月01日 13:55
• 48

## Codeforces Round #442 (Div. 2)

A. #include using namespace std; typedef long long ll; int main() { string s; cin >> s; ...
• Egqawkq
• 2017年10月24日 02:15
• 241

## Codeforces Round #428 (Div. 2)：D. Winter is here（组合数公式）

D. Winter is here time limit per test 3 seconds memory limit per test 256 megabytes input stan...
• Jaihk662
• 2017年08月14日 14:52
• 352

## Codeforces Round #327 (div.2)(A B C D)

codeforces round #327 (div.2)
• lincifer
• 2015年10月26日 13:11
• 1104

## Codeforces Round #460 (Div. 2)

C. Seat Arrangements time limit per test 1 second memory limit per test 256 megabytes ...
• sinat_37668729
• 2018年02月01日 10:08
• 85

## Codeforces Round #460 (Div. 2) D. Substring

• litmxs
• 2018年01月31日 23:25
• 71

举报原因： 您举报文章：【Codeforces Round #369 (Div. 2)】Codeforces 711D Directed Roads 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)