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 ...
随机推荐
- Java中通过Class类获取Class对象的方法详解
方式1:通过Object类的getObject()方法 Person p = new Person(); Class c = p.getClass(); 方式2: 通过 类名.class 获取到字节码 ...
- 学习笔记: ES7(ES2016)新功能
ES7添加了两个新功能 : 1. Array.prototype.includes() 2. 指数运算符 1 .Array.prototype,includes() 判断指定的元素是否存在于数组中, ...
- Unity shader学习之Grab Pass实现玻璃效果
GrabPass可将当前屏幕的图像绘制在一张纹理中,可用来实现玻璃效果. 转载请注明出处:http://www.cnblogs.com/jietian331/p/7201324.html shader ...
- Python全栈-数据库存储引擎
一.存储引擎概述 在个人PC机中,不同的文件类型有不同的处理机制进从存取,例如文本用txt打开.保存:表格用excel读.写等.在数据库中,同时也存在多种类型的表,因此数据库操作系统中也应拥有对各种表 ...
- Windows server 2008 R2实现多用户远程连接 (转)
经常使用远程桌面的朋友可能会注意到,Windows server 2008 R2中,远程桌面最多只允许两个人远程连接,第三个人就无法连接过去,但是生产环境中有一些服务器可能有许多人需要连接上去,而微软 ...
- 01 while 循环输入1 2 3 4 5 6 8 9 10
start = 1while True: if start == 7: start += 1 continue print(start) start ...
- jQuery安装
http://www.runoob.com/jquery/jquery-install.html 网页中添加jQuery: 方法一:可以从http://jquery.com/download/ 下载j ...
- 【Hbase学习之一】Hbase 简介
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 apache-hive-2.1.3 ...
- Qt & VS2013 报错:There's no Qt version assigned to this project for platform Win32
如果你想了解关于Qt与VS2013开发环境搭建,可以至此翻页. 这里主要分享环境已搭建成功,在构建项目时遇到的报错解决方案. [1]Qt 与 VS2013开发环境构建时报错 报错界面如下: 注意:对话 ...
- 举例说明Unicode 和UTF-8之间的转换
1)写这篇博客的原因 首先我要感谢这篇博客,卡了很久,看完下面这篇博客终于明白Unicode怎么转换成UTF-8了. https://blog.csdn.net/qq_32252957/article ...