[ARC144E]GCD of Path Weights
Problem Statement
You are given a directed graph $G$ with $N$ vertices and $M$ edges. The vertices are numbered $1, 2, \ldots, N$. The $i$-th edge is directed from Vertex $a_i$ to Vertex $b_i$, where $a_i < b_i$.
The beautifulness of a sequence of positive integers $W = (W_1, W_2, \ldots, W_N)$ is defined as the maximum positive integer $x$ that satisfies the following:
- For every path $(v_1, \ldots, v_k)$ ($v_1 = 1, v_k = N$) from Vertex $1$ to Vertex $N$ in $G$, $\sum_{i=1}^k W_{v_i}$ is a multiple of $x$.
You are given an integer sequence $A = (A_1, A_2, \ldots, A_N)$. Find the maximum beautifulness of a sequence of positive integers $W = (W_1, \ldots, W_N)$ such that $A_i \neq -1 \implies W_i = A_i$. If the maximum beautifulness does not exist, print -1.
Constraints
- $2\leq N\leq 3\times 10^5$
- $1\leq M\leq 3\times 10^5$
- $1\leq a_i < b_i \leq N$
- $(a_i,b_i)\neq (a_j,b_j)$ if $i\neq j$
- In the given graph $G$, there is a path from Vertex $1$ to Vertex $N$.
- $A_i = -1$ or $1\leq A_i\leq 10^{12}$
Input
Input is given from Standard Input in the following format:
$N$ $M$
$a_1$ $b_1$
$\vdots$
$a_M$ $b_M$
$A_1$ $A_2$ $\ldots$ $A_N$
Output
Print the maximum beautifulness of a sequence of positive integers $W$. If the maximum beautifulness does not exist, print -1.
Sample Input 1
4 4
1 2
1 3
2 4
3 4
-1 3 7 -1
Sample Output 1
4
There are two paths from Vertex $1$ to Vertex $N$: $(1,2,4)$ and $(1,3,4)$.
For instance, $W = (5, 3, 7, 8)$ has a beautifulness of $4$. Indeed, both $W_1 + W_2 + W_4 = 16$ and $W_1 + W_3 + W_4 = 20$ are multiples of $4$.
Sample Input 2
4 5
1 2
1 3
2 4
3 4
1 4
-1 3 7 -1
Sample Output 2
1
There are three paths from Vertex $1$ to Vertex $N$: $(1,2,4)$, $(1,3,4)$, and $(1,4)$.
For instance, $W = (5, 3, 7, 8)$ has a beautifulness of $1$.
Sample Input 3
4 4
1 2
1 3
2 4
3 4
3 -1 -1 7
Sample Output 3
-1
For instance, $W = (3, 10^{100}, 10^{100}, 7)$ has a beautifulness of $10^{100}+10$. Since you can increase the beautifulness of $W$ as much as you want, there is no maximum beautifulness.
Sample Input 4
5 5
1 3
3 5
2 3
3 4
1 4
2 -1 3 -1 4
任意一条从 1 到 \(n\) 的路径都是 \(x\) 的倍数,这是一个很固定的要求。比如现在有这样的两条路径:

那么 \(w_2+w_3+w_n=w_2+w_4+w_n(\bmod x)\)
\(w_2+w_3+w_n-w_2-w_4-w_n=0(\bmod x)\)
这启发我们建一条反边,权值取反。
但权值在点上啊
拆点,把一个点拆成两个,中间的边的正的方向边权是点的权值,反的方向是点的权值取负。原来的单向边改为双向边,权值为0。此时我们想要让图只有 0 环。
只有0环的图有什么特点?从任意一个点出发,到某一个点的任意路径长度相等(又绕回了开头)。
那么如果此时从 \(1\) 开始搜索,到达某一个点有一条路径距离 \(d\),另一条是 \(w\),那么 \(x\) 整除 \(|d-w|\)
这样子不断求gcd,可以得到 \(x\) 的限制。
但是如果点权为 \(-1\)?拆出来的两个点不连边。因为中间这条边可以随意决定。
注意要特判如果 \(1\) 到 \(n\) 是一个连通块,那么 \(x\) 要和 \(d_n\) 求一个 gcd.
小细节:所有 1 到不了的点和到不了 \(n\) 的点都是不影响 \(x\) 的,要去掉。
真的算一个神题了,知识点最难的也就dfs,但 rated 直飙 3280
[ARC144E]GCD of Path Weights的更多相关文章
- Linux下多路径multipath配置
一.multipath在redhat 6.2中的基本配置: 1. 通过命令:lsmod |grep dm_multipath 检查是否正常安装成功.如果没有输出说明没有安装那么通过yum功能安装一下 ...
- 转://Linux Multipath多路径配置与使用案例
在Linux平台一部分存储产品使用操作系统自带的多路径软件,包括最常见的HP和IBM的部分存储产品,在Linux自带的多路径软件叫做multipath,这篇文章以HP EVA系列存储在Linux平台的 ...
- CentOS配置multipath
可以通过2种方式查看HBA的WWN信息: 1. 查看sys文件系统 查看HBA卡型号:[root@localhost ~]# lspci | grep -i fibre13:00.0 Fibre C ...
- Linux下多路径multipath配置【转】
一.multipath在redhat 6.2中的基本配置: 1. 通过命令:lsmod |grep dm_multipath 检查是否正常安装成功.如果没有输出说明没有安装那么通过yum功能安装一下 ...
- CH Round #53 -GCD Path
描述 给定一张N个点的有向图,点i到点j有一条长度为 i/(gcd(i,j))的边.有Q个询问,每个询问包含两个数x和y,求x到y的最短距离. 输入格式 第一行包含两个用空格隔开的整数,N和Q. 接下 ...
- Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5646 Accepted: 1226 Description In an ...
- IOS 多线程05-OperationQueue 、GCD详解
注:本人是翻译过来,并且加上本人的一点见解. 1. 开始 目前在 iOS中有两套先进的同步 API 可供我们使用:操作队列OperationQueue和 GCD .其中 GCD 是基于 C 的底层 ...
- GCD详解
什么是GCD? Grand Central Dispatch或者GCD,是一套低层API,提供了一种新的方法来进行并发程序编写.从基本功能上讲,GCD有点像 NSOperationQueue,他们都允 ...
- POJ3764 The xor-longest Path
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6361 Accepted: 1378 Description In ...
- Grand Central Dispatch (GCD)
Grand Central Dispatch (GCD) Reference Grand Central Dispatch (GCD) comprises language features, run ...
随机推荐
- 小白整理了VUEX
在小白开发的项目中前端使用的是Vue,虽然在日常工作中可以使用Vue进行开发工作.但是没有系统的学习过Vue,对Vue的一些特性和方法使用时常常需要查询资料解决问题,查询资料常常会占用大量时间,尤其对 ...
- from my mac
hello
- Go学习笔记1
学习路线 2023-Go全链路工程师课纲 https://www.processon.com/view/link/63594cd97d9c0854f9ac855e 一.搭建环境 https://stu ...
- 在CentOS 8上安装Xrdp远程桌面服务
如何在CentOS 8上安装Xrdp远程桌面服务 写在前面 Xrdp是Microsoft远程桌面协议(RDP)的开源实现,可让您以图形方式控制远程系统.使用RDP,您可以登录到远程计算机并创建真实的桌 ...
- Springboot多种字段copy工具比较
结论:推荐使用spring自带的copy工具,不能copy的手动set 1.springboot自带的BeanUtils.copyProperties package com.admin; impor ...
- 怎么搭建web组态
web组态是指通过可视化拖拽组件的方式,低代码搭建监控大屏.web页面.物联网各行业的数据以及监控场景,每个行业的业务不同,需要展示的页面也不同.组态快速搭建页面的优势,能更好的满足不同定制化监控页面 ...
- Linux服务器使用Redis作为数据缓存,并用log4j2进行日志记录
前言 个人网站使用Vue作为前端,SpringBoot作为后端,MySQL作为数据库,但前端每次请求都会从MySQL数据库中读取数据,而MySQL数据库的数据是存储于服务器磁盘中,所以响应速度有一定影 ...
- Java 21 新特性:switch的模式匹配
在之前的Java 17新特性中,我们介绍过关于JEP 406: switch的模式匹配,但当时还只是关于此内容的首个预览版本.之后在JDK 18.JDK 19.JDK 20中又都进行了更新和完善.如今 ...
- C++的extern关键字在HotSpot VM中的重要应用
extern关键字有两个用处: (1)extern在C/C++语言中表示函数和全局变量作用范围(可见性)的关键字,这个关键字会告诉编译器,其声明的函数和变量可以在本模块或其它模块中使用. (2)在C+ ...
- Go语言精进之路目录
目录 一.介绍 01.Go 语言的前生今世 二.开发环境搭建 01.Go 语言开发环境搭建 三.初识GO语言 01.Go 多版本管理工具 02.第一个 Go 程序"hello,world&q ...