2015 Multi-University Training Contest 8 hdu 5385 The path
The path
This problem will be judged on HDU. Original ID: 5385
64-bit integer IO format: %I64d Java class name: Main
The length of one edge must ∈ [1,n]
It's guaranteed that there exists solution.
Input
The first line contains two integers n and m,the number of vertexs and the number of edges.Next m lines contain two integers each, ui and vi (1≤ui,vi≤n), indicating there is a link between nodes ui and vi and the direction is from ui to vi.
∑n≤3∗105,∑m≤6∗105
1≤n,m≤105
Output
Sample Input
2
4 6
1 2
2 4
1 3
1 2
2 2
2 3
4 6
1 2
2 3
1 4
2 1
2 1
2 1
Sample Output
1
2
2
1
4
4
1
1
3
4
4
4
Source
左边从2开始,右边从n开始,每次选与之前标记过的点相连的未标记过得点,该点的d[i]为该点加入的时间。最后输出时,判断该点是否在最短路上,不在的话,输出n,在的话输出d[v] - d[u]。
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc {
int u,v,next;
arc(int x = ,int y = ,int z = -) {
u = x;
v = y;
next = z;
}
} e[maxn];
int head[maxn],p[maxn],d[maxn],tot;
void add(int u,int v) {
e[tot] = arc(u,v,head[u]);
head[u] = tot++;
}
void update(int u) {
for(int i = head[u]; ~i; i = e[i].next)
if(!p[e[i].v]) p[e[i].v] = u;
}
int main() {
int kase,n,m,u,v;
scanf("%d",&kase);
while(kase--) {
memset(head,-,sizeof head);
memset(p,,sizeof p);
scanf("%d%d",&n,&m);
for(int i = tot = d[] = ; i < m; ++i) {
scanf("%d%d",&u,&v);
add(u,v);
}
d[] = d[n] = ;
p[] = -;
int L = , R = n,ds = ;
while(L <= R) {
if(p[L]) {
update(L);
d[L++] = ds++;
}
if(p[R]) {
update(R);
d[R--] = ds++;
}
}
for(int i = ; i < tot; ++i)
printf("%d\n",p[e[i].v] == e[i].u?d[e[i].v] - d[e[i].u]:n);
}
return ;
}
2015 Multi-University Training Contest 8 hdu 5385 The path的更多相关文章
- 2015 Multi-University Training Contest 8 hdu 5390 tree
tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...
- 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!
Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: ...
- 2015 Multi-University Training Contest 3 hdu 5324 Boring Class
Boring Class Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ
RGCDQ Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple
CRB and Apple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries
CRB and Queries Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- 2015 Multi-University Training Contest 6 hdu 5362 Just A String
Just A String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 2015 Multi-University Training Contest 6 hdu 5357 Easy Sequence
Easy Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 2015 Multi-University Training Contest 7 hdu 5378 Leader in Tree Land
Leader in Tree Land Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
随机推荐
- CodeForces 19D Points(离散化+线段树+单点更新)
题目链接: huangjing 题意:给了三种操作 1:add(x,y)将这个点增加二维坐标系 2:remove(x,y)将这个点从二维坐标系移除. 3:find(x,y)就是找到在(x,y)右上方的 ...
- maven 自建库
maven repository 标签: mavenjarxmlserver工具磁盘 2009-11-26 10:56 42322人阅读 评论(7) 收藏 举报 目录(?)[+] 什么是Mav ...
- java.util.ComparableTimSort中的sort()方法简单分析
TimSort算法是一种起源于归并排序和插入排序的混合排序算法,设计初衷是为了在真实世界中的各种数据中能够有较好的性能. 该算法最初是由Tim Peters于2002年在Python语言中提出的. T ...
- codetemplate
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templa ...
- Node.js:REPL(交互式解释器)
ylbtech-Node.js:REPL(交互式解释器) 1.返回顶部 1. Node.js REPL(交互式解释器) Node.js REPL(Read Eval Print Loop:交互式解释器 ...
- 将python的程序包装成windows下的service
使用python编写的脚本应用程序,在运行的时候需要有python的运行环境,但是我们肯定是希望整个python程序能够像应用程序一样打包生成一个包括其运行环境的exe文件包,这是第一步,但是要想使用 ...
- 关于阿里云oss
这两天抽时间看了一下阿里云oss,阿里云oss是阿里为大数据推出的开放存储服务,为多种语言预留出了接口,下面是我对php接口的一点理解. 当注册了阿里云oss账号时会得到接口,在config里面填上这 ...
- 试图ddms 如果丢失adv链接解决办法!
点击如下图菜单 重启链接adv即可显示.
- href 与 src
href:常用有两个标签<a>和<link> 1.<a href="http://www.w3school.com.cn">W3School&l ...
- GCC编译步骤
gcc -E t1.c -o t1.i 预处理gcc -S t1.i -o t1.s 转成汇编语言gcc -c t1.s -o t1.o 转成机器码gcc t1.o -o t1.exe 链接 直接使用 ...