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 ...
随机推荐
- Jemeter第一个实例
http://www.jianshu.com/p/0e4daecc8122?from=timeline&isappinstalled=0 学习地址:http://leafwf.blog.51c ...
- 学习vi和vim编辑器(1):vi文本编辑器
UNIX系统中有非常多编辑器.能够分为两种类型:行编辑器和全屏编辑器.行编辑器每次仅仅能在屏幕中显示文件的一行,如ed和ex编辑器.全屏编辑器能够在屏幕上显示文件的一部分. vi(读为vee-eye) ...
- SWERC13 Trending Topic
map暴力. .. Imagine you are in the hiring process for a company whose principal activity is the analys ...
- 安装eclipse maven插件m2eclipse No repository found containing
m2eclipse插件是Eclipse的一款Maven插件. 安装m2eclipse插件的步骤例如以下: 启动Eclipse,在菜单条中选择Help,然后选择Install New Software- ...
- codetemplate
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templa ...
- day63-webservice 08.在web项目中配置带有接口的webservice服务
这个是配置带有接口的WebService的服务. http://localhost:8080/cxf-web-server/service 带有接口的实现类也给它做好了.jaxws:endpoint是 ...
- LINUX/UNIX找回删除的文件
当Linux计算机受到入侵时,常见的情况是日志文件被删除,以掩盖攻击者的踪迹.管理错误也可能导致意外删除重要的文件,比如在清理旧日志时,意外地删除了数据库的活动事务日志.有时可以通过lsof来恢复这些 ...
- php打马赛克
本文实例讲述了php实现图片局部打马赛克的方法.分享给大家供大家参考.具体分析如下: 原理: 对图片中选定区域的每一像素,增加若干宽度及高度,生成矩型.而每一像素的矩型重叠在一起,就形成了马赛克效果. ...
- 多个tomcat配置,解决冲突问题
一.一般修改 路径: /opt/apache-tomcat/conf/server.xml 1.第一个tomcat使用默认配置 2.第二个tomcat一般配置 二.特殊修改 1.第二个tomcat特殊 ...
- C-C语言概述
1.数据+算法=程序. 2.C语言程序是由一个或多个函数组成的,函数是由语句组成的,语句是由关键字,标识符,运算符,数据组成的:语句可分为:声明语句,赋值语句,控制语句,函数语句,空语句. 3.#in ...