Codeforces 711 D. Directed Roads (DFS判环)
题目链接: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判环)的更多相关文章
- CodeForces 711D Directed Roads (DFS判环+计数)
题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...
- 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 ...
- codeforces 711D D. Directed Roads(dfs)
题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 711D Directed Roads (DFS找环+组合数)
<题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...
- codeforces 711 D.Directed Roads(tarjan 强连通分量 )
题目链接:http://codeforces.com/contest/711/problem/D 题目大意:Udayland有一些小镇,小镇和小镇之间连接着路,在某些区域内,如果从小镇Ai开始,找到一 ...
- Atcoder Grand Contest 032C(欧拉回路,DFS判环)
#include<bits/stdc++.h>using namespace std;int vis[100007];vector<int>v[100007];vector&l ...
- cf1278D——树的性质+并查集+线段树/DFS判环
昨天晚上本来想认真打一场的,,结果陪女朋友去了.. 回来之后看了看D,感觉有点思路,结果一直到现在才做出来 首先对所有线段按左端点排序,然后用并查集判所有边是否联通,即遍历每条边i,和前一条不覆盖它的 ...
- 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 ...
- CodeForces #369 div2 D Directed Roads DFS
题目链接:D Directed Roads 题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边.这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方 ...
随机推荐
- [转]使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 事件详解
在前文<使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 默认配置与事件基础>中,Kayo 对 jQuery Mobile 事件的基 ...
- WWDC 2015 - 概记
WWDC 2015已经过去快一个月了,今年似乎没有像去年那样变化巨大,一切都在慢慢演进,iOS.Mac OS.watchOS都变得越来越好. 新的三大平台的发布,iOS 9/Mac OS EL Cap ...
- mysql装载本地文件及模式匹配
使用load data装载本地文件到表中,文件每行一条记录,列值之间用tab分隔,并按照次序一一列出,对于无值或丢失的情况可以使用\N.但是在使用insert into插入的时候不能使用\N,而应该是 ...
- scala学习笔记(9):Scala函数(2)
1 指令式编程&函数式编程 指令式:imperative 风格编程.指令式风格,是你常常使用像 Java,C++和 C 这些语言里用的风格,一次性发出一个指令式的命令,用循环去枚举,并经常改变 ...
- scala学习笔记(4):占位符
scala 中占位符的用法 1.作为“通配符”,类似Java中的*.如import scala.math._ 2.:_*作为一个整体,告诉编译器你希望将某个参数当作参数序列处理!例如val s = s ...
- 【英语】Bingo口语笔记(24) - L的发音技巧
舌头往上跑
- Dataguard常用命令汇总
----标准DataGuard参数设置------------------------------alter system set log_archive_dest_2='SERVICE=ta_std ...
- C# Math.Round中国式的四舍五入法
double dou = 1.255; //这种是错误的 double dou_result = Math.Round(dou, 2); //结果: 1.25 dou_result = Math.Ro ...
- c# 读取其他程序的ListView内容
ArcMap没找到一个导出图层字段结构的功能,自已花点时间用C#做了个小工具,专门用来导arcmap中图层属性面板中的字段信息. 使用说明: 1) 点击“查找窗口”按钮.2) 在ListView控件上 ...
- 对JAVA集合进行遍历删除时务必要用迭代器
java集合遍历删除的方法: 1.当然这种情况也是容易解决,实现方式就是讲遍历与移除操作分离,即在遍历的过程中,将需要移除的数据存放在另外一个集合当中,遍历结束之后,统一移除. 2.使用Iterato ...