题目链接:http://codeforces.com/problemset/problem/711/D

给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环。

每个连通量必然有一个环,dfs的时候算出连通量中点的个数y,算出连通量的环中点的个数x,所以这个连通量不成环的答案是2^(y - x) * (2^x - 2)。

最后每个连通量的答案相乘即可。

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef __int64 LL;
typedef pair <int, int> P;
const int N = 2e5 + ;
struct Edge {
int next, to;
}edge[N << ];
LL mod = 1e9 + , d[N], ans, f[N], sum;
int head[N], cnt;
bool vis[N]; LL fpow(LL a, LL b) {
LL res = ;
while(b) {
if(b & )
res = res * a % mod;
a = a * a % mod;
b >>= ;
}
return res;
} inline void add(int u, int v) {
edge[cnt].next = head[u];
edge[cnt].to = v;
head[u] = cnt++;
} void dfs(int u, int p, int dep) {
if(!ans && vis[u]) {
ans = dep - d[u] ? dep - d[u] : ;
return ;
} else if(vis[u]) {
return ;
}
d[u] = dep;
vis[u] = true;
++sum;
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == p)
continue;
dfs(v, u, dep + );
}
} int main()
{
f[] = ;
for(int i = ; i < N; ++i) {
f[i] = f[i - ]*2LL % mod; //2的阶乘预处理
}
memset(head, -, sizeof(head));
cnt = ;
int n, u;
scanf("%d", &n);
for(int i = ; i <= n; ++i) {
scanf("%d", &u);
add(u, i);
add(i, u);
}
LL res = ;
for(int i = ; i <= n; ++i) {
if(!vis[i]) {
ans = sum = ;
dfs(i, -, );
res = ((res * (f[ans] - ) % mod + mod) % mod * f[sum - ans]) % mod;
}
}
printf("%lld\n", res);
return ;
}

Codeforces 711 D. Directed Roads (DFS判环)的更多相关文章

  1. CodeForces 711D Directed Roads (DFS判环+计数)

    题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...

  2. 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 ...

  3. codeforces 711D D. Directed Roads(dfs)

    题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

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

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

  5. codeforces 711 D.Directed Roads(tarjan 强连通分量 )

    题目链接:http://codeforces.com/contest/711/problem/D 题目大意:Udayland有一些小镇,小镇和小镇之间连接着路,在某些区域内,如果从小镇Ai开始,找到一 ...

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

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

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

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

  8. Codeforces Round #369 (Div. 2) D. Directed Roads dfs求某个联通块的在环上的点的数量

    D. Directed Roads   ZS the Coder and Chris the Baboon has explored Udayland for quite some time. The ...

  9. CodeForces #369 div2 D Directed Roads DFS

    题目链接:D Directed Roads 题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边.这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方 ...

随机推荐

  1. 浅谈 Scala 中下划线的用途

    Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就 ...

  2. MVC+Ef项目(3) 抽象数据库访问层的统一入口;EF上下文线程内唯一

    抽象一个数据库访问层的统一入口(类似于EF的上下文,拿到上下文,就可以拿到所有的表).实际这个入口就是一个类,类里面有所有的仓储对应的属性.这样,只要拿到这个类的实例,就可以点出所有的仓储,我们在 R ...

  3. 【JavaScript学习笔记】鼠标样式

    style="cursor:hand"   手形 style="cursor:crosshair"   十字形       style="cursor ...

  4. openssl rsa 加解密

    <h4>1.openssl进行rsa加密解密</h4>首先介绍下命令台下openssl工具的简单使用:生成一个密钥:<pre lang="c" esc ...

  5. IconRes提供免费高质量的Material风格android官方图标库

    连接地址: http://www.iconres.com/android/index.php

  6. 【转】 ios开发证书CER文件、P12文件,mobileprovition许可文件的用途

    钥吻合之后.我们一般会给证书做个备份,这个备份就是个P12文件. 这个p12文件很好用,它不仅包含CER的信息,还有私钥信息,即: P12备份文件 = CER文件  + 私钥:所以有了这个p12就再也 ...

  7. 无法启动ArcSDE服务

    ArcSDE服务启动错误:Error (-327), No ArcSDE server license found解决方法:>sdesetup -o update_key -d oracle10 ...

  8. 高新服务平台在SUSE系统上重新部署笔录

    安装步骤     ./shutdown.sh 停止tomcat (1) 删除 zjgx 目录下 除bbs 和Demos所有目录和文件 (2)上传zjgx0519.zip 到zjgx目录下 (3)unz ...

  9. 嵌入式 H264视频通过RTMP直播

    前面的文章中提到了通过RTSP(Real Time Streaming Protocol)的方式来实现视频的直播,但RTSP方式的一个弊端是如果需要支持客户端通过网页来访问,就需要在在页面中嵌入一个A ...

  10. ORACLE变量定义及使用(另,T-SQL EXISTS的PLSQL替代写法)

    1. 简单变量 declare v_cnt NUMBER(10,0) := 0; BEGIN   SELECT COUNT(1) INTO v_cnt FROM concept.Decoction W ...