[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 ...
随机推荐
- SSM登录操作
1.编写实体类 略 2. 写mapper映射文件 通过名字查询 通过ID主键查询... 略 写dao CRUD相关抽象方法 List<Student> getAll(); Student ...
- 《SQLi-Labs》03. Less 11~15
@ 目录 索引 Less-11 题解 原理 Less-12 题解 Less-13 题解 Less-14 题解 Less-15 题解 原理 sqli.开启新坑. 索引 Less-11:POST 回显注入 ...
- pygame 入门实例教程 1 - 复古方块赛车游戏
作者自我介绍:大爽歌, b站小UP主 ,直播编程+红警三 ,python1对1辅导老师 . 本教程步骤明确,过程清晰简明,最终代码量250行上下,适合学习pygame的新手. 项目代码已上传到我的gi ...
- 「codeforces - 1486F」Pairs of Paths
link. 还算萌,但是代码有些难写-- 你首先会想要 int n, m, fa[20][300100], pa[300100], dep[300100], cnt[900100]; int ldf[ ...
- Biwen.QuickApi代码生成器功能上线
[QuickApi("hello/world")] public class MyApi : BaseQuickApi<Req,Rsp>{} 使用方式 : dotnet ...
- c语言代码练习6
//输入三个数字,依次按照从大到小输出#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int a = 0 ...
- Python使用socket的UDP协议实现FTP文件服务
简介 本示例主要是用Python的socket,使用UDP协议实现一个FTP服务端.FTP客户端,用来实现文件的传输.在公司内网下,可以不适用U盘的情况下,纯粹使用网络,来实现文件服务器的搭建,进而实 ...
- 栈和堆的区别、FreeRTOS 中的任务栈
栈和堆的区别.FreeRTOS 中的任务栈 01 堆和栈的概念 堆 功能 堆是一块用于动态分配内存的区域,用于存储程序运行时动态创建的对象.堆的大小可以在程序运行时动态调整. 特点 堆的分配和释放是由 ...
- 在 Linux 环境(Ubuntu)下安装 Slurm 和 OpenMPI
安装 Slurm 从软件源安装 slurm-wlm(每个节点都需要装的执行工具).slurm-client(客户机装的提交命令的工具).munge(节点间通信插件) sudo apt install ...
- for遍历
for遍历 一:常规方式 1.遍历数组 int arr[10] = {1,2,3,4,5,6,7,8,9,10}; for(int i = 0;i<10;i++) { cout<<a ...