题目传送门

BICIKLI

题意翻译

给定一个有向图,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:

3
输入样例#2:

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

inf

说明

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


  分析:

  考试的题,考场上居然没想到用$Tarjan$来处理环,傻逼的打了几个$DFS$,然后开心的$WA$成狗。

  首先不难想到$inf$的情况就是$1$到$2$的任一路径上有环出现,那么我们可以这么处理:先正反向建边,然后分别以$1,2$为起点$DFS$,然后标记那些点是$1$到$2$的路径上的点,再就可以缩点重建图,如果发现某个强联通分量既是路径上的点而且大小又超过$1$那么就是$inf$,否则就跑拓扑排序记录路径数就行了。

  Code:

//It is made by HolseLee on 23rd Oct 2018
//Luogu.org P4645
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define mod (1000000000)
using namespace std; const int N=1e5+;
int n,m,head[N],cnte,siz[N],cnt1,cnt2,dg[N];
int dfn[N],low[N],scc[N],idx,tot,sta[N],top,ans[N];
int h1[N],to1[N],nxt1[N],h2[N],to2[N],nxt2[N];
bool vis1[N],vis2[N],ins[N];
struct Edge {
int u,v,to,nxt;
}e[N],edge[N];
queue<int>q; inline int read()
{
char ch=getchar(); int num=; bool flag=false;
while( ch<'' || ch>'' ) {
if( ch=='-' ) flag=true; ch=getchar();
}
while( ch>='' && ch<='' ) {
num=num*+ch-''; ch=getchar();
}
return flag ? -num : num;
} inline void add(int x,int y)
{
to1[++cnt1]=y, nxt1[cnt1]=h1[x], h1[x]=cnt1;
to2[++cnt2]=x, nxt2[cnt2]=h2[y], h2[y]=cnt2;
} inline void add_edge(int x,int y)
{
e[++cnte].to=y;
e[cnte].nxt=head[x];
head[x]=cnte;
} void dfs(int x,bool *vis,int *h,int *to,int *nxt)
{
vis[x]=;
for(int i=h[x],y; i; i=nxt[i]) {
y=to[i];
if( !vis[y] ) dfs(y,vis,h,to,nxt);
}
} void tarjan(int x)
{
dfn[x]=low[x]=++idx; ins[x]=;
sta[++top]=x; int y;
for(int i=head[x]; i; i=e[i].nxt) {
y=e[i].to;
if( !dfn[y] ) {
tarjan(y);
low[x]=min(low[x],low[y]);
} else if( ins[y] ) {
low[x]=min(low[x],dfn[y]);
}
}
if( dfn[x]==low[x] ) {
tot++;
do{
y=sta[top--]; ins[y]=;
scc[y]=tot; siz[tot]++;
}while(y!=x);
}
} int main()
{
freopen("bicikli.in","r",stdin);
freopen("bicikli.out","w",stdout);
n=read(), m=read();
for(int i=; i<=m; ++i)
edge[i].u=read(), edge[i].v=read(), add(edge[i].u,edge[i].v);
dfs(,vis1,h1,to1,nxt1); dfs(,vis2,h2,to2,nxt2);
if( !vis1[] ) puts(""), exit();
for(int i=; i<=m; ++i) {
if( vis1[edge[i].u] && vis1[edge[i].v] )
add_edge(edge[i].u,edge[i].v), dg[edge[i].v]++;
}
for(int i=; i<=n; ++i)
if( !dfn[i] && vis1[i] ) tarjan(i);
if( scc[]==scc[] ) puts("inf"), exit();
for(int i=; i<=n; ++i)
if( siz[scc[i]]> && vis1[i] && vis2[i] ) puts("inf"), exit();
q.push();ans[]=;
int x,y;
while( !q.empty() ) {
x=q.front(); q.pop();
for(int i=head[x]; i; i=e[i].nxt) {
y=e[i].to;
ans[y]=(ans[y]+ans[x])%mod;
if( !(--dg[y]) ) q.push(y);
}
}
printf("%d\n",ans[]);
return ;
}

洛谷P4645 [COCI2006-2007 Contest#7] BICIKLI [Tarjan,拓扑排序]的更多相关文章

  1. 洛谷P4581 [BJOI2014]想法(玄学算法,拓扑排序)

    洛谷题目传送门 萝卜大毒瘤 题意可以简化成这样:给一个DAG,求每个点能够从多少个入度为\(0\)的点到达(记为\(k\)). 一个随机做法:给每个入度为\(0\)的点随机一个权值,在DAG上求出每个 ...

  2. 动态规划 洛谷P4017 最大食物链计数——图上动态规划 拓扑排序

    洛谷P4017 最大食物链计数 这是洛谷一题普及/提高-的题目,也是我第一次做的一题 图上动态规划/拓扑排序 ,我认为这题是很好的学习拓扑排序的题目. 在这题中,我学到了几个名词,入度,出度,及没有环 ...

  3. 洛谷P3065 [USACO12DEC]第一!First!(Trie树+拓扑排序)

    P3065 [USACO12DEC]第一!First! 题目链接:https://www.luogu.org/problemnew/show/P3065 题目描述 Bessie一直在研究字符串.她发现 ...

  4. 洛谷 P2046 BZOJ 2007 海拔(NOI2010)

    题目描述 YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作 一个正方形,每一个区域也可看作一个正方形.从而,YT城市中包括(n+1)×(n+1)个 ...

  5. 洛谷P1080 国王游戏 python解法 - 高精 贪心 排序

    洛谷的题目实在是裹脚布 还编的像童话 这题要 "使得获得奖赏最多的大臣,所获奖赏尽可能的少." 看了半天都觉得不像人话 总算理解后 简单说题目的意思就是 根据既定的运算规则 如何排 ...

  6. 【洛谷 P1452】 Beauty Contest (二维凸包,旋转卡壳)

    题目链接 旋转卡壳模板题把. 有时间再补总结吧. #include <cstdio> #include <cmath> #include <algorithm> u ...

  7. 洛谷 P1131 [ ZJOI 2007 ] 时态同步 —— 树形DP

    题目:https://www.luogu.org/problemnew/show/P1131 记录 x 子树内同步的时间 f[x],同步所需代价 g[x]: 直接转移即可,让该儿子子树与其它儿子同步, ...

  8. BZOJ 1634 洛谷2878 USACO 2007.Jan Protecting the flowers护花

    [题意] 约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小 ...

  9. 洛谷 P1177 【模板】快速排序【13种排序模版】

    P1177 [模板]快速排序 题目描述 利用快速排序算法将读入的N个数从小到大排序后输出. 快速排序是信息学竞赛的必备算法之一.对于快速排序不是很了解的同学可以自行上网查询相关资料,掌握后独立完成.( ...

随机推荐

  1. java抽象类和普通类的区别

    1.抽象类不能被实例化. 2.抽象类可以有构造函数,被继承时子类必须继承父类一个构造方法,抽象方法不能被声明为静态. 3.抽象方法只需申明,而无需实现,抽象类中可以允许普通方法有主体 4.含有抽象方法 ...

  2. select 的字段为空,给他显示默认值

    select 的字段为空,给他显示默认值: 解决办法一: select id,name,(case when level is null then 0 else level end) as a fro ...

  3. HBase基本概念

    HBase是什么 HBase构建在 HDFS 之上的分布式列式键值存储系统.HBase内部管理的文件全部存储在HDFS中. HBase VS HDFS HDFS适合批处理场景 不支持数据随机查找 不适 ...

  4. Java并发编程原理与实战二十四:简易数据库连接池

    public class MyDataSource { private static LinkedList<Connection> pool = new LinkedList<> ...

  5. 凸优化(Convex Optimization)浅析

    本博客已经迁往http://www.kemaswill.com/, 博客园这边也会继续更新, 欢迎关注~ 在机器学习中, 很多情况下我们都需要求得一个 问题的全局最优值(global optimum) ...

  6. Print Numbers by Recursion

    Print numbers from 1 to the largest number with N digits by recursion. Notice It's pretty easy to do ...

  7. 金蝶K3,名称或代码在系统中已被使用,由于数据移动,未能继续以NOLOCK方式扫描

    使用金蝶K3时出现:名称或代码在系统中已被使用:错误代码:3604(E14H)source:Microsoft OLE DB provider for SQL SERVERDetail:由于数据移动, ...

  8. 利用Mysql5.7的新特性实现多机房高可用架构【转】

    再牛逼的架构也敌不过挖掘机,无论单机房内你的架构多么的高可用,多么的完善,当挖掘机挖下去那一瞬间,都是扯蛋,楼主所在的公司也被挖掘机挖断过光纤.电力线. 为什么大家都在谈论服务冗余,缓存击穿等高可用时 ...

  9. Gitlab权限管理

    使用管理员登陆gitlab(版本为8.9)创建一个组 给用户授权 创建新用户 再创建两个dev1和dev2 然后再到项目界面授权给pm授权master 创建库(事先先建一个java组) 设置权限 创建 ...

  10. RestTemplate中文乱码问题(spring-web3.0.7版本)

    从网上找的方法: 方法一: //resttemplate乱码问题 //3.1.X以上版本使用 // restTemplate.getMessageConverters().add(0, StringH ...