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 ...
随机推荐
- uml的几种关系总结
UML类图几种关系的总结 在UML类图中,常见的有以下几种关系:泛化(Generalization), 实现(Realization),关联(Association),聚合(Aggregati ...
- 笔记 : windows系统下 命令行 php --version 的版本与phpinfo()版本不一致问题
第一 : php --version命令cmd不随wamp中php版本改变而改变的, php命令是随着wamp安装时将:wamp/bin/php/php5.6.25[版本]自动或手动添加到环境变量, ...
- Nginx使用rewrite重新定向
[Rewrite重定向]Nginx使用rewrite重新定向 使用nginx做重新定向. nginx参考网址:http://blog.sina.com.cn/s/blog_97688f8e0100 ...
- opcode
https://www.cnblogs.com/JohnABC/p/4531029.html
- jdk8新特性-亮瞎眼的lambda表达式
jdk8之前,尤其是在写GUI程序的事件监听的时候,各种的匿名内部类,大把大把拖沓的代码,程序毫无美感可言!既然Java中一切皆为对象,那么,就类似于某些动态语言一样,函数也可以当成是对象啊!代码块也 ...
- man查看帮助命令
man -h/-help 1.在man命令帮助信息的界面中,所包含的常用操作按键及其用途 按键 用处 空格键 向下翻一页 PaGe down 向下翻一页 PaGe up 向上翻一页 home 直接前往 ...
- Day9 面向对象高级
一.方法 方法包括:普通方法.静态方法和类方法,三种方法在内存中都归属于类,区别在于调用方式不同. 普通方法:由对象调用:至少一个self参数:执行普通方法时,自动将调用该方法的对象赋值给self: ...
- python 将字节写入文本文件
想在文本模式打开的文件中写入原始的字节数据 将字节数据直接写入文件的缓冲区即可 >>> import sys >>> sys.stdout.write(b'Hell ...
- sklearn异常检测demo
sklearn 异常检测demo代码走读 # 0基础学python,读代码学习python组件api import time import numpy as np import matplotlib ...
- interface接口——公共规范标准
黑马课程学习记录: 个人理解也可以看成一个类:源代码还是.java,编译后的字节文件还是.class 抽象类中可以含有普通成员方法,但是有抽象方法的必须是抽象类或者接口, 接口中只能含有抽象方法: 创 ...