HDU 5385(The path-构造最短路树)

The path

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 724    Accepted Submission(s): 277

Special Judge

Problem Description

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 x
satisfy 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

Author

SXYZ

Source

2015 Multi-University Training Contest 8

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5421 5420 5419 5418 5417

我们如果知道每一个点的d[i] 显然cost[i,j]=d[j]-d[i]

我们从起点开始,每次从首或尾加一个节点进来,同时保证连通性

注意不能只找一次,因为首序列与尾序列大小无关

1->n->2->n-1->...->x 反例

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
#define MAXM (100000+10)
#pragma comment(linker, "/STACK:102400000,102400000")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
typedef long long ll;

int n,m;
int u[MAXM],v[MAXM],ans[MAXM];

vector<int> To[MAXN];

int id[MAXN];
bool b[MAXN];
int main()
{
//	freopen("F.in","r",stdin);

	int T; cin>>T;
	while(T--) {
		MEM(u) MEM(v) MEM(ans) 

		cin>>n>>m;

		For(i,n) To[i].clear();

		For(i,m) {
			scanf("%d%d",&u[i],&v[i]);
			To[u[i]].pb(v[i]);
		}

		MEM(id) int cnt=0;
		MEM(b) b[1]=1;
		int l=1,r=n;
		while(l<=r) {
			if (b[l]) {
				id[l]=++cnt;
				int sz=To[l].size();
				Rep(i,sz) b[To[l][i]]=1;
				++l;
			}
			else if (b[r]) {
				id[r]=++cnt;
				int sz=To[r].size();
				Rep(i,sz) b[To[r][i]]=1;
				--r;
			}

		}

		For(i,m)
		{
			ans[i]=id[v[i]]-id[u[i]];
			if (ans[i]<=0||ans[i]>n) ans[i]=n;
			printf("%d\n",ans[i]);
		}

	} 	

	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-06 16:02:57

HDU 5385(The path-构造最短路树)的相关文章

hdu 5385 The path(最短路+构造)

题目链接:hdu 5385 The path 维护一个l,r,l从2开始递增,r从N开始递减,每个距离值l,r至多走一步,并且每次将可以到达的点标记,注意最后最大值只能有一个. #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; typedef pair<int,in

hdu 5385 The path

HDU 5385   构造题 使用贪心法构造,因为保证有解,点2或n至少有一个直接与点1相连 上述结论可以用反证法证明. 假若2和n不直接与1相连,那么必存在点x直接与1相连,间接与2,n相连.这种情况下无论如何设置边权,都有d[x]<d[2]&&d[x]<d[n],与已知矛盾 那么可以按照每次添加两头的点与1相连,记添加时间为1到该点的最短路,边添加边计算边权 然后不在最短路树上的边权都设为n即可,这样不会使最短路树发生变化 #include<cstdio> #i

HDOJ 5385 The path 构造

The path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 521    Accepted Submission(s): 190 Special Judge Problem Description You have a connected directed graph.Let d(x) be the length of the s

HDU - 2433 Travel (最短路树)

题目大意:有N个点,M条边,每次删掉一条边,问删掉该边后,所有点之间的最短路的和是多少 解题思路:刚开始就想,删掉一次floyd一次,结果可想而之,TLE了 后来看了别人的,发现了一种叫做最短路树的东西. 就是先求出以每个点为源点的最短路并纪录该点到每个点的距离和,和每个点的pre,这样的话,就预处理好了 因为要删掉边,前面我们已经预处理好了最短路树的pre,也就是说,就可以依次判断删除的边是否在最短路树上,只要你要删除的边不在该最短路树上,那么就没有影响了,可以直接用前面纪录的数据 如果要删掉

uva 1416 Warfare And Logistics (最短路树)

uva 1416 Warfare And Logistics Description The army of United Nations launched a new wave of air strikes on terrorist forces. The objective of the mission is to reduce enemy's logistical mobility. Each air strike will destroy a path and therefore inc

HDU 4889 Scary Path Finding Algorithm

题意: 构造一幅图  使题中给出的程序按该图执行  最终变量doge大于输入的C跳出 思路: 出题人思维简直神了!  实在是膜拜!  借题解的图: 按照图中所画  可以继续向右延伸 为何这样可以??  为何边权要这么取?? 先回答第二个问题: 取负数是为了可以不断的去更新  例如如果走了上面的-4那条边  那么它右边的所有点就又可以再更新一次  这样使更新达到了指数级别 (其实可以把所有的值加上一个值K  不过这个K一定要取好!) 除了负数外我们发现数字都是2的几次幂  如果我们按2进制来考虑的

UVA 1416 最短路树

Warfare And Logistics The army of United Nations launched a new wave of air strikes on terroristforces. The objective of the mission is to reduce enemy's logistical mobility. Each airstrike will destroy a path and therefore increase the shipping cost

POJ1122_FDNY to the Rescue!(逆向建图+最短路树)

FDNY to the Rescue! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2368   Accepted: 721 Description The Fire Department of New York (FDNY) has always been proud of their response time to fires in New York City, but they want to make the

hdu 5168 Legal path

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5168 题意: 一个有向图,给定起点终点,每条边上有权值. 一条合法的路径定义为相邻边的权值之差不小于K的路径,即路径上每条边的权值至少要比上一条边的权值大K,如果上一条边存在.合法路径的长度定义为路径上的边权值总和. 求从起点到终点的合法路径的最短长度. 限制: 有多组数据,第一行为数据组数T(T≤10). 对于每组数据,第一行为三个整数n,m,K,n,m分别表示这组数据的有向图的点数,边数,起点