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. CodeForces 19D Points(离散化+线段树+单点更新)

    题目链接: huangjing 题意:给了三种操作 1:add(x,y)将这个点增加二维坐标系 2:remove(x,y)将这个点从二维坐标系移除. 3:find(x,y)就是找到在(x,y)右上方的 ...

  2. maven 自建库

    maven repository 标签: mavenjarxmlserver工具磁盘 2009-11-26 10:56 42322人阅读 评论(7) 收藏 举报   目录(?)[+]   什么是Mav ...

  3. java.util.ComparableTimSort中的sort()方法简单分析

    TimSort算法是一种起源于归并排序和插入排序的混合排序算法,设计初衷是为了在真实世界中的各种数据中能够有较好的性能. 该算法最初是由Tim Peters于2002年在Python语言中提出的. T ...

  4. codetemplate

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><templa ...

  5. Node.js:REPL(交互式解释器)

    ylbtech-Node.js:REPL(交互式解释器) 1.返回顶部 1. Node.js REPL(交互式解释器) Node.js REPL(Read Eval Print Loop:交互式解释器 ...

  6. 将python的程序包装成windows下的service

    使用python编写的脚本应用程序,在运行的时候需要有python的运行环境,但是我们肯定是希望整个python程序能够像应用程序一样打包生成一个包括其运行环境的exe文件包,这是第一步,但是要想使用 ...

  7. 关于阿里云oss

    这两天抽时间看了一下阿里云oss,阿里云oss是阿里为大数据推出的开放存储服务,为多种语言预留出了接口,下面是我对php接口的一点理解. 当注册了阿里云oss账号时会得到接口,在config里面填上这 ...

  8. 试图ddms 如果丢失adv链接解决办法!

    点击如下图菜单 重启链接adv即可显示.

  9. href 与 src

    href:常用有两个标签<a>和<link> 1.<a href="http://www.w3school.com.cn">W3School&l ...

  10. 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 链接 直接使用 ...