题意翻译

给定一个有向图,n个点,m条边。请问,1号点到2号点有多少条路径?如果有无限多条,输出inf,如果有限,输出答案模10^9的余数。

两点之间可能有重边,需要看成是不同的路径。

题目描述

A bicycle race is being organized in a land far, far away. There are N town in the land, numbered 1 through N. There are also M one-way roads between the towns. The race will start in town 1 and end in town 2. How many different ways can the route be set? Two routes are considered different if they do not use the exact same roads.

输入输出格式

输入格式:

The first line of input contains two integers N and M (1 ≤ N ≤ 10 000, 1 ≤ M ≤ 100 000), the number of towns and roads.
Each of the next M lines contains two different integers A and B, representing a road between towns A and B.
Towns may be connected by more than one road.

输出格式:

Output the number of distinct routes that can be set on a
single line. If that number has more than nine digits, output only the
last nine digits of the number. If there are infinitely many routes,
output "inf".

输入输出样例

输入样例#1:

6 7
1 3
1 4
3 2
4 2
5 6
6 5
3 4
输出样例#1:

6 8
1 3
1 4
3 2
4 2
5 6
6 5
3 4
4 3
输入样例#2:

3
输出样例#2:

inf

说明

本题数据已经被更改,无需保留前导0

Solution:

  本题Tarjan缩点+拓扑序dp。

  考虑结果为inf的情况,只要1经过一个环再到2就说明有无数条路径。

  于是我们建两幅图,一正一反,分别处理出1能到达的所有点和能到达2的所有点,然后再对原图缩点(小优化是只需要对1能访问的点缩点就好了),枚举每个点作为中间点,判断1能否经过该点到达2且该点在一个环上,若满足条件说明解为inf咯。

  判完inf的情况,说明剩下的图是个DAG了,直接拓扑序dp就好了(注意模数是$1e9$,开始下意识当作$1e9+7$,然后WA了,咕咕咕~)。

代码:

/*Code by 520 -- 9.5*/
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=,mod=1e9;
int n,m,rd[N],bl[N],scc,low[N],dfn[N],siz[N],tot;
int to[N],net[N],h[N],cnt,stk[N],top,ans[N];
int To[N],Net[N],H[N];
bool vis1[N],vis2[N],ins[N];
struct node{
int u,v;
}e[N];
int gi(){
int a=;char x=getchar();
while(x<''||x>'')x=getchar();
while(x>=''&&x<='')a=(a<<)+(a<<)+(x^),x=getchar();
return a;
} il void add(int u,int v){
to[++cnt]=v,net[cnt]=h[u],h[u]=cnt;
To[++cnt]=u,Net[cnt]=H[v],H[v]=cnt;
} void dfs(int u,bool *vis,int *h,int *to){
vis[u]=;
for(RE int i=h[u];i;i=net[i])
if(!vis[to[i]]) dfs(to[i],vis,h,to);
} void tarjan(int u){
dfn[u]=low[u]=++tot,stk[++top]=u,ins[u]=;
for(RE int i=h[u];i;i=net[i])
if(!dfn[to[i]]) tarjan(to[i]),low[u]=min(low[u],low[to[i]]);
else if(ins[to[i]]) low[u]=min(low[u],dfn[to[i]]);
if(dfn[u]==low[u]){
++scc;
while(stk[top+]!=u)
bl[stk[top]]=scc,ins[stk[top--]]=,siz[scc]++;
}
} queue<int>q;
int main(){
n=gi(),m=gi();
For(i,,m) e[i].u=gi(),e[i].v=gi(),add(e[i].u,e[i].v);
dfs(,vis1,h,to),dfs(,vis2,H,To);
if(!vis1[]) cout<<,exit();
memset(h,,sizeof(h)),cnt=;
For(i,,m) if(vis1[e[i].u]&&vis1[e[i].v]) add(e[i].u,e[i].v),rd[e[i].v]++;
For(i,,n) if(!dfn[i]&&vis1[i]) tarjan(i);
if(bl[]==bl[]) cout<<"inf",exit();
For(i,,n) if(vis1[i]&&vis2[i]&&siz[bl[i]]>) cout<<"inf",exit();
q.push(),ans[]=;
while(!q.empty()){
RE int u=q.front();q.pop();
for(RE int i=h[u];i;i=net[i]){
ans[to[i]]=(ans[to[i]]+ans[u])%mod;
if(!--rd[to[i]])q.push(to[i]);
}
}
cout<<ans[];
return ;
}

P4645 [COCI2006-2007 Contest#3] BICIKLI的更多相关文章

  1. 洛谷P4645 [COCI2006-2007 Contest#7] BICIKLI [Tarjan,拓扑排序]

    题目传送门 BICIKLI 题意翻译 给定一个有向图,n个点,m条边.请问,1号点到2号点有多少条路径?如果有无限多条,输出inf,如果有限,输出答案模10^9的余数. 两点之间可能有重边,需要看成是 ...

  2. [COCI2006-2007 Contest#3] BICIKLI

    不难的一道题,就是码的时候出了点问题,看了其他巨佬的题解才发现问题所在... 题目大意: 给定一个有向图,n个点,m条边.请问,1号点到2号点有多少条路径?如果有无限多条,输出inf,如果有限,输出答 ...

  3. IOCCC(The International Obfuscated C Code Contest)

    国际 C 语言混乱代码大赛(IOCCC, The International Obfuscated C Code Contest)是一项国际编程赛事,从 1984 年开始,每年举办一次(1997年.1 ...

  4. poi读取excel模板,填充内容并导出,支持导出2007支持公式自动计算

    /** * 版权所有(C) 2016 * @author www.xiongge.club * @date 2016-12-7 上午10:03:29 */ package xlsx; /** * @C ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. BZOJ 2007: [Noi2010]海拔

    2007: [Noi2010]海拔 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 2410  Solved: 1142[Submit][Status] ...

  7. hdu 4946 2014 Multi-University Training Contest 8

    Area of Mushroom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. 2016 Multi-University Training Contest 2 D. Differencia

    Differencia Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  9. 2016 Multi-University Training Contest 1 G. Rigid Frameworks

    Rigid Frameworks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. Docker数据卷容器

    用户需要在多个容器之间共享一些数据,就可以使用数据卷容器   从阿里云仓库下载镜像(也可以自己制作一个基础进项,比如只有几M的alpine) sudo docker pull registry.cn- ...

  2. javaweb学习3——验证码

    声明:本文只是自学过程中,记录自己不会的知识点的摘要,如果想详细学习JavaWeb,请到孤傲苍狼博客学习,JavaWeb学习点此跳转 本文链接:https://www.cnblogs.com/xdp- ...

  3. JAVA的关键特性

    Java团队对设计Java时的关键考虑因素进行了总结,关键特性包含以下列表: 简单性 安全性 可移植性 面向对象 健壮性 多线程 体系结构中立 解释执行 高性能 分布式 动态性 简单性 Java的设计 ...

  4. 在eclipse中修改项目发布tomcat的路径名

    第一种.右键点击项目,选中Properties 第二种.双击tomcat 保存 第三种.修改项目目录下的  .setting目录下的

  5. Docker 入门之docker容器创建

    使用docker容器的大多数人都是因为想要隔离不同运行环境的差异,使得自己的应用能更好的移植和部署.那么我们来看看掌握docker需要掌握哪些方面. 1,搭建docker环境 2,编译镜像并将其运行成 ...

  6. Docker 私有仓库方案比较与搭建

    我们知道docker镜像可以托管到dockerhub中,跟代码库托管到github是一个道理.但如果我们不想把docker镜像公开放到dockerhub中,只想在部门或团队内部共享docker镜像,能 ...

  7. gulp: Did you forget to signal async completion? 解决方案

    背景 学习gulp的前端自动化构建,按照示例代码,跑了一个简单的task,控制台打出如下提示: The following tasks did not complete: testGulp Did y ...

  8. string类型和int类型之间的转换

    一.string转int 1. 使用string流 /* 字符串转整型 */ /* * istringstream:从 string 读取数据 * ostringstream:向 string 写入数 ...

  9. Daily Scrum (2015/11/7)

    今晚谢金洛同学的UI工作完成,我们进行了UI和后端的拼接,准备开始规范化地进行系统测试. 成员 今日任务及成果 时间 明日任务 符美潇 1.把之前PM分配的编码任务及其说明准备好发给PM 1h 待定 ...

  10. 面向对象OO第5-7次作业总结

    面向对象OO第5-7次作业总结 学习OO七周了,深切的感受到了这门课程的不友好.前三次作业能够算是勉强地通过了,但是从第五次作业开始就完全GG了.这三次作业,从多线程电梯开始,然后文件监控,然后到出租 ...