P4645 [COCI2006-2007 Contest#3] 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".
输入输出样例
6 7
1 3
1 4
3 2
4 2
5 6
6 5
3 4
6 8
1 3
1 4
3 2
4 2
5 6
6 5
3 4
4 3
3
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的更多相关文章
- 洛谷P4645 [COCI2006-2007 Contest#7] BICIKLI [Tarjan,拓扑排序]
题目传送门 BICIKLI 题意翻译 给定一个有向图,n个点,m条边.请问,1号点到2号点有多少条路径?如果有无限多条,输出inf,如果有限,输出答案模10^9的余数. 两点之间可能有重边,需要看成是 ...
- [COCI2006-2007 Contest#3] BICIKLI
不难的一道题,就是码的时候出了点问题,看了其他巨佬的题解才发现问题所在... 题目大意: 给定一个有向图,n个点,m条边.请问,1号点到2号点有多少条路径?如果有无限多条,输出inf,如果有限,输出答 ...
- IOCCC(The International Obfuscated C Code Contest)
国际 C 语言混乱代码大赛(IOCCC, The International Obfuscated C Code Contest)是一项国际编程赛事,从 1984 年开始,每年举办一次(1997年.1 ...
- poi读取excel模板,填充内容并导出,支持导出2007支持公式自动计算
/** * 版权所有(C) 2016 * @author www.xiongge.club * @date 2016-12-7 上午10:03:29 */ package xlsx; /** * @C ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- BZOJ 2007: [Noi2010]海拔
2007: [Noi2010]海拔 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2410 Solved: 1142[Submit][Status] ...
- 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) ...
- 2016 Multi-University Training Contest 2 D. Differencia
Differencia Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 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) ...
随机推荐
- How to: Provide Credentials for the Dashboards Module when Using External Data Sources
XAF中使用dashboard模块时,如果使用了sql数据源,可以使用此方法提供连接信息 https://www.devexpress.com/Support/Center/Question/Deta ...
- flask的jinja2模板中过过滤器的相关小内容
jinja2模板中有自带的过滤器,有需要直接拿来使用.也可以自己定义过滤器 在过滤器中,有一些常见得操作及关键字.有对字符串的操作,还有对大小写转换的操作.还有对list的操作 过滤器的语法 {# 过 ...
- Halcon四 双目视觉的标定
原文作者写的一系列博客,挺不错的学习halcon:http://blog.sina.com.cn/s/blog_442bfe0e0100yjtn.html 1.get_image_pointer1(I ...
- 管理项目中的贴图-Texture overview插件
Texture overview插件管理项目中的贴图 1. Assetstore地址 2. 总览项目中所有贴图 3. 针对不同平台的贴图压缩设置 在插件的右上角 4. 支持多选批量修改 5. 点击表头 ...
- Ubuntu解压zip包中文乱码
解决方法:通过unar 工具解压 步骤一: 安装unar: sudo apt-get install unrar 步骤二: 解压(以test.zip为例):unar test.zip 解压成功,乱码问 ...
- Java字符串分割
java中字符串的分割函数,split("你想要分割的字符", 你想要最多分割为多少段,正整数) 注意事项: 1.分割特殊字符考虑转义字符的使用.如: . \ | 2.第二个参数: ...
- 如何在window服务器上搭建一个能代替ftp的传输工具
通常对于服务器上的文件管理和数据传输都是利用ftp来实现,但随着存储技术的发展,数据资产的存储规模和复杂程度不断提高,传统的ftp传输显得有笨重.今天给大家介绍一款能够取代ftp的在线文档管理软件—— ...
- [奇葩问题] Error Domain=NSURLErrorDomain Code=-1003
问题描述: 新上线的产品,ios同事拿着一串报错来找我,日志如下:err =Error Domain=NSURLErrorDomain Code=-1003 "未能找到使用指定主机名的服务器 ...
- SpringBoot初始教程之Redis集中式Session管理
1.介绍 有关Session的管理方式这里就不再进行讨论,目前无非就是三种单机Session(基于单机内存,无法部署多台机器).基于Cookie(安全性差).基于全局的统一Session管理(redi ...
- python判断文件和文件夹是否存在、没有则创建文件夹
原文出处:https://www.cnblogs.com/hushaojun/p/4533241.html >>> import os >>> os.path.ex ...