穹妹的家

In solitude , where we are least alone.

思路：

n个点n条边，无自环，每点出度为1，这个模型应该也出过很多次了。

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
typedef long long ll;
const ll mod = 1e9+7;
vector<int>G[N];
int in[N];
bool hs[N];
ll qpow(ll a, ll k){
ll res = 1;
while(k){
if(k&1) res = (res*a)%mod;
a = (a*a)%mod;
k >>= 1;
}
return res;
}
ll solve(int rt){
int all = 0;
queue<int>q, qq;
q.push(rt); hs[rt] = 1;
while(!q.empty()){
int u = q.front(); q.pop();
all++;
if(in[u] == 1) qq.push(u);
for(int v : G[u]){
if(!hs[v]){
hs[v] = 1;
q.push(v);
}
}
}
int ciy = all;
while(!qq.empty()){
int u = qq.front(); qq.pop();
--ciy;
for(int v : G[u]){
if((--in[v]) == 1) qq.push(v);
}
}
return qpow(2, all-ciy)*((qpow(2, ciy)-2+mod)%mod)%mod;
}
int main(){
int n;
scanf("%d", &n);
ll ans = 1;
for(int x, i = 1; i <= n; ++i){
scanf("%d", &x);
G[x].push_back(i);
G[i].push_back(x);
in[x]++, in[i]++;
}
for(int i = 1; i <= n; ++i){
if(hs[i]) continue;
ans = ans*solve(i)%mod;
}
printf("%lld\n", ans);
}

2016-08-30 15:01:42

2016-08-31 21:09:14

Codeforces Round #369 (Div. 2) D. Directed Roads （dfs+组合数学 图论）

2016-08-30 19:06:28

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

2018-01-31 23:18:49

2015-05-15 13:51:14

Codeforces Round #369 (Div. 2) D. Directed Roads 图论、组合学、二重dfs、并查集形式的图、Interesting、好题

2016-09-06 19:06:06

Codeforces 543B Destroying Roads 【暴力 SPFA】

2015-11-10 20:40:13

[最短路 杂题] Codeforces 806D VK Cup 2017 Round 3 D. Perishable Roads

2017-05-20 07:20:54

2015-05-14 09:15:25

2017-03-22 19:26:29