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的更多相关文章

  1. Linux下多路径multipath配置

    一.multipath在redhat 6.2中的基本配置: 1. 通过命令:lsmod |grep dm_multipath  检查是否正常安装成功.如果没有输出说明没有安装那么通过yum功能安装一下 ...

  2. 转://Linux Multipath多路径配置与使用案例

    在Linux平台一部分存储产品使用操作系统自带的多路径软件,包括最常见的HP和IBM的部分存储产品,在Linux自带的多路径软件叫做multipath,这篇文章以HP EVA系列存储在Linux平台的 ...

  3. CentOS配置multipath

    可以通过2种方式查看HBA的WWN信息: 1. 查看sys文件系统 查看HBA卡型号:[root@localhost ~]# lspci  | grep -i fibre13:00.0 Fibre C ...

  4. Linux下多路径multipath配置【转】

    一.multipath在redhat 6.2中的基本配置: 1. 通过命令:lsmod |grep dm_multipath  检查是否正常安装成功.如果没有输出说明没有安装那么通过yum功能安装一下 ...

  5. CH Round #53 -GCD Path

    描述 给定一张N个点的有向图,点i到点j有一条长度为 i/(gcd(i,j))的边.有Q个询问,每个询问包含两个数x和y,求x到y的最短距离. 输入格式 第一行包含两个用空格隔开的整数,N和Q. 接下 ...

  6. Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5646   Accepted: 1226 Description In an ...

  7. IOS 多线程05-OperationQueue 、GCD详解

      注:本人是翻译过来,并且加上本人的一点见解. 1. 开始 目前在 iOS中有两套先进的同步 API 可供我们使用:操作队列OperationQueue和 GCD .其中 GCD 是基于 C 的底层 ...

  8. GCD详解

    什么是GCD? Grand Central Dispatch或者GCD,是一套低层API,提供了一种新的方法来进行并发程序编写.从基本功能上讲,GCD有点像 NSOperationQueue,他们都允 ...

  9. POJ3764 The xor-longest Path

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6361   Accepted: 1378 Description In ...

  10. Grand Central Dispatch (GCD)

    Grand Central Dispatch (GCD) Reference Grand Central Dispatch (GCD) comprises language features, run ...

随机推荐

  1. Esxi 8 更换Nvme硬盘后 如何迁移Esxi主机和虚拟机到新硬盘

    Esxi 8 更换Nvme硬盘后 如何迁移Esxi主机和虚拟机到新硬盘 因为去年底开始SSD和内存大幅降价,ITGeeker技术奇客就想着给自己的小主机升个级,换个三星1G的980硬盘,再加了一根32 ...

  2. 前端Vue仿企查查 天眼查知识产权标准信息列表组件

    ​ 引入Vue仿企查查天眼查知识产权标准信息列表组件 随着技术的不断发展,传统的开发方式使得系统的复杂度越来越高.在传统开发过程中,一个小小的改动或者一个小功能的增加可能会导致整体逻辑的修改,造成牵一 ...

  3. 小札 Combinatorics 2

    对于 Newton Expansion,式子本身的证明其实无甚可翻新的花样,但是题还是很有意思的.比如 codeforces - 1332E Height All the Same 这个. 首先给出几 ...

  4. C++ 重载运算符在HotSpot VM中的应用

    C++支持运算符重载,对于Java开发者来说,这个可能比较陌生一些,因为Java不支持运算符重载.运算符重载本质上来说就是函数重载.下面介绍一下HotSpot VM中的运算符重载. 1.内存分配与释放 ...

  5. c语言代码练习6

    //输入三个数字,依次按照从大到小输出#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int a = 0 ...

  6. Docker Compose V2 安装常用数据库MySQL+Mongo

    前言 书接上篇:Docker V24 及 Docker Compose V2 的安装及使用 本篇操作都在 centos8 虚拟机 devops01 中进行,并都归属网络:devopsnetwork 主 ...

  7. Android Orm框架(GreenDao)

    Android Orm框架(GreenDao) 分类: android2014-04-10 14:29 723人阅读 评论(0) 收藏 举报 GreenDao与Ormlite对比 Ormlite:简单 ...

  8. ERROR: Command errored out with exit status 1:

    Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting CairoSVG==2.4.2 Using cached ...

  9. 如何使用webgl(three.js)实现煤矿隧道、井下人员定位、掘进面、纵采面可视化解决方案——第十九课(一)

    序: 又是很久没有更新文章了,这次索性将之前做的三维煤矿项目拿出来讲讲,一是回顾技术,二是锻炼一下文笔. 随着科技的不断发展,越来越多的人开始关注煤矿采集的安全和效率问题.为了更好地展示煤矿采集的过程 ...

  10. np.random.uniform()

    np.random.uniform(start,end,second) start:开始数 end:结束数 second:次数,也就是选择几次. 代码结果如下所示: import numpy as n ...