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 ...
随机推荐
- Unity 动画系统
Legacy动画系统:Animation组件(旧) Mecanim动画系统:Animator组件(新) 动画播放过程: //动画片段 [System.Serializable] public clas ...
- cobbler批量安装操作
打开mirrors.aliyun.com/epel http://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm rpm -ivh h ...
- Numpy系列(一)- array
初始Numpy 一.什么是Numpy? 简单来说,Numpy 是 Python 的一个科学计算包,包含了多维数组以及多维数组的操作. Numpy 的核心是 ndarray 对象,这个对象封装了同质数据 ...
- Docker:手动制作镜像 [五]
一.制作docker镜像的步骤 1.启动容器安装软件服务 2.将安装好服务的容器commit提交为镜像 3:.启动新容器来测试新提交的镜像 二.制作支持ssh远程登录的docker镜像 1.启动容器安 ...
- 深入剖析Kubernetes学习笔记:开篇词(00)
一.关于Kubernetes初学的疑惑 就在这场因"容器"而起的技术变革中,kubernetes项目已经成为容器技术的事实标准,重新定义了基础设置领域对应用编排与管理的种种可能 1 ...
- EF CodeFirst系列(6)---配置1对1,1对多,多对多关系
这一节介绍EF CodeFirst模式中的1对0/1,1对多,多对多关系的配置,只有梳理清楚实体间的关系,才能进行愉快的开发,因此这节虽然很简单但是还是记录了一下. 1. 1对0/1关系配置 1. 通 ...
- golang命令行库cobra使用
github地址:https://github.com/spf13/cobra Cobra功能 简单子命令cli 如 kubectl verion kubectl get 自动识别-h,--h ...
- ue4 材质表达式分类
绿色节点 颜色 Color Desaturation 数学 Math GO 字体 Font FontSample,FontSampleParameter 实用程序 Utility 常用: Desatu ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 【原创】大数据基础之Ambari(3)通过Ambari部署Airflow
ambari2.7.3(hdp3.1) 安装 airflow1.10 ambari的hdp中原生不支持airflow安装,下面介绍如何通过mpack方式使ambari支持airflow安装: 1 下载 ...