Dicription
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 $\in$ $[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, $u_i$ and $v_i$ $(1 \leq u_i,v_i \leq n)$, indicating there is a link between nodes $u_i$ and $v_i$ and the direction is from $u_i$ to $v_i$.

$\sum n \leq 3*10^5$,$\sum m \leq 6*10^5$ 
$1\leq n,m \leq 10^5$

Output

For each test case,print $m$ lines.The i-th line includes one integer:the length of edge from $u_i$ to $v_i$

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 一开始只有d[1]是已知的且为0,那么我们不妨维护一个区间[l,r],区间内的位置的d值都是未知的,区间外的d值都是已知的且满足任意一个
区间外的d值都小于区间内的d值。
我们让区间不断缩小,最后就可以得到题目要求的玩意了。
问题是怎么缩小?? 反正本题是SPJ只要你得到一个合法解就行了。 而且还有一个很重要的性质是,l或r肯定是[l,r]里d值最小的,所以我们每次只需要把l或者r扩展成已知的然后让l++或者r--就行了的。
假如要扩展l吧,那么首先区间内连向它的边是没有用的,因为本来d值就肯定比它大了,不可能影响最短路;
而区间外的任意一点x连向它的边都至少是d[l]-d[x]且至少有一个是d[l]-d[x],又因为边权有上界限制,所以我们让d[l]为区间外最大的
d值+1即可,这样最大的d也只是n,不会出事。 然后就可以A题了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
#define maxn 100005
using namespace std;
int to[maxn],ne[maxn],hd[maxn];
int num,n,m,l,r,val[maxn],d[maxn];
int now,T; inline void init(){
for(int i=;i<=n;i++) hd[i]=;
for(int i=;i<=m;i++) val[i]=;
num=;
} inline bool check(int pos){
for(int i=hd[pos];i;i=ne[i]) if(to[i]<l||to[i]>r){
now++,d[pos]=now;
for(;i;i=ne[i]) if(to[i]<l||to[i]>r){
val[i]=now-d[to[i]];
}
return ;
}
return ;
} inline void solve(){
d[]=now=;
l=,r=n;
while(l<=r){
if(check(l)) l++;
else check(r),r--;
}
} int main(){
scanf("%d",&T);
while(T--){
init();
int uu,vv;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d",&uu,&vv);
to[++num]=uu,ne[num]=hd[vv],hd[vv]=num;
} solve(); for(int i=;i<=m;i++){
if(!val[i]) val[i]=n;
printf("%d\n",val[i]);
}
} return ;
}
 

HDOJ 5385 The path的更多相关文章

  1. 贪心 HDOJ 5385 The Path

    题目传送门 /* 题意:给一张图和一些有向边,问如何给边赋值使得d1 < d2 < .. < dx > ,,,< ddn 贪心:左边从2开始,右边从n开始,每次选择未标记 ...

  2. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  3. hdu 5385 The path

    http://acm.hdu.edu.cn/showproblem.php?pid=5385 题意: 给定一张n个点m条有向边的图,构造每条边的边权(边权为正整数),令d(x)表示1到x的最短路,使得 ...

  4. HDU.5385.The path(构造)

    题目链接 最短路构造题三连:这道题,HDU4903,SRM590 Fox And City. \(Description\) 给定一张\(n\)个点\(m\)条边的有向图,每条边的边权在\([1,n] ...

  5. 2015 Multi-University Training Contest 8

    Hdu 5385 The path 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5385 题意:有一个联通的有向图,d(x)用来记录从1点到x点的最短 ...

  6. hdu 1232, disjoint set, linked list vs. rooted tree, a minor but substantial optimization for path c 分类: hdoj 2015-07-16 17:13 116人阅读 评论(0) 收藏

    three version are provided. disjoint set, linked list version with weighted-union heuristic, rooted ...

  7. SPFA(建图) HDOJ 4725 The Shortest Path in Nya Graph

    题目传送门 题意:有两种路径,每个点会分别在某一层,层相邻之间权值c.还有直接两点传送,花费w.问1到n的最短距离. 分析:1~n正常建边.然后n + a[i]表示i点在第a[i]层.然后再优化些就不 ...

  8. DP HDOJ 5492 Find a path

    题目传送门 题意:从(1, 1)走到(n, m),每次往右或往下走,问(N+M−1)∑(Ai−Aavg)2 的最小值 分析:展开式子得到(N+M−1)∑(Ai2) - (∑(Ai))2的最小值.用普通 ...

  9. 继续node爬虫 — 百行代码自制自动AC机器人日解千题攻占HDOJ

    前言 不说话,先猛戳 Ranklist 看我排名. 这是用 node 自动刷题大概半天的 "战绩",本文就来为大家简单讲解下如何用 node 做一个 "自动AC机&quo ...

随机推荐

  1. 【BZOJ 2503】相框 图论+讨论

    这道题目就是考验了一下图论基本知识与对可爱的代码实现的应对能力. 我们先分析题干信息.我们要形成相框,那么所有的点的度为2(参与的点),那么所有度大于2的点都需要熔断,而且一次完成所有关于这个点的熔断 ...

  2. [bzoj1033] [ZJOI2008]杀蚂蚁 Big MoNI

    这个模拟就不用说了吧...... 注意事项(救命的):1.不能回原位 2.在可以打到target的塔打target的时候,其他打不到的继续打自己的(这是显然的事情只是当时已惘然) 3.如果游戏在某一秒 ...

  3. P2765 魔术球问题(网络流24题)

    题目描述 «问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全 ...

  4. CSS中的text-shadow。

    text-shadow(文字投影),box-shadow(容器投影),border-radius(圆角)这三个属性估计以后用的比较多,记录 一下.目前不支持IE系列(不过可以使用其他方法实现,下文有详 ...

  5. DIV + CSS问题收集

    div里面有三列数据,怎么让他竖向排列,在css中怎么设置 https://zhidao.baidu.com/question/712007772597664245.html css设置块元素在div ...

  6. noip2016 普及组

    T1 买铅笔 题目传送门 #include<cstdio> #include<cstring> #include<algorithm> using namespac ...

  7. [object-c 2.0 程序设计]object-c file handle (二)

    // // main.m // cmdTry // // Created by Calos Chen on 2017/8/21. // Copyright © 2017年 Calos Chen. Al ...

  8. mobius反演讲解

    mobius反演的基本形式为,假设知道函数F(x)=Σf(d) d|x,那么我们可以推出f(x)=Σmiu(d)*F(x/d) d|x,另一基本形式为假设知道函数F(x)=Σf(d) x|d,那么我们 ...

  9. 编辑器KindEditor的使用

    1.具体使用方法看点这里 2.下载点这里 3.文件夹说明 ├── asp asp示例,删掉 ├── asp.net asp.net示例,删掉 ├── attached 空文件夹,放置关联文件attac ...

  10. button的格式的问题

    1. <input type="button"  class="buttoncls"  onclick=""  value=" ...