题目大意:给定一个 N 个点,N 条边的无向图,现给每条边定向,求有多少种定向方式使得定向后的有向图中无环。

题解:显然,这是一个外向树森林,定向后存在环的情况只能发生在基环树中环的位置,环分成顺时针和逆时针两种情况,其他边方向随意。因此,记外向树森林中环的大小为 \(w[i]\),则答案为$$2^{n-\sum w[i]}*\prod (2^{w[i]}-2)$$

代码如下

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=2e5+10;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll sqr(ll x){return x*x;}
inline ll read(){
ll x=0,f=1;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
return f*x;
} vector<P> G[maxn];int tot=1;
int n,m,dep[maxn],vis[maxn];
ll ans=1,p[maxn]; void read_and_parse(){
n=m=read(),p[0]=1;
for(int i=1;i<=n;i++)p[i]=p[i-1]*2%mod;
for(int i=1,to;i<=n;i++)to=read(),G[i].pb(mp(to,++tot)),G[to].pb(mp(i,++tot));
} void dfs(int u,int fe){
vis[u]=1;
for(int i=0;i<G[u].size();i++){
int v=G[u][i].first,e=G[u][i].second;if(fe==(e^1))continue;
if(!vis[v])dep[v]=dep[u]+1,dfs(v,e);
else if(vis[v]==1)m-=dep[u]-dep[v]+1,ans=ans*(p[dep[u]-dep[v]+1]-2)%mod;
}
vis[u]=2;
} void solve(){
for(int i=1;i<=n;i++)if(!vis[i])dfs(i,0);
ans=ans*p[m]%mod;
printf("%lld\n",ans);
} int main(){
read_and_parse();
solve();
return 0;
}

【CF711D】Directed Roads的更多相关文章

  1. 【CF711D】Directed Roads(环,强连通分量)

    题意: 给一张N个点N条有向边的图,边可以逆向.问任意逆向若干条边使得这张图无环的方案数(mod 1e9+7). n<=200000 思路:三个样例给的好 找规律方便很多 易得有N点的环有(2^ ...

  2. 【题解】Paid Roads [SP3953] [Poj3411]

    [题解]Paid Roads [SP3953] [Poj3411] 传送门:\(\text{Paid}\) \(\text{Roads}\) \(\text{[SP3953]}\) \(\text{[ ...

  3. 【网络流】One-Way Roads

    [网络流]One-Way Roads 题目描述 In the country of Via, the cities are connected by roads that can be used in ...

  4. 【34.40%】【codeforces 711D】Directed Roads

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【lightoj-1002】Country Roads(dijkstra变形)

    light1002:传送门 [题目大意] n个点m条边,给一个源点,找出源点到其他点的‘最短路’ 定义:找出每条通路中最大的cost,这些最大的cost中找出一个最小的即为‘最短路’,dijkstra ...

  6. 【HDU1301】Jungle Roads(MST基础题)

    爽爆.史上个人最快MST的记录7分40s..一次A. #include <iostream> #include <cstring> #include <cstdlib&g ...

  7. 【HDU1102】Constructing Roads(MST基础题)

    最小生成树水题.prim一次AC #include <iostream> #include <cstring> #include <cstdlib> #includ ...

  8. 【USACO02FEB】Rebuilding Roads 重建道路 题解(树形DP)

    题目链接 题目大意:问使含有$p$个节点的子树分离至少需要去掉几条边. ------------------ 设$f[i][j]$表示以$i$为根的子树保留$j$个节点所去掉的最少边数. 初始化$f[ ...

  9. 【图论】USACO11JAN Roads and Planes G

    题目内容 洛谷链接 Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到\(T\)个城镇 (\(1 <= T <= 25,000\)),编号为\(1\)到\ ...

随机推荐

  1. webpack教程(五)——图片的加载

    首先安装的依赖 npm install file-loader --save-devnpm install image-webpack-loader --save-devnpm install url ...

  2. SpringBoot笔记--FastJson

    FastJson配置 ObjectId class ObjectIdSerializer : ObjectSerializer { override fun write(serializer: JSO ...

  3. Microsoft Visual Studio 2013安装及试用

    我是在网上下载的vs2013版的安装包,下载的是压缩文件,解压后是2.86GB.安装包下载完成后我们就可以进入安装了. 同时建议最好在互联网连接的情况下安装. 打开下载好的文件,我们要选择.exe可执 ...

  4. synchronized关键字的学习与总结

  5. 虚拟机Linux(centos)系统能ping通主机,主机无法ping通Linux解决方案

    本文引用:https://blog.csdn.net/clean_water/article/details/53023308 三个步骤: 第一步:虚拟机网络连接方式选择Nat 第二步.关闭liunx ...

  6. Vector,ArrayList, LinkedList的区别

    1.Vector.ArrayList都是以类似数组的形式存储在内存中,LinkedList则以链表的形式进行存储. 2.List中的元素有序.允许有重复的元素,Set中的元素无序.不允许有重复元素. ...

  7. Character Encoding Issues for tomcat

    https://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8 https://stackoverflow.com/questions/10936846 ...

  8. node的读写流

    let http = require('http'); http.createServer((req,res)=>{ res.end(); }).listen(,()=>{ console ...

  9. Linux查看版本

    最简单的命令 lsb_release -a 查看机器名 hostname 查看内核版本 uname -r 红帽 centos 查看版本 cat /etc/redhat-release ubuntu 查 ...

  10. super 使用以及原理

    用super也很久了,但是一直没有关注过他的原理.最近开始越来越多关注python更底层的实现和奇技淫巧.看到该方法越发使用得多所以也研究了一波 平时单继承可能是我们遇到最多的情况.无非就是类似情况. ...