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 from1 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.
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.
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.
3
2 3 1
6
4
2 1 1 1
8
5
2 4 2 5 3
28
Consider the first sample case. There are 3 towns and 3 roads. The towns are numbered from 1 to 3 and the roads are ,
,
initially. Number the roads 1 to 3 in this order.
The sets of roads that ZS the Coder can flip (to make them not confusing) are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}. Note that the empty set is invalid because if no roads are flipped, then towns 1, 2, 3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of 6 possible sets ZS the Coder can flip.
The sample image shows all possible ways of orienting the roads from the first sample such that the network is not confusing.
题目大意
给定一个有向图,每个点的出度为1。有一种操作可以将一条边的方向翻转,问有多少种不同的操作方案使得操作后图中不存在点数大于1的强连通分量。
因为图很特殊,所以有比较特殊的计算方法。
容易发现,对于已经存在的强连通分量,只有全部翻转中间的边和什么都不做的量两种方案不可行,其他都可行。
对于不在强连通分量内的边,可翻转也可以不翻转。
然后用dfs找环,用乘法原理乘一乘就好。
Code
/**
* Codeforces
* Problem#711D
* Accepted
* Time: 62ms
* Memory: 4376k
*/
#include <bits/stdc++.h>
using namespace std; const int M = 1e9 + ; int n;
int cnt = ;
int ric = ;
int *suf;
int *vid;
int *bel; int qpow(int a, int pos) {
int rt = , pa = a;
for (; pos; pos >>= , pa = pa * 1ll * pa % M)
if (pos & )
rt = rt * 1ll * pa % M;
return rt;
} inline void init() {
scanf("%d", &n);
suf = new int[(n + )];
vid = new int[(n + )];
bel = new int[(n + )];
for (int i = ; i <= n; i++)
scanf("%d", suf + i);
memset(vid, , sizeof(int) * (n + ));
memset(bel, , sizeof(int) * (n + ));
} int dfs(int node, int id) {
vid[node] = ++cnt;
bel[node] = id;
int e = suf[node];
if (vid[e])
return (bel[e] == id) ? (vid[node] - vid[e] + ) : ();
return dfs(e, id);
} int ans = ;
inline void solve() {
for (int i = , s; i <= n; i++)
if (!vid[i]) {
s = dfs(i, i);
if(s)
ans = (ans * 1ll * ((qpow(, s) + M - ) % M)) % M;
ric += s;
}
ans = (ans * 1ll * qpow(, n - ric)) % M;
printf("%d\n", ans);
} int main() {
init();
solve();
return ;
}
Codeforces 711D Directed Roads - 组合数学的更多相关文章
- codeforces 711D Directed Roads(DFS)
题目链接:http://codeforces.com/problemset/problem/711/D 思路:由于每个点出度都为1,所以没有复杂的环中带环.DFS遍历,若为环则有2^k-2种,若为链则 ...
- 【图论】Codeforces 711D Directed Roads
题目链接: http://codeforces.com/problemset/problem/711/D 题目大意: 给一张N个点N条有向边的图,边可以逆向.问任意逆向若干条边使得这张图无环的方案数( ...
- CodeForces 711D Directed Roads (DFS判环+计数)
题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...
- CodeForces 711D Directed Roads
计数,模拟. 首先观察一下给出的图的特点: $1.$一定存在环. $2.$可能存在多个环. 我们对每个环计算方案数,假设环$C$上包含$x$条边,那么把环$C$破坏掉的方案数有${2^x} - 2$种 ...
- CodeForces 711D Directed Roads (DFS找环+组合数)
<题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...
- Code Forces 711D Directed Roads
D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforces 711D D. Directed Roads(dfs)
题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 【34.40%】【codeforces 711D】Directed Roads
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Directed Roads CodeForces - 711D (基环外向树 )
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...
随机推荐
- Linux 7.x 设置主机名称
Linux 7.x 设置主机名称 在Linux7.x 版本中,临时设置主机名称使用指令:hostnamectrl set-name 主机名称 [root@localhost ~]# hostname ...
- Linux MySQL数据库文件同步及数据库备份
Mysql数据库链接 mysql -uroot -p -hdatacenter.jiaofukeyan.com -P33069 1.文件同步 rsync -avz --delete root@(需要同 ...
- netCore webapi Uow实现方式
参照 http://www.cnblogs.com/GreedyL/p/7474368.html 思路: 1.创建ActionFilter拦截请求,在拦截器中注入IUOW,IUOW里面注入IDbCon ...
- arcgis api for javascript 添加图层时设置标注,自定义符号
<!DOCTYPE html> <html> <head> <meta charset="gb2312"> <meta nam ...
- Python之装饰器的实例
1.1装饰器的应用:参数类型检查 函数参数的检查,一定是在函数外 函数应该作为参数,传入到检查函数中 检查函数拿到函数传入的实际参数,与形参声明对比 __annotations__属性是一个字典,其中 ...
- html5-css综合练习
div{ width: 600px; height: 800px; padding: 40px; font-size: 12px; line-height: 25px; ...
- F12搜索json内容
- JSP FreeMarker Velocity 原理
JSP原理 JSP的运行原理:JSP 本质上是一个Servlet. 每个JSP 页面在第一次被访问时,JSP引擎将它翻译成一个Servlet 程序,然后再把这个 Servlet 源程序编译成Servl ...
- python 简单了解namedtuple
namedtuple类位于collections模块,有了namedtuple后通过属性访问数据能够让我们的代码更加的直观更好维护 namedtuple能够用来创建类似于元祖的数据类型,除了能够用索引 ...
- python os.path.dirname()
----返回文件所在的路径 ----如果path变量直接是文件名则返回空