题目大意:给你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. oozie JAVA Client 编程提交作业

    1,eclipse环境搭建 在eclipse中新建一个JAVA工程,导入必要的依赖包,目前用到的有: 其次编写JAVA 程序提交Oozie作业,这里可参考:oozie官方参考文档 在运行提交程序前,首 ...

  2. linux 命令收集 阿里云nginx升级等 查看磁盘空间 版本等

    —————————————————— 查磁盘 df -h此命令直观的呈现出磁盘大小有多少Gdf -hl文件系统 容量 已用 可用 已用% 挂载点/dev/hdb2 75G 75G 0 100% /就是 ...

  3. 51nod1331 狭窄的通道

    题目传送门 这道题 51nod只Ac了十二个人 没有题解可以研究 所以就自己YY了半天 在这里先感谢一波岚清大爷 orz 然后这道题我分了两种情况 一种是左边的往左跑右边的往右跑 中间有一部分直接走不 ...

  4. OracleHelper与SqlServerHelper

    1.OracleHelper using System; using System.Data; using System.Configuration; using System.Linq; using ...

  5. pandas 读csv文件 TypeError: Empty 'DataFrame': no numeric data to plot

    简单的代码,利用pandas模块读csv数据文件,这里有两种方式,一种是被新版本pandas遗弃的Series.from_csv:另一种就是pandas.read_csv 先说一下问题这个问题就是在读 ...

  6. 第15月第22天 libz.dylib

    1. 3.在弹出的对话框中输入"cmd"+"shift"+"g" 4 4.输入/usr/lib https://jingyan.baidu. ...

  7. Android判断当前是否在主线程

    Android开发中, 有时需要判断当前线程到底是主线程, 还是子线程, 例如: 我们在自定义View时, 想要让View重绘, 需要先判断当前线程到底是不是主线程, 然后根据判断结果来决定到底是调用 ...

  8. 恶意PDF文档分析记录

    0x1 PDF是什么 PDF(便携式文件格式,Portable Document Format)是由Adobe Systems在1993年用於文件交换所发展出的文件格式. 因为PDF的文件格式性质广泛 ...

  9. 定位内网中毒主机IP经历小记

    一.事件起因 客户向公司反映使用IDS设备捕获到木马上线域名需要处理,虽然是逆向岗但还是有预感未来应急响应的工作只会越来越多.所以作为新人的我选择了跟带头BOSS去现场学习,并且将自己参与应急响应中的 ...

  10. 从SDP中至少要看到那些东西?

    最近对通过抓包获得了很多的SDP封包,对这些SDP媒体协商过程很是好奇,为什么不同的sip终端所提供的SDP包不尽相同,为什么同一台FS对不同的sip终端发送不同的SDP? 对我菜鸟级别的我们,我分享 ...