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

/* ***********************************************
Author        :CKboss
Created Time  :2015年08月15日 星期六 12时15分02秒
File Name     :HDOJ5385_2.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int maxn=100100;

struct P
{
	int from,to,len;
}ep[maxn];

struct Edge
{
	int to,next,id;
}edge[maxn];

int Adj[maxn],Size;
int n,m;
int Time[maxn];
bool vis[maxn];

void init()
{
	memset(Time,0,sizeof(Time));
	memset(vis,false,sizeof(vis));
	memset(Adj,-1,sizeof(Adj));
	Size=0;
}

void Add_Edge(int u,int v,int id)
{
	edge[Size].to=v;
	edge[Size].id=id;
	edge[Size].next=Adj[u];
	Adj[u]=Size++;
}

bool ans[maxn];

void color(int u,int t)
{
	Time[u]=t;
	for(int i=Adj[u];~i;i=edge[i].next)
	{
		int v=edge[i].to;
		int eid=edge[i].id;
		if(vis[v]==true) continue;
		ans[eid]=true;
		vis[v]=true;
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		init();
		scanf("%d%d",&n,&m);
		for(int i=0,u,v;i<m;i++)
		{
			scanf("%d%d",&u,&v);
			Add_Edge(u,v,i);
			ep[i].from=u; ep[i].to=v; ep[i].len=-1;
		}

		int ti=1;
		int s=1,e=n;
		vis[1]=true; Time[1]=1;

		memset(ans,false,sizeof(ans));

		while(s<=e)
		{
			if(vis[s]) color(s++,ti++);
			if(vis[e]) color(e--,ti++);
		}

		for(int i=0;i<m;i++)
		{
			if(ans[i]) printf("%d\n",abs(Time[ep[i].from]-Time[ep[i].to]));
			else printf("%d\n",n);
		}
	}

    return 0;
}

版权声明:----- from: http://blog.csdn.net/ck_boss

时间: 2024-08-24 02:37:31

HDOJ 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 4671 Backup Plan 构造优先队列

优先队列构造前两列.... Backup Plan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 775    Accepted Submission(s): 365 Special Judge Problem Description Makomuno has N servers and M databases. All databa

HDOJ 5534 Virtual Participation 构造

构造 小于10^5的直接输出1 大于10^5的构造如下的串 1111...12222...233....3.....t,t+1,t+2.. 设长度为n,每种字符出现了L[i]次则不同的串有  n*n+n-sigma(L[i])=2*K 大约估计一个n,用贪心求出L Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi

ClassLoader.getResourceAsStream(name); 获取配置文件的方法

ClassLoader.getResourceAsStream(name);路径问题 InputStream in = getClass().getResourceAsStream('/'+"spring-beans.dtd"); 表示从classs目录下面的找文件,文件放在src下面就可以了.InputStream in = getClass().getResourceAsStream("spring-beans.dtd"); 表示从当前classs下面的路径找文

【转】最大流EK算法

转自:http://www.cnblogs.com/kuangbin/archive/2011/07/26/2117636.html 图-1 如图-1所示,在这个运输网络中,源点S和汇点T分别是1,7,各边的容量为C(u,v).图中红色虚线所示就是一个可行流.标准图示法如图-2所示: 其中p(u,v) / c(u,v)分别表示该边的实际流量与最大容量. 关于最大流 熟悉了什么是网络流,最大流也就很好理解了.就是对于任意的u∈V-{s},使得p(s,u)的和达到最大.上面的运输网络中,最大流如图-

JAVA用POI读取和创建2003和2007版本Excel完美示例

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import j

然并卵, Xamarin Android 仿制个性动画菜单

唱衰 .NET / Xamarin 的, 看完图示就可以关掉了. 不为证明什么, 仅为示例, 不做实际用途, 所以然并卵. 照例,上图 上代码: https://github.com/gruan01/Xamarin-Example/tree/master/DiscMenu 此示例 防照: http://blog.csdn.net/lmj623565791/article/details/43131133 不过没有按照他样那做, 因为那也是然并卵. 谁会把 App 做的如此花梢? 本示例主要 实践

ZooKeeper源码分析:Quorum请求的整个流程(转)

Quorum请求是转发给Leader处理,并且需要得一个Follower Quorum确认的请求.这些请求包括: 1)znode的写操作(OpCode.create,OpCode.delete,OpCode.setData,OpCode.setACL) 2)Session的创建和关闭操作(OpCode.createSession和OpCode.closeSession) 3)OpCode.multi操作. 本文分析了Client, Follower和Leader协同完成Quorum请求的过程.另