## 缺氧

2015/12/03 Go the extra mile.

http://codeforces.com/contest/711/problem/D

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
typedef long long  ll;
const int N=2*100000+50;
int vis[N];
vector<int > mp[N];
int tol,huan,tmp;
ll mod=1e9+7;
int dep[N];
long long powe_m(long long  a,long long  b )
{
long long ans=1;
long long tmp=a;
while(b!=0)
{
if (b&1)
ans=ans*tmp%mod;
tmp=tmp*tmp%mod;
b=b>>1;
}
return ans;
}
ll two[N];
void dfs(int x,int h)
{
tol++;
vis[x]=1;
dep[x]=h;
for (int i=0;i<mp[x].size();i++)
{
int v=mp[x][i];
if (dep[v])
{
huan=dep[x]-dep[v]+1;
break;
}
if (vis[v]) break;
dfs(v,h+1);
}
dep[x]=0;
}
int  main()
{
ll n,m,k;
cin>>n;
two[0]=1;
for (int i=1;i<=n;i++)
two[i]=two[i-1]*2%mod;
int x;
for (int i=1; i<=n; i++)
{
scanf("%d",&x);
mp[i].push_back(x);
}
ll ans=1;
for (int i=1; i<=n; i++)
{
if (vis[i] )continue;
tol=0;
huan=0;
dfs(i ,1);
if(huan )
ans= ans* ((two[huan]-2+mod)%mod)%mod*two[tol-huan]%mod;
else
ans= ans  *two[tol]%mod;
}
printf("%lld\n",(ans%mod+mod)%mod);

return 0;

}


#### D. Almost Acyclic Graph【拓扑排序判环】

2018-01-17 17:41:49

#### Codeforces 919D - Substring 【有向图判环+DP】

2018-01-31 23:18:49

#### 【Python】对一个有向无环图(Directed Acyclic Graph，DAG)G进行拓扑排序

2018-06-05 19:17:17

#### codeforces 711D. Directed Roads 找环

2016-08-31 21:09:14

#### CodeForces - 711D Directed Roads(找环）

2018-01-05 19:44:59

#### Codeforces 711D Directed Roads dfs(找环)

2017-01-09 11:38:51

#### dfs找环

2016-10-22 22:16:20

#### 有向图找环

2013-07-01 20:49:44

#### ZSTUOJ 4191 无向图找环（DFS应用，经典）

2015-04-14 23:18:35

#### 环的寻找：寻找无向图中所有存在的环-删除点法

2017-11-09 10:12:13