Codeforces Round #369 (Div. 2) -- D. Directed Roads (DFS找环)
来源:互联网 发布:openwrt 阿里云ddns 编辑:程序博客网 时间:2024/05/22 23:51
大体题意:
给你一个有向图,可能会有环,你的操作是反向一条路,求得使得图中没有环所有方案数?
思路:
假如图中没有环的话,有n条边,答案就是2^n
如果有个m边的环,间接法考虑,总方案是2^m, 操作不改变的环的方法有2种, 什么也不动,和 每一个边都反向。
所有破坏环有2^m - 2种操作!
所以这个问题转换为 我们求出图中有几个环,求出操作数,在求剩下不成环的操作!
详细见代码:
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 200000 + 10;const int mod = 1e9+7;int sum = 0,vis[maxn],cnt = 0,dis[maxn],a[maxn];ll ans = 1ll;int POW[maxn];ll num = 0ll;void init(){POW[0] = 1;for (int i = 1; i < maxn; ++i)POW[i] = (POW[i-1] % mod * 2 % mod)%mod;}void dfs(int cur,int val){vis[val] = cnt;dis[val] = cur;if (vis[a[val]] != 0 && vis[a[val]] == cnt){sum = (cur - dis[a[val]] + 1);num += sum;ans = (ans % mod * (POW[sum]-2) % mod) % mod;}else if (!vis[a[val]])dfs(cur+1,a[val]);}int main(){int n;init();scanf("%d",&n);for (int i = 1; i <= n; ++i){scanf("%d",&a[i]);}for (int i = 1; i <= n; ++i){if (!vis[i]){++cnt;dfs(0,i);}}ans = (ans % mod * POW[n-num] % mod) % mod; printf("%I64d\n",ans);return 0;}
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists ofn towns numbered from 1 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 townB 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 every1 ≤ i < k there is a road from townAi to townAi + 1 and another road from townAk to townA1. In other words, the roads are confusing ifsome 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 willnot 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 towni 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 modulo109 + 7.
32 3 1
6
42 1 1 1
8
52 4 2 5 3
28
Consider the first sample case. There are 3 towns and3 roads. The towns are numbered from 1 to 3 and the roads are ,, initially. Number the roads1 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 towns1, 2, 3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of6 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 isnot confusing.
- Codeforces Round #369 (Div. 2) -- D. Directed Roads (DFS找环)
- Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂
- Codeforces Round #369 (Div. 2) 711D Directed Roads (dfs)
- Codeforces Round #369 (Div. 2) D. Directed Roads (dfs+组合数学 图论)
- 【Codeforces Round #369 (Div. 2)】Codeforces 711D Directed Roads
- Codeforces Round #369 (Div. 2) [D] Directed Roads
- Codeforces Round #369 (Div. 2) D. Directed Roads
- Codeforces 711D Directed Roads dfs(找环)
- cf#369-D - Directed Roads-dfs找环
- codeforces 711D. Directed Roads 找环
- codeforces 711D Directed Roads(DFS)
- [Codeforces Round #369 (Div. 2)D. Directed Roads]Tarjan强连通分量+组合计数
- 有向图找环——Directed Roads ( Codeforces Round #369 (Div. 2) D )
- Codeforces Round #369 (Div. 2) D. Directed Roads 图论、组合学、二重dfs、并查集形式的图、Interesting、好题
- 【Codeforces 711 D D. Directed Roads】 + DFS
- Codeforces 711D Directed Roads【Dfs+思维】
- Codeforces Round #369 (Div. 2) D DFS
- Codeforces #369 div2 D.Directed Roads
- android 使用开源库zxing生成二维码,扫描二维码
- Linux 暑期培训课程总结
- eclipse开发配置及优化
- CSS定位position定位问题
- Android之Debug Certificate expired解决方案
- Codeforces Round #369 (Div. 2) -- D. Directed Roads (DFS找环)
- ubuntu环境中操作MySQL,使用C语言API调用MySQL
- 强大的vim配置文件,让编程更随意
- Spring第一个 helloworld 程序
- Revit开发之AddInCommandBinding
- Zookeeper 源代码编译导入Eclipse
- 学习Hadoop第二课(安装JDK)
- PAT 1016. Phone Bills (25)(map排序,去掉不匹配的,分时计算money)(待修改)
- 祖孙询问 纪中3054 LCA