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

析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有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. poj2750 线段树 +DP Potted Flower

    问题描述:给定一个环形序列,进行在线操作,每次修改一个元素,输出环上的最大连续子列的和,但不能是完全序列. 算法:把环从一个地方,切断拉成一条直线,用线段树记录当前区间的非空最大子列和当前区间的非空最 ...

  2. 转载]IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本 )

    原文地址:IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本作者:佐佐木小次郎 因为最近项目上要用有关LBS的功能.于是我便做一下预研. 一般说来LBS功能一般分为两块:一块是地理 ...

  3. kthread_stop引起的OOP

    1 使用kthread_create创建线程:     struct task_struct *kthread_create(int (*threadfn)(void *data), void *da ...

  4. js屏弊错误

    <SCRIPT language=javascript> <!-- window.onerror=function(){return true;} // --> </SC ...

  5. over-fitting、under-fitting 与 regularization

    机器学习中一个重要的话题便是模型的泛化能力,泛化能力强的模型才是好模型,对于训练好的模型,若在训练集表现差,不必说在测试集表现同样会很差,这可能是欠拟合导致:若模型在训练集表现非常好,却在测试集上差强 ...

  6. 【解题报告】[动态规划] RQNOJ - PID15 / 采药

    原题地址:http://www.rqnoj.cn/problem/15 好久以前做的题了,是个背包问题,就不解释了. #include<stdio.h> #define MAX 100 i ...

  7. yii2.0 输出url 注册js css文件

    //输出url <a href="<?=  Url::to(['/users/login/login','id'=>5,'mark'=>true]) ?>&qu ...

  8. SQL利用Case When Then多条件判断

    CASE     WHEN 条件1 THEN 结果1     WHEN 条件2 THEN 结果2     WHEN 条件3 THEN 结果3     WHEN 条件4 THEN 结果4 ....... ...

  9. 【模版消息】C#推送微信模版消息(Senparc.Weixin.MP.dll)

    定义的模版内容: {{first.DATA}} 商品名称:{{product.DATA}} 商品价格:{{price.DATA}} 购买时间:{{time.DATA}} {{remark.DATA}} ...

  10. openlayers加载地图没有图片时有红叉的解决方法

    解决方式:设置样式隐藏图片 <style type="text/css"> .olImageLoadError { /*ol2.12 onImageLoadError ...