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. cookie中的path与domain属性详解

    1.domain表示的是cookie所在的域,默认为请求的地址,如网址为www.jb51.net/test/test.aspx,那么domain默认为www.jb51.net.而跨域访问,如域A为t1 ...

  2. 小K的农场

    小K的农场 题目描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述: 农场a比农场b至少多种植了 ...

  3. Kafka自我学习3-Scalable

    1.After created the zookeeper cluster, we found all broker cluster topic can be find in zoo1, zoo2, ...

  4. AngularJs学习——模拟用户登录的简单操作

    效果截图:

  5. final 的作用

    1.修饰类 类不能被继承 2.修饰方法 目的有二: 1)禁止子类重写该方法 2)执行效率(JVM相关的东西,不用太关注) 3.修饰变量 final修饰原始类型的变量,该变量不能被修改 final修饰引 ...

  6. 【BZOJ1468】Tree [点分治]

    Tree Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description 给你一棵TREE,以及这棵树上边的距 ...

  7. 【BZOJ】1708: [Usaco2007 Oct]Money奶牛的硬币

    [算法]DP [题解] 如果每个排列算一种,则令f[i]表示凑成面值为i的方案数,容易推出f[i]+=f[i-a[j]]. 现在是每个组合才算一种,令f[i][j]第二维表示只使用前j种面值,f[i] ...

  8. 关于vscode的个人配置

      vs code官方下载地址 : https://code.visualstudio.com/Download   下载好的vs code相当是一款纯文本编辑器,接下来开始进行对其配置:   页面设 ...

  9. eclipse使用git下载项目

    准备工作: 目的:从远程仓库github上down所需的项目 eclipse使用Git插件下载github上项目 eclipse版本:eclipse4.5  64位 jdk版本:jdk-1.7 64位 ...

  10. passwd讲解

    root:$dffjioowwf/:16274:0:999999:7::: 1用户名:密码:最近修改密码的日期:密码不能更改的天数:密码过期时间:密码需要更改期限到拉前7发出警告:宽限天数:帐号过期时 ...