题目大意:给你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. AAA游戏中雪的实现

    现在越来越多的AAA游戏中都实现了逼真的雪的效果,比如战神4.地平线:黎明时分.荒野大镖客:救赎2.古墓丽影:崛起等都实现了不错的雪地效果.今天我们就来探究一下他们的实现方式.现在主流的实现方式都是通 ...

  2. 【黑客免杀攻防】读书笔记6 - PE文件知识在免杀中的应用

    0x1 PE文件与免杀思路 基于PE文件结构知识的免杀技术主要用于对抗启发式扫描. 通过修改PE文件中的一些关键点来达到欺骗反病毒软件的目的. 修改区段名 1.1 移动PE文件头位置免杀 工具:PeC ...

  3. MISC混杂设备 struct miscdevice /misc_register()/misc_deregister()【转】

    本文转自:http://blog.csdn.net/angle_birds/article/details/8330407 在Linux系统中,存在一类字符设备,他们共享一个主设备号(10),但此设备 ...

  4. Shell脚本中执行sql语句操作mysql的5种方法【转】

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  5. oracle 远程导入导出(本地win)

    导出 exp hongtastock_account/hongtastock_account@192.168.1.22/orcl file=D:\hongta\hongtastock_account. ...

  6. rsa加密解密, 非对称加密

    2016年3月17日 17:21:08 星期四 现在越来越懒了.... 参考: http://www.xuebuyuan.com/1399981.html 左边是加密流程, 右边是解密流程 呃...有 ...

  7. mockito简单教程

    注:本文来源:sdyy321的<mockito简单教程> 官网: http://mockito.org API文档:http://docs.mockito.googlecode.com/h ...

  8. 父窗口中获取iframe中的元素

    js 在父窗口中获取iframe中的元素 1. 格式:window.frames["iframe的name值"].document.getElementById("ifr ...

  9. InnoDB的关键特性-插入缓存,两次写,自适应hash索引

    InnoDB存储引擎的关键特性包括插入缓冲.两次写(double write).自适应哈希索引(adaptive hash index).这些特性为InnoDB存储引擎带来了更好的性能和更高的可靠性. ...

  10. HRBUST - 1818 石子合并 区间dp入门

    有点理解了进阶指南上说的”阶段,状态和决策“ /* 区间dp的基础题: 以区间长度[2,n]为阶段,枚举该长度的区间,状态dp[l][r]表示合并区间[l,r]的最小费用 状态转移方程dp[l][r] ...