洛谷P4645 [COCI2006-2007 Contest#7] BICIKLI [Tarjan,拓扑排序]
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
3
6 8
1 3
1 4
3 2
4 2
5 6
6 5
3 4
4 3
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,拓扑排序]的更多相关文章
- 洛谷P4581 [BJOI2014]想法(玄学算法,拓扑排序)
洛谷题目传送门 萝卜大毒瘤 题意可以简化成这样:给一个DAG,求每个点能够从多少个入度为\(0\)的点到达(记为\(k\)). 一个随机做法:给每个入度为\(0\)的点随机一个权值,在DAG上求出每个 ...
- 动态规划 洛谷P4017 最大食物链计数——图上动态规划 拓扑排序
洛谷P4017 最大食物链计数 这是洛谷一题普及/提高-的题目,也是我第一次做的一题 图上动态规划/拓扑排序 ,我认为这题是很好的学习拓扑排序的题目. 在这题中,我学到了几个名词,入度,出度,及没有环 ...
- 洛谷P3065 [USACO12DEC]第一!First!(Trie树+拓扑排序)
P3065 [USACO12DEC]第一!First! 题目链接:https://www.luogu.org/problemnew/show/P3065 题目描述 Bessie一直在研究字符串.她发现 ...
- 洛谷 P2046 BZOJ 2007 海拔(NOI2010)
题目描述 YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作 一个正方形,每一个区域也可看作一个正方形.从而,YT城市中包括(n+1)×(n+1)个 ...
- 洛谷P1080 国王游戏 python解法 - 高精 贪心 排序
洛谷的题目实在是裹脚布 还编的像童话 这题要 "使得获得奖赏最多的大臣,所获奖赏尽可能的少." 看了半天都觉得不像人话 总算理解后 简单说题目的意思就是 根据既定的运算规则 如何排 ...
- 【洛谷 P1452】 Beauty Contest (二维凸包,旋转卡壳)
题目链接 旋转卡壳模板题把. 有时间再补总结吧. #include <cstdio> #include <cmath> #include <algorithm> u ...
- 洛谷 P1131 [ ZJOI 2007 ] 时态同步 —— 树形DP
题目:https://www.luogu.org/problemnew/show/P1131 记录 x 子树内同步的时间 f[x],同步所需代价 g[x]: 直接转移即可,让该儿子子树与其它儿子同步, ...
- BZOJ 1634 洛谷2878 USACO 2007.Jan Protecting the flowers护花
[题意] 约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小 ...
- 洛谷 P1177 【模板】快速排序【13种排序模版】
P1177 [模板]快速排序 题目描述 利用快速排序算法将读入的N个数从小到大排序后输出. 快速排序是信息学竞赛的必备算法之一.对于快速排序不是很了解的同学可以自行上网查询相关资料,掌握后独立完成.( ...
随机推荐
- MapReduce (MRV1)设计理念与基本架构
MapReduce 是一个分布式计算框架,主要由两部分组成:编程模型和运行时环境. 其中,编程模型为用户提供了非常易用的编程接口,用户只需要像编写串行程序一样实现几个简单的函数即可实现一个分布式程序, ...
- 微服务深入浅出(7)-- 网关路由Zuul
Zuul用于构建边界服务,致力于动态路由,过滤,监控,弹性伸缩和安全等方向. 1.Zuul+Ribbon+Eureka结合,可以实现智能路由和负载均衡 2.网关将所有服务的API接口统一聚合统一暴露 ...
- 对一道pwnhub的一点点记录
一.通过ssh弱口令,建立socket5代理进内网. 1.修改proxychains配置文件vi /etc/proxychains.conf如下: 2.建立ssh隧道:ssh -qTfnN -D 70 ...
- POJ 3468 A Simple Problem with Integers (区间更新+区间查询)
题目链接 Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operation ...
- 字符加密 cipher
评测传送门 Description: Valentino 函数的定义: 对于一个由数字和小写字母组成的字符串 S,两个整数 K,M,将 S 视为一个 P 进制数,定义: Valentino(S, K, ...
- UVA1386 【Cellular Automaton】题解
题面:UVA1386 Cellular Automaton 矩阵乘法+快速幂解法: 这是一个比较裸的有点复杂需要优化的矩乘快速幂,所以推荐大家先做一下下列洛谷题目练练手: (会了,差不多就是多倍经验题 ...
- Nginx服务优化详解
Nginx服务优化详解 1.隐藏Nginx版本信息 编辑主配置文件nginx.conf,在http标签中添加代码 server_tokens off;来隐藏软件版本号. 2.更改Nginx服务启动的默 ...
- [Openwrt 扩展上篇]USB挂载&U盘启动&Samba共享
最近偷懒,没学习,反想起自己的路由刷了Openwrt,正好闲置了一个硬盘想拿来做个网络硬盘,于是开始了折腾....这里将不谈论如何刷Openwrt,如何ssh,如何添加PPOE,如何添加相对应服务的包 ...
- weblogica domain目录 环境变量 如何启动weblogic server
手工启动weblogic server
- Method for balancing binary search trees
Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...