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) ...
随机推荐
- X5webview完美去掉分享功能和缓存功能(2)
前段时间比较忙,没有来得及写完如何将X5WEBVIEW分享功能和缓存功能屏蔽,下面直接来干货,上代码. 1.首先在布局文件中增加一个全屏的布局, <!-- 视频全屏--> <Fram ...
- 大数据中HBase集群搭建与配置
hbase是分布式列式存储数据库,前提条件是需要搭建hadoop集群,需要Zookeeper集群提供znode锁机制,hadoop集群已经搭建,参考 Hadoop集群搭建 ,该文主要介绍Zookeep ...
- 自动化运维工具saltstack05 -- 之salt-ssh模式
salt-ssh模式 1.说明: salt-ssh即通过ssh得方式进行管理,不需要安装salt-minion, salt-ssh 用的是sshpass进行密码交互的. 2.salt-ssh得局限性 ...
- C#多线程的几种使用
参见链接 :http://www.jb51.net/article/46234.htm
- CentOS 7 安装图形化界面及 Xshell 连接
CentOS 比较适合用作服务器的系统,之前用过 CentOS 6,但是在配置 Nginx 的时候,发现很多语句版本7的系统都进行了更新,而且网上针对版本7的例子会更多一下,遂将系统换成版本7. 下载 ...
- php在数组中判断某个值是否存在
php在数组中查找指定值是否存在的方法有很多,记得很久以前我一直都是傻傻的用foreach循环来查找的,下面我主要分享一下用php内置的三个数组函数来查找指定值是否存在于数组中,这三个数组分别是 in ...
- 机器装多个版本php,并安装redis插件报错【已解决】
机器原版本php5.5.3 适应新的框架安装了7.1.12 期间遇到的小问题就是安装 redis插件的时候,总报错,报错如下: Starting php-fpm [02-Jan-2019 10:15: ...
- 2018年第九届蓝桥杯【C++省赛B组】
2标题:明码 汉字的字形存在于字库中,即便在今天,16点阵的字库也仍然使用广泛.16点阵的字库把每个汉字看成是16x16个像素信息.并把这些信息记录在字节中. 一个字节可以存储8位信息,用32个字节就 ...
- 团队博客作业Week5 --- 团队贡献分--分配规则
团队会议 时间:公元2015年10月26日22时3分20秒 地点:宿舍楼716房间 与会人员:陈谋,李剑锋,卢惠民,刘夕霆,仉伯龙,潘成鼎. 会议内容:今天的组会主要讨论的是项目团队贡献分的计算方式, ...
- JAVA开发环境的熟悉
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1352 姓名:马悦 学号:20135235 成绩: 指导教师:娄嘉鹏 实验日期:2015.4.13 实验密级: 预习程度 ...