题意:

  给一张N个点N条有向边的图,边可以逆向。问任意逆向若干条边使得这张图无环的方案数(mod 1e9+7)。

n<=200000

思路:三个样例给的好 找规律方便很多

易得有N点的环有(2^n)-2中改法,除了不改和都改

剩下的都是链,设除环外还有K个点,他们的总贡献就是2^k,因为都是一条边相连接怎么改也没有环

CF上快速幂要写在外面不然会出现奇奇怪怪的CE

 const mo=;
var head,vet,next,stack,low,dfn,b,s,flag:array[..]of longint;
n,tot,i,id,time,top,x,m:longint;
ans,f,tmp:int64; procedure add(a,b:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
head[a]:=tot;
end; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; procedure dfs(u:longint);
var e,v:longint;
begin
flag[u]:=;
inc(top); stack[top]:=u;
inc(time); dfn[u]:=time; low[u]:=time;
e:=head[u];
while e<> do
begin
v:=vet[e];
if flag[v]= then
begin
dfs(v);
low[u]:=min(low[u],low[v]);
end
else if s[v]= then low[u]:=min(low[u],low[v]);
e:=next[e];
end;
if dfn[u]=low[u] then
begin
inc(id); s[u]:=id; inc(b[id]);
while (top>)and(stack[top]<>u) do
begin
s[stack[top]]:=id;
inc(b[id]);
stack[top]:=;
dec(top);
end;
stack[top]:=; dec(top);
end;
end; begin readln(n);
for i:= to n do
begin
read(x);
add(i,x);
end;
for i:= to n do
if flag[i]= then dfs(i);
m:=;
for i:= to n do
if b[s[i]]= then m:=m+;
ans:=;
for i:= to id do
if b[i]> then
begin
f:=; tmp:=;
while b[i]> do
begin
if b[i] mod = then f:=f*tmp mod mo;
tmp:=tmp*tmp mod mo;
b[i]:=b[i] div ;
end;
ans:=(ans*(f-)) mod mo;
end;
ans:=(ans+mo) mod mo;
f:=; tmp:=;
while m> do
begin
if m mod = then f:=f*tmp mod mo;
tmp:=tmp*tmp mod mo;
m:=m div ;
end;
ans:=ans*f mod mo;
writeln(ans); end.

【CF711D】Directed Roads(环,强连通分量)的更多相关文章

  1. cf711D. Directed Roads(环)

    题意 题目链接 \(n\)个点\(n\)条边的图,有多少种方法给边定向后没有环 Sol 一开始傻了,以为只有一个环...实际上N个点N条边还可能是基环树森林.. 做法挺显然的:找出所有的环,设第\(i ...

  2. codeforces 711 D.Directed Roads(tarjan 强连通分量 )

    题目链接:http://codeforces.com/contest/711/problem/D 题目大意:Udayland有一些小镇,小镇和小镇之间连接着路,在某些区域内,如果从小镇Ai开始,找到一 ...

  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. HDU 3594 Cactus (强连通分量 + 一个边只能在一个环里)

    题意:判断题目中给出的图是否符合两个条件.1 这图只有一个强连通分量 2 一条边只能出现在一个环里. 思路:条件1的满足只需要tarjan算法正常求强连通分量即可,关键是第二个条件,我们把对边的判断转 ...

  5. 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 ...

  6. Codeforces 711D Directed Roads - 组合数学

    ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...

  7. Kosaraju算法解析: 求解图的强连通分量

    Kosaraju算法解析: 求解图的强连通分量 欢迎探讨,如有错误敬请指正 如需转载,请注明出处 http://www.cnblogs.com/nullzx/ 1. 定义 连通分量:在无向图中,即为连 ...

  8. 强连通分量&hdu_1269&Codeforce 369D

    强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...

  9. 【Luogu P3387】缩点模板(强连通分量Tarjan&拓扑排序)

    Luogu P3387 强连通分量的定义如下: 有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶 ...

  10. Tarjan算法--强连通分量

    tarjan的过程就是dfs过程. 图一般能画成树,树的边有三种类型,树枝边 + 横叉边(两点没有父子关系) + 后向边(两点之间有父子关系): 可以看到只有后向边能构成环,即只有第三张图是强连通分量 ...

随机推荐

  1. graph-SCC

    strongly connected component(SCC): 里面的任一对顶点都是互相可达的. 一个有向图,将每个SCC缩成一个点,那么这个图就变成了DAG(有向无环图). 原图进行DFS之后 ...

  2. LeetCode(215) Kth Largest Element in an Array

    题目 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...

  3. LeetCode(166) Fraction to Recurring Decimal

    题目 Given two integers representing the numerator and denominator of a fraction, return the fraction ...

  4. cento命令之which、whereis、locate、find

    [which] 查看可执行文件的位置 语法: [root@localhost ~]# which 可执行文件名称 例如: [root@localhost ~]# which passwd /usr/b ...

  5. Linux学习-核心的编译与安装

    编译核心与核心模块 核心与核心模块需要先编译起来,而编译的过程其实非常简单,你可以先使用『 make help 』去查 阅一下所有可用编译参数, 就会知道有底下这些基本功能: [root@study ...

  6. 15年多校第一场七题hdu5294

    要做这题,先要明白图的割,说白了就是 为了让原点无法到汇点要删几条边(之所以叫割,就是在图面上切一刀,减掉最小的边是原点和汇点成为两个集合),想到了割先放着一会用. 题中说只有沿最短路走才有可能追上, ...

  7. winServer08上安装SQL时提示“必须使用管理角色安装”或配置microsoft.net framework 3.5

    server 2008安装vs2008后报错,如图: 解决方法: 控制面板—>程序—>打开或关闭Windows功能—>进入服务器管理器选择功能—>添加功能 然后勾选.NET F ...

  8. [文章备份]Powershell Studio 2019 5.6.160 X64破解版 Crack

  9. Jenkins自动化搭建测试环境(一)

    Jenkins基础 首先上官网jenkins.io上下载最新的Jenkins war包 将下载完成的war包解压 java -jar jenkins.war 接下来使用浏览器访问localhost:8 ...

  10. A+B问题的异常解法

    先%XZZ为敬 http://www.cnblogs.com/xzz_233/p/a-plus-b-problem.html 万恶之源:https://www.luogu.org/discuss/sh ...