题目大意:给你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. webstorm常用功能FTP,及常用快捷键

    常用的FTP功能截图如下: 打开配置窗口 常用配置选项,填好FTP登录信息后点 Test FTP 测试一下,(测试之前,这里选“被动模式”) 默认选哪个FTP上传 注意这里的路径是开发后的上传和访问路 ...

  2. String类型的方法总结

    String :字符串对象的包装类型 var stringObject = new String("wanglehui"); 方法总结: 1.返回该对象表示的基本字符串值(也就是返 ...

  3. Nginx URL匹配

      Nginx 下 location模块  可以实现对网页URL进行分析处理 location ~ .*\.(gif|jpg|jpeg|png|bmg|swf)$ { // 扩展名为gif|jpg|j ...

  4. 停靠窗口QDockWidget

    停靠窗口QDockWidget要和QMainWindow一起搭配使用的 样式: import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets ...

  5. luogu P4360 [CEOI2004]锯木厂选址

    斜率优化dp板子题[迫真] 这里从下往上标记\(1-n\)号点 记\(a_i\)表示前缀\(i\)里面树木的总重量,\(l_i\)表示\(i\)到最下面的距离,\(s_i\)表示\(1\)到\(i-1 ...

  6. cetus系列~安装和基本配置

    cetus系列一 安装   1 安装软件环境   yum install cmake gcc glib2-devel flex mysql-devel gperftools-libs  bison f ...

  7. Kali2.0第一节

    kali2.0很好的做了工具缩减,以及图形处理,重要的是msf更快了!汉化也远远超过了1.0 如果想要汉化,选择设置里面的Region&language  将language选择设置为中文,注 ...

  8. 简述JavaScript全局对象

    全局对象是JavaScript中非常重要的一类对象,它作为程序顶层(程序最顶端,不包括在任何函数之内)的上下文存在,JavaScript中的全局属性.全局函数都是通过全局对象来提供给程序的,比如 全局 ...

  9. JavaScript内置对象——Math对象

    这几天在刷leetcode的时候用到了一些Math对象的知识,故作一下总结~ JavaScript中的Math对象也是一个常见的内置对象,然而与String等其它常见对象不同,Math对象没有构造函数 ...

  10. C++ 字符串的编码

    转载链接:https://www.cnblogs.com/akb48/p/5439154.html windows平台 char 表示单字符,占用一个字节 wchar_t 表示宽字符,占用两个字节 L ...