The path

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 995    Accepted Submission(s): 419
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
题目大意:给定一张n个点m条有向边的图,构造每条边的边权(边权为正整数),令d(x)表示1到x的最短路,使得存在点i(1<=i<=n)满足d(1)<d(2)<…<d(i)>d(i+1)>…>d(n)。
分析:有关最短路的构造题,和hdu4903有点类似,都是事先确定最短路再来构造边权.
   首先,令每个点的最短路都在[0,n-1]区间内,并且每个点的最短路都不一样.如果能够能够使所有点的最短路都合理,那么对于一条边所连的点u,v,只需要这条边的权值等于abs(d(v) - d(u))即可.这就能满足最短路的要求.
   接下来是要使得最短路合理.从两边向中间构造,维护两个指针l,r和一个计数器cnt,一开始l = 2,r = n.先设d[1] = 0,将1号点连边指向的点都打上标记.接着从l,r指针指向的点中选一个打上标记的点,那么这个点的d值就是++cnt.然后把这个点连边所指向的点都打上标记,并且向中间移动指针.因为题目保证有解,所以每次必然有一个指针能够移动.
   这样做是使得这些点到1的最短路合理,如果直接胡乱分配d,设定边权,那么可能只是这条边是最短路上的边,但实际上这些点到1的最短路并不一定是分配好的d.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm> using namespace std; const int maxn = ; int T,n,m,head[maxn],nextt[maxn],to[maxn],tot = ,d[maxn],cnt;
bool vis[maxn]; struct node
{
int x,y,w;
}e[]; void add(int x,int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} int main()
{
scanf("%d",&T);
while (T--)
{
memset(head,,sizeof(head));
memset(vis,false,sizeof(vis));
memset(d,,sizeof(d));
cnt = ;
tot = ;
scanf("%d%d",&n,&m);
for (int i = ; i <= m; i++)
{
scanf("%d%d",&e[i].x,&e[i].y);
add(e[i].x,e[i].y);
}
d[] = ;
for (int i = head[];i;i = nextt[i])
{
int v = to[i];
vis[v] = ;
}
int l = ,r = n;
while (cnt < n)
{
int u;
if (vis[l])
u = l++;
else
u = r--;
d[u] = cnt++;
for (int i = head[u];i;i = nextt[i])
{
int v = to[i];
vis[v] = ;
}
}
for (int i = ; i <= m; i++)
{
if(e[i].x == e[i].y)
printf("%d\n",n);
else
printf("%d\n",abs(d[e[i].x] - d[e[i].y]));
}
} return ;
}

Hdu5385 The path的更多相关文章

  1. NodeJs之Path

    Path模块 NodeJs提供的Path模块,使得我们可以对文件路径进行简单的操作. API var path = require('path'); var path_str = '\\Users\\ ...

  2. 【原】实时渲染中常用的几种Rendering Path

    [原]实时渲染中常用的几种Rendering Path 本文转载请注明出处 —— polobymulberry-博客园 本文为我的图形学大作业的论文部分,介绍了一些Rendering Path,比较简 ...

  3. Node.js:path、url、querystring模块

    Path模块 该模块提供了对文件或目录路径处理的方法,使用require('path')引用. 1.获取文件路径最后部分basename 使用basename(path[,ext])方法来获取路径的最 ...

  4. VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%

    1.问题描述 由于安装VS15 Preview 5,搞的系统由重新安装一次:在用vscdoe编译go语言时,出现以下问题: # odbcexec: "gcc": executabl ...

  5. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  6. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  7. Thinking in Unity3D:渲染管线中的Rendering Path

      关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的 ...

  8. node之path模块

    node之path模块 原文链接 //引用该模块 var path = require("path"); 1.路径解析,得到规范化的路径格式 对window系统,目录分隔为'', ...

  9. Linux系统修改PATH环境变量方法

    在Linux安装一些软件通常要添加路径环境变量PATH.PATH环境变量通俗的讲就是把程序的路径"备案"到系统中,这样执行这些程序时就不需要输入完整路径,直接在bash输入程序名就 ...

随机推荐

  1. Hadoop生态圈-Azkaban实战之Command类型执行指定脚本

    Hadoop生态圈-Azkaban实战之Command类型执行指定脚本 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1>.服务端测试代码(别忘记添加权限哟!) [yinzh ...

  2. Python基础数据类型-函数传参详解

    Python基础数据类型-函数传参详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.位置参数 #!/usr/bin/env python #_*_coding:utf-8_* ...

  3. javamail插件发送不同类型邮件方式

    一.RFC882文档简单说明 RFC882文档规定了如何编写一封简单的邮件(纯文本邮件),一封简单的邮件包含邮件头和邮件体两个部分,邮件头和邮件体之间使用空行分隔. 邮件头包含的内容有: from字段 ...

  4. docker 时区设置

    今天查问题的时候发现 在对时间 格式化为 时间戳的时候,time.mktime(time.strptime('20170609-15:00:00','%Y%m%d-%H:%M:%S')) 发现测试环境 ...

  5. ubuntu 发送邮件

    1. 使用下面命令安装 sudo apt-get install heirloom-mailx 2. 编辑配置信息 vim /etc/nail.rc //此时如果打印没有权限则使用sudo命令,并且在 ...

  6. 考研:操作系统:进程同步—信号量实现同步互斥(PV操作)

    进程互斥的硬件实现方法

  7. 20155227 2016-2017-2 《Java程序设计》第八周学习总结

    20155227 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 NIO与NIO2 NIO即New IO.java从JDK1.4开始提供了NIO,在JAVA ...

  8. PHP删除数组中空值

    array_filter   函数的功能是利用回调函数来对数组进行过滤,一直都以为用回调函数才能处理, 却没有发现手册下面还有一句,如果没有回调函数,那么默认就是删除数组中值为false的项目 代码: ...

  9. bellman-ford算法(判断有没有负环)

    #include <iostream> #include <vector> #include<string> #include<cstring> usi ...

  10. 【并行计算】用MPI进行分布式内存编程(一)

    通过上一篇关于并行计算准备部分的介绍,我们知道MPI(Message-Passing-Interface 消息传递接口)实现并行是进程级别的,通过通信在进程之间进行消息传递.MPI并不是一种新的开发语 ...