Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

CF711D Directed Roads

LuoGu: CF711D Directed Roads

CF: D. Directed Roads

找出环,环上不能方向一样。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <cstdio>
#include <algorithm>
#include <vector>
#define pb push_back
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type, class ...rest>
void read (Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
typedef long long LL;
const int N = 2e5 + 10, mod = 1e9 + 7;
vector <int> g[N];
int n, sz, tot, p[N], p2[N];
int stmp, dfn[N], low[N];
void tarjan (int u)
{
++tot;
dfn[u] = low[u] = ++stmp;
for (int v : g[u]) if (v ^ p[u])
if (!dfn[v])
{
p[v] = u;
tarjan(v);
low[u] = min(low[u], low[v]);
}
else low[u] = min(low[u], dfn[v]);
for (int v : g[u]) if (dfn[v] > dfn[u] && p[v] ^ u)
for (int t = v; t ^ u; t = p[t]) ++sz;
}
int main ()
{
read(n);
p2[0] = 1;
for (int i = 1; i <= n; ++i) p2[i] = p2[i - 1] * 2ll % mod;
for (int i = 1, a; i <= n; ++i)
read(a), g[a].pb(i), g[i].pb(a);
int res = 1;
for (int i = 1; i <= n; ++i) if (!dfn[i])
{
sz = 1, tot = 0;
tarjan(i);
if (sz == 1) sz = 2;
res = p2[tot - sz] * (p2[sz] - 2ll) % mod * res % mod;
}
write(res);
return 0;
}