HDU.5385.The path(构造)
最短路构造题三连:这道题,HDU4903,SRM590 Fox And City。
\(Description\)
给定一张\(n\)个点\(m\)条边的有向图,每条边的边权在\([1,n]\)之间。记\(d[i]\)为\(1\)到\(i\)的最短路。你需要对每条边确定一个边权,使得存在一个\(i\in[2,n]\),满足\(d[1]\lt d[2]\lt...d[i]\gt d[i+1]\gt...d[n]\)。
输出方案(每条边的边权)。输入保证有解。
\(n,m\leq10^5\)。
\(Solution\)
\(d[1]=0\),考虑\(d[i]=1\)的点有哪些。那要么是\(2\),要么是\(n\),但一定存在边\(1\to i\)。
\(d[i]=2,3...\)的点同理。
所以标记\(1\)能到的所有点,然后维护两个指针\(l=2,r=n\)。从\(l,r\)中选一个标记过的,令其\(d[i]=++now\),然后标记能到的点,往中间移动这个指针即可。
因为保证有解,所以每次至少有一个指针会移动。
输出答案时对于边\((i,j)\),输出\(|d[i]-d[j]|\)即可。
会有重边,不要输出\(0\)就好了。
确实是这样的=-=

//187MS 3328K
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
#define MAXIN 500000
//#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=1e5+5;
int Enum,H[N],nxt[N],fr[N],to[N],dis[N];
bool vis[N];
char IN[MAXIN],*SS=IN,*TT=IN;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-48,c=gc());
return now;
}
inline void AE(int v,int u)//opposite
{
to[++Enum]=v, fr[Enum]=u, nxt[Enum]=H[u], H[u]=Enum;
}
int main()
{
for(int T=read(); T--; )
{
int n=read(),m=read();
Enum=0, memset(H,0,n+1<<2), memset(vis,0,n+1);
for(int i=1; i<=m; ++i) AE(read(),read());
for(int i=H[1]; i; i=nxt[i]) vis[to[i]]=1;
for(int l=2,r=n,now=1; l<=r; )
{
int x=vis[l]?l++:r--; dis[x]=now++;
for(int i=H[x]; i; i=nxt[i]) vis[to[i]]=1;
}
for(int i=1; i<=m; ++i) printf("%d\n",fr[i]==to[i]?1:std::abs(dis[fr[i]]-dis[to[i]]));
}
return 0;
}
HDU.5385.The path(构造)的更多相关文章
- hdu 5385 The path
http://acm.hdu.edu.cn/showproblem.php?pid=5385 题意: 给定一张n个点m条有向边的图,构造每条边的边权(边权为正整数),令d(x)表示1到x的最短路,使得 ...
- 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 ...
- 贪心 HDOJ 5385 The Path
题目传送门 /* 题意:给一张图和一些有向边,问如何给边赋值使得d1 < d2 < .. < dx > ,,,< ddn 贪心:左边从2开始,右边从n开始,每次选择未标记 ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- HDU 5573 Binary Tree 构造
Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- hdu 5015 233 Matrix(构造矩阵)
http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...
- HDU 5813 Elegant Construction 构造
Elegant Construction 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...
随机推荐
- Linux硬盘性能测试工具 - FIO
1.安装:方法一:直接用指令yum -y install fio方法二:如果方法一不可行则,在官网http://freshmeat.net/projects/fio/下载fio的安装包.安装方法很简单 ...
- Python之——CentOS 6.5安装Python2.7.14
Python之——CentOS 6.5安装Python2.7.14 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/l1028386804/art ...
- Day047--JS BOM介绍, jQuery介绍和使用
内容回顾 DOM 文档对象模型(model) 一个模型就是一个对象(属性和方法 面向对象的三大特性:封装 继承 多态) 为了可扩展性 DOM操作 标签属性操作 获取值 getAttribute() 设 ...
- ansible基本使用方法
一.ansible的运行流程 ansible是基于ssh模块的软件,所以主控端和被控端的ssh服务必须正常才能保证ansbile软件的可用性. 检查ssh服务是否正常: systemctl sta ...
- 爬虫保存cookies时重要的两个参数(ignore_discard和ignore_expires)的作用
两个参数的作用: 官方的解释: ignore_discard: save even cookies set to be discarded. ignore_expires: save even coo ...
- 深入剖析Kubernetes学习笔记:深入理解镜像(08)
一.Python 应用案例环境 [root@k8s-node1 Flask]# pwd /opt/Dockerfile/Flask [root@k8s-node1 Flask]# ll total 1 ...
- array_slice()函数造成的一次sql注入
HDwiki6.0 sql注入 下载连接http://kaiyuan.hoodong.com/download/ 漏洞出现在\control\edition.php的docompare()函数 !de ...
- [物理学与PDEs]第5章第4节 本构方程 - 应力与变形之间的关系
5. 4 本构方程 - 应力与变形之间的关系 5.4.1. 本构关系的一般形式 1. 若 Cauchy 应力张量 ${\bf T}$ 满足 $$\bex {\bf T}({\bf y})=\hat{\ ...
- [译]Ocelot - Delegating Handlers
原文 可以为HttpClient添加delegating handlers. Usage 为了添加delegating handler需要做两件事. 首先如下一样创建一个类. public class ...
- mac office2016