题目大意:给你n个点n条边的有向图,你可以任意地反转一条边的方向,也可以一条都不反转,问你有多少种反转的方法

使图中没有环。

思路:我们先把有向边全部变成无向边,每个连通图中肯定有且只有一个环,如果这个连通图里边有n个点,环由m个元素

构成,那么这个连通图的反转方法数为,(2^(n-m)) * (2^m-2),然后将所有连通图的种数乘到一起就好啦。具体求圆环由几

个点组成看代码。

ps:最后算ans的时候忘了加括号,debug了一个小时QAQ。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=*1e5+;
const ll mod=1e9+;
vector<ll> e[N];
ll n,pre[N],S,E,cnt,dfn[N],idext,ans;
ll q_pow(ll a,ll b)
{
ll ans=;
while(b)
{
a%=mod;
if(b&) ans=ans*a%mod;
b>>=; a=a*a%mod;
}
return ans;
}
void dfs(ll v,ll p)
{
pre[v]=p; cnt++;
dfn[v]=++idext;
for(ll i:e[v])
{
if(i==p) continue;
if(pre[i] && S==-)
{
if(dfn[i]<dfn[v]) E=v,S=i;
else E=i,S=v;
}
if(pre[i]==) dfs(i,v);
}
}
int main()
{
scanf("%lld",&n);
for(ll i=;i<=n;i++)
{
ll g; scanf("%lld",&g);
e[g].push_back(i);
e[i].push_back(g);
}
ans=;
for(ll i=;i<=n;i++)
{
cnt=; S=E=-;
if(pre[i]==)
{
dfs(i,-);
ll num=;
while(E!=S)
{
num++;
E=pre[E];
}
ll res=ans;
ans=ans*((q_pow(,cnt-num)*(q_pow(,num)-))%mod)%mod;
}
}
printf("%lld\n",ans);
return ;
}

Codeforces Round #369 (Div. 2)-D Directed Roads的更多相关文章

  1. Codeforces Round #369 (Div. 2) D. Directed Roads 数学

    D. Directed Roads 题目连接: http://www.codeforces.com/contest/711/problem/D Description ZS the Coder and ...

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

  4. Codeforces Round #369 (Div. 2) D. Directed Roads (DFS)

    D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #302 (Div. 2) D - Destroying Roads 图论,最短路

    D - Destroying Roads Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544 ...

  6. Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)

    题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...

  7. Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)

    Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...

  8. Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路

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

  9. Codeforces Round #369 (Div. 2) C. Coloring Trees(简单dp)

    题目:https://codeforces.com/problemset/problem/711/C 题意:给你n,m,k,代表n个数的序列,有m种颜色可以涂,0代表未涂颜色,其他代表已经涂好了,连着 ...

随机推荐

  1. 变量,id()

    >>> a = 1 >>> print id(a) 2870961640 >>> b = a >>> print id(b) 2 ...

  2. Node、PHP、Java 和 Go 服务端 I/O 性能PK

    http://blog.csdn.net/listen2you/article/details/72935679

  3. Android ThreadPool

    在Android开发中,如果我们要执行某个耗时任务,一般都会考虑开启一个线程去处理. 一个线程run方法执行完毕后,才算真正结束:但是,这只是结束,并没有被回收:会一直闲置在那里,等待GC去回收.所以 ...

  4. 基于神经网络的颜色恒常性—Fully Convolutional Color Constancy with Confidence-weighted Pooling

    论文地址: http://openaccess.thecvf.com/content_cvpr_2017/papers/Hu_FC4_Fully_Convolutional_CVPR_2017_pap ...

  5. C++11 多线程编程

    http://blog.csdn.net/column/details/ccia.html?&page=1

  6. 【转】Shell编程基础篇-下

    [转]Shell编程基础篇-下 1.1 条件表达式 1.1.1 文件判断 常用文件测试操作符 常用文件测试操作符 说明 -d文件,d的全拼为directory 文件存在且为目录则为真,即测试表达式成立 ...

  7. Win10 + VS2017 15.5.6 环境下解决 Python 3.6 环境无法刷新DB的问题

    作为宇宙第一IDE,VS2017对Python的支持还算可以,虽然和PyCharm等Python专用IDE相比还有些差距,但是经过后续的更新升级,我相信VS2017将越来越完善.由于本人一直都是使用V ...

  8. MPI 在Windows10 上安装,使用VS2013编译生成可执行程序

    原文地址:http://www.cnblogs.com/leijin0211/p/6851789.html 参考博客: http://www.cnblogs.com/shixiangwan/p/662 ...

  9. redis学习笔记(面试题)

    1. 什么是redis Redis是一个数据库,他和我们传统的oracle数据库差别是它是基于内存的数据库:因为是基于内存,所以效率就高,在某些场景下就可以对我们传统的关系型数据库做一个补充 2. r ...

  10. openwrt git 代码下载地址

    openwrt 各个版本代码下载 trunk:git clone git://github.com/openwrt/openwrt.git 15.05  (Chaos Calmer)git clone ...