题目大意:给定一个 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. 程序员眼中的Redis

    Redis 是用C语言编写的内存中的数据结构存储系统,可以用来作数据库.缓存.消息中间件. 数据结构 字符串(strings):值是任何种类的字符串 散列(hashs):值是map 字典,数组+链表, ...

  2. Centos7系统下修改主机名操作笔记

    习惯了在Centos6系统下修改主机名的操作,但是Centos7下修改主机名的操作却大不相同!操作笔记如下: 在CentOS中,有三种定义的主机名:静态的(static),瞬态的(transient) ...

  3. C_数据结构_走迷宫

    #include <stdio.h> #include <conio.h> #include <windows.h> #include <time.h> ...

  4. 如何在css中设置按钮button中包含图片文字对齐方式

    <el-button class="class-management style="line-heught">班级管理

  5. Kruskal算法-HDU1863畅通工程

    链接 [http://acm.hdu.edu.cn/showproblem.php?pid=1863] 题意 Problem Description 省政府"畅通工程"的目标是使全 ...

  6. alpa开发阶段团队贡献分

    这是我们团队之前决定的分配方式: 1.凡是认真完成自己任务的队员,都将有基础分30分(态度分). 2. 将整个项目细化为不同的任务,列出一个任务清单,在综合.协调完每名成员的意愿后,我会分配清单中的任 ...

  7. 读书笔记(chapter17)

    设备类型:在所有Unix系统中为了统一普通设备的操作所采用的分类 模块:Linux内核中用于按需加载和卸载目标码的机制 内核对象:内核数据结构中支持面对对象的简单操作,还支持维护对象之间的父子关系 1 ...

  8. 《Multiplayer Game Programming》阅读笔记

    在图书馆发现一本<网络多人游戏架构与编程>-- Joshua Glazer, Sanjay Madhav 著.书挺新的,17年出版的,内容很有趣,翻一翻可以学到不少在<计算机网络&g ...

  9. Ehcache配置参数示例

    从Ehcache的jar包里抽取的 <!-- ~ Licensed to the Apache Software Foundation (ASF) under one ~ or more con ...

  10. 原生js作用域(红宝书)

    function fn(){ ; alert(a); // 2; } alert(a);//未被定义: alert(b);//全局变量:b=2: ; function fn1(){ ; functio ...