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. Different Integers 牛客多校第一场只会签到题

    Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, r ...

  2. bzoj 3513 [MUTC2013]idiots FFT 生成函数

    [MUTC2013]idiots Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 806  Solved: 265[Submit][Status][Di ...

  3. 转:java 帐号激活与忘记密码 实例

    原文链接:http://endual.iteye.com/blog/1613679 一.帐户激活   在 很多时候,在某些网站注册一个用户之后,网站会给这个用户注册时填写的email地址发送一封帐户激 ...

  4. OOP第三次上机

    上机问题 T1 CSet 还是熟悉的CSet,只是多了个构造函数以及收缩空间. T2 SingleTon 单例问题. 用一个指针保存唯一的实例,用户无法在外部直接新建实例,只能使用外部接口(函数),函 ...

  5. [POJ1845&POJ1061]扩展欧几里得应用两例

    扩展欧几里得是用于求解不定方程.线性同余方程和乘法逆元的常用算法. 下面是代码: function Euclid(a,b:int64;var x,y:int64):int64; var t:int64 ...

  6. 【AtCoder】ARC082 F - Sandglass

    [链接]F - Sandglass [题意]给定沙漏A和B,分别装着a和X-a的沙子,开始时A在上B在下,每秒漏1,漏完不再漏.给定n,有n个时刻ai沙漏倒转.给定m个询问,每次询问给定初值a和时刻t ...

  7. 2017年上海金马五校程序设计竞赛:Problem K : Treasure Map (蛇形填数)

    Description There is a robot, its task is to bury treasures in order on a N × M grids map, and each ...

  8. CDLinux 自动休眠功能的关闭方法

    CDLinux 自动休眠功能的关闭方法: 控制台下使用xset命令来完成. xset q  可以查看当前屏幕保护和电源管理的状态信息 具体设置时,常用的有以下参数: xset s  //这个参数设置屏 ...

  9. algorithm ch15 FastWay

    这是DP最基础的一个问题,刚开始学习这一块,实现了一下,不要黑我巨长的参数表,新手. 代码如下: void FastWay(int l1[], int l2[], int e1, int e2, in ...

  10. Git-cheatsheet