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

  1. 2
  2. 4 6
  3. 1 2
  4. 2 4
  5. 1 3
  6. 1 2
  7. 2 2
  8. 2 3
  9. 4 6
  10. 1 2
  11. 2 3
  12. 1 4
  13. 2 1
  14. 2 1
  15. 2 1

Sample Output

  1. 1
  2. 2
  3. 2
  4. 1
  5. 4
  6. 4
  7. 1
  8. 1
  9. 3
  10. 4
  11. 4
  12. 4

Source

 
解题:贪心
 

左边从2开始,右边从n开始,每次选与之前标记过的点相连的未标记过得点,该点的d[i]为该点加入的时间。最后输出时,判断该点是否在最短路上,不在的话,输出n,在的话输出d[v] - d[u]。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = ;
  4. struct arc {
  5. int u,v,next;
  6. arc(int x = ,int y = ,int z = -) {
  7. u = x;
  8. v = y;
  9. next = z;
  10. }
  11. } e[maxn];
  12. int head[maxn],p[maxn],d[maxn],tot;
  13. void add(int u,int v) {
  14. e[tot] = arc(u,v,head[u]);
  15. head[u] = tot++;
  16. }
  17. void update(int u) {
  18. for(int i = head[u]; ~i; i = e[i].next)
  19. if(!p[e[i].v]) p[e[i].v] = u;
  20. }
  21. int main() {
  22. int kase,n,m,u,v;
  23. scanf("%d",&kase);
  24. while(kase--) {
  25. memset(head,-,sizeof head);
  26. memset(p,,sizeof p);
  27. scanf("%d%d",&n,&m);
  28. for(int i = tot = d[] = ; i < m; ++i) {
  29. scanf("%d%d",&u,&v);
  30. add(u,v);
  31. }
  32. d[] = d[n] = ;
  33. p[] = -;
  34. int L = , R = n,ds = ;
  35. while(L <= R) {
  36. if(p[L]) {
  37. update(L);
  38. d[L++] = ds++;
  39. }
  40. if(p[R]) {
  41. update(R);
  42. d[R--] = ds++;
  43. }
  44. }
  45. for(int i = ; i < tot; ++i)
  46. printf("%d\n",p[e[i].v] == e[i].u?d[e[i].v] - d[e[i].u]:n);
  47. }
  48. return ;
  49. }

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. spring学习笔记(22)声明式事务配置,readOnly无效写无异常

    在上一节内容中.我们使用了编程式方法来配置事务,这种优点是我们对每一个方法的控制性非常强.比方我须要用到什么事务,在什么位置假设出现异常须要回滚等.能够进行非常细粒度的配置.但在实际开发中.我们可能并 ...

  2. &lt;LeetCode OJ&gt; 31. Next Permutation

    31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...

  3. 【cl】在代码中查找系统页面中的代码方法

    页面链接http://192.168.2.51:8080/xxcb1/xxbs/action/handling!view.action?toId=402882ae4e7d1761014e877fb22 ...

  4. luogu3807 【模板】 卢卡斯定理

    题目大意 对于一个很大的$n,m,p$如何求$C_{n+m}^m\mod p$? Lucas定理 若$n_i,m_i$分别是$n,m$在$p$进制下第$i$位的数字,则有 $$C_n^m\mod p= ...

  5. nyoj--105--九的余数(水题)

    九的余数 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 现在给你一个自然数n,它的位数小于等于一百万,现在你要做的就是求出这个数整除九之后的余数. 输入 第一行有一个整 ...

  6. 通过top 5等待事件查看sql语句

    设计的动态性能视图有:v$session_event,v$session,v$sqlarea,首先在v$session_event中可以找到event,然后通过其动态性能视图找到sid,可以在v$se ...

  7. day63-webservice 11.cxf整合spring

    如果我们有Spring配置文件,怎么把WebService整合到Spring里面,用Ioc容器来管理这个Bean. 做项目的时候一般都是分层:Dao层.Service层.Service层要调Dao层, ...

  8. hiho一下第76周《Suzhou Adventure》

    我是菜鸡,我是菜鸡,我是菜鸡....重要的事说三遍 算是第一次做树形dp的题吧,不太难.. 园林构成一棵树,root为1,Hi从root出发,有k个园林必须玩,每个园林游玩后会得到权值w[i],最多玩 ...

  9. 一篇个人感觉比较好的lua入门的文章

    原文转自www.cppprog.com,由三篇文章组成 Lua是一个嵌入式的脚本语言,它不仅可以单独使用还能与其它语言混合调用.Lua与其它脚本语言相比,其突出优势在于: 1.  可扩展性.Lua的扩 ...

  10. LeetCode Weekly Contest 20

    1. 520. Detect Capital 题目描述的很清楚,直接写,注意:字符串长度为1的时候,大写和小写都是满足要求的,剩下的情况单独判断.还有:我感觉自己写的代码很丑,判断条件比较多,需要改进 ...