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 (1ui,vin) , indicating there is a link between nodes ui and vi and the direction is from ui to vi .

n3105 , m6105
1n,m105
 

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



你可能感兴趣的:(HDU 5385(The path-构造最短路树))