您的位置:首页 > 大数据 > 人工智能

2015 Multi-University Training Contest 8 hdu 5385 The path

2015-08-16 14:32 453 查看

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

2015 Multi-University Training Contest 8 1006

解题:贪心

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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 301000;
struct arc {
int u,v,next;
arc(int x = 0,int y = 0,int z = -1) {
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,-1,sizeof head);
memset(p,0,sizeof p);
scanf("%d%d",&n,&m);
for(int i = tot = d[0] = 0; i < m; ++i) {
scanf("%d%d",&u,&v);
add(u,v);
}
d[1] = d
= 1;
p[1] = -1;
int L = 1, R = n,ds = 1;
while(L <= R) {
if(p[L]) {
update(L);
d[L++] = ds++;
}
if(p[R]) {
update(R);
d[R--] = ds++;
}
}
for(int i = 0; i < tot; ++i)
printf("%d\n",p[e[i].v] == e[i].u?d[e[i].v] - d[e[i].u]:n);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: