2015 Multi-University Training Contest 8 hdu 5385 The path
2015-08-16 14:32
453 查看
The path
Time Limit: 2000msMemory 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 viSample 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
相关文章推荐
- HDU1532 Drainage Ditches 【最大流量】
- POJ---1363-Rails(stack)
- 2015 Multi-University Training Contest 8 hdu 5390 tree
- 一个"Median Maintenance"问题
- Dimensional gate public transport system
- 数组实现的队列(限制最大长度)的head、tail指针的修改方法。
- No grammar constraints (DTD or XML Schema) 处理后导致junit停顿异常
- 关于[No grammar constraints (DTD or XML schema) detected for the document.]
- POJ-2527 Polynomial Remains-多项式相除
- hdoj1789Doing Homework again
- hdoj 2473 Junk-Mail Filter【并查集节点的删除】
- HDU 5172 pairs
- UVa10986_Sending email(最短)(白皮书图论的话题)
- yuanzisaima-C
- HDU 2473-Junk-Mail Filter-并查集删除
- PAT 1079. Total Sales of Supply Chain (25)
- hit2060 Fibonacci Problem Again
- select系统调用源码分析 http://blog.csdn.net/martin_liang/article/details/9124911
- Known plaintext attack
- Clairewd’s message - HDU 4300(next[]运用)