题目链接:

  http://codeforces.com/problemset/problem/711/D

题目大意:

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

题目思路:

  【图论】

  因为是N条边所以不会有复杂的环,最多只会有若干组一个环加一条链。

  推算得到,一个大小为k的环对答案的贡献是*(2k-2),而长度为k的链对答案的贡献是2k(链不包括环上的)

  用dfs找出每一组环的大小和链的长度,计算答案即可。

 //
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 200014
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
LL aans;
LL e[N];
int to[N];
int t[N];
bool mark[N];
LL mi(int x,int y)
{
LL sum=;
while(y)
{
if(y&)sum=(sum*x)%mod;
x=(x*x)%mod;y>>=;
}
return sum;
}
void dfs(int u)
{
while(!mark[u])
{
mark[u]=;t[u]=++cas;
u=to[u];
}
if(t[u]<=cass)
aans=(aans*e[cas-cass])%mod;
else
{
aans=(aans*((e[cas-t[u]+]-+mod)%mod))%mod;
aans=(aans*e[t[u]--cass])%mod;
}
return;
}
void init()
{
int i;
e[]=;
for(i=;i<N;i++)e[i]=(e[i-]*)%mod;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
init();
// for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s+1))
while(~scanf("%d",&n))
{
mem(mark,);
for(i=;i<=n;i++)scanf("%d",&to[i]);
aans=;cas=;
for(i=;i<=n;i++)
{
if(mark[i])continue;
cass=cas;
dfs(i);
}
printf("%I64d\n",aans);
}
return ;
}
/*
// //
*/

【图论】Codeforces 711D Directed Roads的更多相关文章

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

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

  2. codeforces 711D Directed Roads(DFS)

    题目链接:http://codeforces.com/problemset/problem/711/D 思路:由于每个点出度都为1,所以没有复杂的环中带环.DFS遍历,若为环则有2^k-2种,若为链则 ...

  3. CodeForces 711D Directed Roads (DFS判环+计数)

    题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...

  4. CodeForces 711D Directed Roads

    计数,模拟. 首先观察一下给出的图的特点: $1.$一定存在环. $2.$可能存在多个环. 我们对每个环计算方案数,假设环$C$上包含$x$条边,那么把环$C$破坏掉的方案数有${2^x} - 2$种 ...

  5. CodeForces 711D Directed Roads (DFS找环+组合数)

    <题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...

  6. Code Forces 711D Directed Roads

    D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. codeforces 711D D. Directed Roads(dfs)

    题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

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

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

  9. Directed Roads CodeForces - 711D (基环外向树 )

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

随机推荐

  1. linux系统时间同步更新

    linux中的时钟分为系统时钟和硬件时钟.硬件时钟通过bioss进行设置,系统时钟通过linux kernel进行设置.当linux启动时,bioss会读取系统时钟的时钟设置.在linux中涉及时间设 ...

  2. Winform 程序中dll程序集嵌入exe可执行文件

    关于这方面,Google一下有很多方法,参考: http://blog.csdn.net/astonqa/article/details/7300856 但按照以上的方法我并没有成功,于是继续找到了一 ...

  3. Java_Activiti5_菜鸟也来学Activiti5工作流_之初识常用服务类和数据表(二)

    /** * 代码清单中使用 ProcessEngines类加载默认的流程配置文件(activiti.cfg.xml),再获取各个服务组件的实例. * RepositoryService主要用于管理流程 ...

  4. CSS美化 input type=file 兼容各个浏览器(转)

    HTML代码: <FORM> <A class=btn_addPic href="javascript:void(0);"><SPAN>< ...

  5. oracle - 创建数据库

    在服务器端的oracle,用户有点差异,当 我通过 'sqlplus / as sysdba' 命令登陆后,并不能创建数据库,sysdba拥有最高的系统权限,登陆后是 sys,以as sysdba登录 ...

  6. Uploadify 笔记分享 -- 2014年10月18日

    最近要做一个项目,有个部分需要用到Uploadify,以前用过,但不是很懂,找了无数遍的中文文档,发现好多都是以前的,都不能用,一时间索性自己写了个笔记,随用随查 <form> <i ...

  7. java把InputStram 转换为String

    public static String readStream(InputStream in) throws Exception{ //定义一个内存输出流 ByteArrayOutputStream ...

  8. Unity 5.x---00使用重力

    Unity 5.x---00使用重力 步骤一: 打开一个工程(导入Unity自带的资源),并创建并配置好必要的GameObject ,如下图: 步骤二: 1.创建一个Cube,使其位于平面上方.    ...

  9. cocos2dx ease 公式

    所有ease计算公式都在CCTweenFunction.cpp里.

  10. 【POJ1151】【扫描线+线段树】Atlantis

    Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...