题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图。

析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如果有环呢?假设x是这个连通块的边数,

y是这个环的边数,那么就一共有2^x * (2 ^ y - 2) 种,减去这两种就是一边也不变,和所有的边都就变,这样就形成环了。

那么怎么计算呢?这个题的边很特殊,所以我们可以利用这个特征,我们在每个连通块时,用vis记录一下开始的父结点,用cnt记录每一个到每个点的深度,

然后如果产生环了,那么我们就可以很轻松的算出这个环的结点数,用当前的cnt减去就好,然后用sum记录一下环结点的总数,

最后用n减去环中的结点数,就剩下不是环的结点数了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <list>
#include <sstream>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int vis[maxn];
int a[maxn], cnt[maxn];
LL ans;
int sum; LL quick_pow(LL a, LL b){
LL ans = 1;
while(b){
if(b & 1) ans = (ans * a) % mod;
b >>= 1;
a = (a * a) % mod;
}
return ans;
} void dfs(int d, int u, int fa){
vis[u] = fa;
cnt[u] = d;
if(!vis[a[u]]) dfs(d+1, a[u], fa);
else if(vis[a[u]] == fa){
sum += cnt[u]-cnt[a[u]]+1;
ans = (ans * (quick_pow(2LL, cnt[u]-cnt[a[u]]+1) - 2 + mod)) % mod;
}
} int main(){
while(scanf("%d", &n) == 1){
memset(vis, 0, sizeof vis);
memset(cnt, 0, sizeof cnt);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]); ans = 1, sum = 0;
for(int i = 1; i <= n; ++i){
if(vis[i]) continue;
dfs(0, i, i);
} ans = (ans * quick_pow(2LL, n-sum)) % mod;
cout << ans << endl;
}
return 0;
}

CodeForces 711D Directed Roads (DFS判环+计数)的更多相关文章

  1. CodeForces 711D Directed Roads (DFS找环+组合数)

    <题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...

  2. Codeforces 711 D. Directed Roads (DFS判环)

    题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...

  3. Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂

    题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...

  4. Codeforces 711D Directed Roads - 组合数学

    ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...

  5. codeforces 711D Directed Roads(DFS)

    题目链接:http://codeforces.com/problemset/problem/711/D 思路:由于每个点出度都为1,所以没有复杂的环中带环.DFS遍历,若为环则有2^k-2种,若为链则 ...

  6. 【图论】Codeforces 711D Directed Roads

    题目链接: http://codeforces.com/problemset/problem/711/D 题目大意: 给一张N个点N条有向边的图,边可以逆向.问任意逆向若干条边使得这张图无环的方案数( ...

  7. CodeForces 711D Directed Roads

    计数,模拟. 首先观察一下给出的图的特点: $1.$一定存在环. $2.$可能存在多个环. 我们对每个环计算方案数,假设环$C$上包含$x$条边,那么把环$C$破坏掉的方案数有${2^x} - 2$种 ...

  8. Atcoder Grand Contest 032C(欧拉回路,DFS判环)

    #include<bits/stdc++.h>using namespace std;int vis[100007];vector<int>v[100007];vector&l ...

  9. cf1278D——树的性质+并查集+线段树/DFS判环

    昨天晚上本来想认真打一场的,,结果陪女朋友去了.. 回来之后看了看D,感觉有点思路,结果一直到现在才做出来 首先对所有线段按左端点排序,然后用并查集判所有边是否联通,即遍历每条边i,和前一条不覆盖它的 ...

随机推荐

  1. 使用C++读写Excel

    1.导入Excel类型库 使用Visual C++的扩展指令#import导入Excel类型库: 1 2 3 4 5 6 7 8 9 10 11 12 #import "C:\\Progra ...

  2. Servlet的延迟加载和预加载

    我们什么时候使用了延迟加载呢? 先从hibernate引入这个概念吧. hibernate使用lazy属性设置延迟加载,load方法会使用延迟加载. 举个例子: 一个学生有多部手机,如果使用了延迟加载 ...

  3. 插件svn简单使用

    首先安装服务器Server 傻瓜式样操作.下一步下一步就这样完成了,选择的是个人版. 然后打开Server的图形化界面:VisualSVN Server Manager Repositories:资源 ...

  4. HiveQL 与 SQL的异同

    1 select 别名 (1)别名一定要加as 例:select ID as stuID from students (2) Hive QL不支持在group by, order by 中使用sele ...

  5. UIColor,CGColor,CIColor三者的区别和联系

    UIColor,CGColor,CIColor三者的区别和联系((转)) 最近看了看CoreGraphics的东西,看到关于CGColor的东西,于是就想着顺便看看UIColor,CIColor,弄清 ...

  6. HTML5中DOM元素的querySelector/querySelectorAll的工作机制

    在HTML5中,提供了强大的DOM元素选择API querySelector/querySelectorAll,允许使用JavaScript代码来完成类似CSS选择器的DOM元素选择功能.通常情况下, ...

  7. webtest 文章

    一直也没有和游戏类测试打过交道,看到“腾讯WeTest ”提供的测试服务,以及和手机游戏相关的技术文章.在此作个备份记录的. 手游专题 http://wetest.qq.com/lab/tag/?ta ...

  8. eclipse quick diff功能

    Eclipse文本编辑器和Java编辑器都提供了quick diff功能.这就使得你可以快速地识别出当前所编辑文件版本和该文件的参考版本之间的不同. 如果编辑器的quick diff功能没有启用,可以 ...

  9. 【LeetCode】101 - Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  10. UDP模式与TCP模式的区别

    1.TCP有连接状态,而UDP没有. 2.TCP应用层使用无需考虑包的大小及发送情况,而UDP需要. 3.TCP中IP包大小的决定者是Socket,而UDP为应用层.