The path

Time Limit: 2000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 5385
64-bit integer IO format: %I64d      Java class name: Main

Special Judge
 
You have a connected directed graph.Let d(x) be the length of the shortest path from 1 to x.Specially d(1)=0.A graph is good if there exist xsatisfy d(1)<d(2)<....d(x)>d(x+1)>...d(n).Now you need to set the length of every edge satisfy that the graph is good.Specially,if d(1)<d(2)<..d(n),the graph is good too.

The length of one edge must ∈ [1,n]

It's guaranteed that there exists solution.

 

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
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

For each test case,print m lines.The i-th line includes one integer:the length of edge from ui to vi

 

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的更多相关文章

  1. 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 ...

  2. 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:  ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. WAS_集群部署应用遭遇ADMA0085E和ADMA0109W错误

    原创作品,出自 "深蓝的blog" 博客.深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/47143431 近日 ...

  2. HDU3117-Fibonacci Numbers(矩阵高速幂+log)

    题目链接 题意:斐波那契数列,当长度大于8时.要输出前四位和后四位 思路:后四位非常easy,矩阵高速幂取模,难度在于前四位的求解.  已知斐波那契数列的通项公式:f(n) = (1 / sqrt(5 ...

  3. SRM 622 D2L3: Subsets, math, backtrack

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=10554&rd=15855 符合条件的集中非1的元素个数是非 ...

  4. Vue环境搭建+VSCode+Win10

    一.安装Node.js(js的运行环境) 1.在Node.js官网https://nodejs.org/en/download/下载安装包.2.下载后进行安装.3.打开命令行,输入node -v可以查 ...

  5. Shell编程中Shift的用法【转】

    本文转载自:http://www.cnblogs.com/image-eye/archive/2011/08/20/2147153.html Shell编程中Shift的用法 位置参数可以用shift ...

  6. Build website project by roslyn through devenv.com

    1.fetch the source code2.compile controls project3.copy files under bin folder of controls to bin fo ...

  7. 深入Session

    早上考虑Spring MVC和Structs2项目共用时看到一个问题,如何保持session一致?Session是怎么样被服务器处理的呢,Spring MVC中是如何封装处理Session并在不同请求 ...

  8. [HTML] 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能

    在做一个微信的微网站中的一个便民服务电话功能的应用,用到移动web页面中列出的电话号码,点击需要实现调用通讯录,网页一键拨号的拨打电话功能. 如果需要在移动浏览器中实现拨打电话,发送email,美国服 ...

  9. 原生JS通过勾股定理计算苹果菜单效果

    JS原生苹果菜单效果 知识点: 勾股定理 a²+b²=c² Event 是一个事件对象,当一个事件发生后,和当前事件发生相关的详细信息会被临时存储到一个指定的地方,也就是event对象,以方便我们在需 ...

  10. web拼图错误分析

    老师要求用web制作一个拼图游戏. 发现的问题:点击随机生成拼图的按钮后,打乱的图片会出现无法还原的情况. 发现过程:每次生成一个拼图后会测试它怎么拼回去,结果发现有时候拼不回去. 数学原理:如果两个 ...