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. 面霸的自我修养:synchronized专题

    王有志,一个分享硬核Java技术的互金摸鱼侠 加入Java人的提桶跑路群:共同富裕的Java人 今天是<面霸的自我修养>的第3弹,内容是Java并发编程中至关重要的关键字synchroni ...

  2. Unity 性能优化之Shader分析处理函数ShaderUtil.HasProceduralInstancing: 深入解析与实用案例

    Unity 性能优化之Shader分析处理函数ShaderUtil.HasProceduralInstancing: 深入解析与实用案例 点击封面跳转到Unity国际版下载页面 简介 在Unity中, ...

  3. win如何根据端口号查找并杀死一个线程

    查看端口占用 netstat -ano | findstr "端口号" 杀死一个进程 taskkill /pid 进程号 -f

  4. Java 21 新特性:Unnamed Classes and Instance Main Methods

    Java 21引入了两个语言核心功能: 未命名的Java类你说 新的启动协议:该协议允许更简单地运行Java类,并且无需太多样板 下面一起来看个例子.通常,我们初学Java的时候,都会写类似下面这样的 ...

  5. Python基础——字符编码、文件处理

    文章目录 字符编码 一 引入 二 知识储备 2.1 三大核心硬件 2.2 文本编辑器读取文件内容的流程 2.3 python解释器执行文件的流程 2.4 总结 三.字符编码介绍 3.1 什么是字符编码 ...

  6. Xshell远程连接、MBR/BOOT和GRUB三者关系总结(系统启动过程)

    远程连接 远程连接Linux服务器的常见工具有Xshell.SecureCRT.Putty等,这些客户端连接工具在Linux服务器对应着相同SSH服务进程sshd,即远程连接都是使用SSH协议,当然它 ...

  7. C转C++ 个人总结

    # C转C++ 个人总结 1.使用C++的好处 2.using namespace std 3.cin和cout #include<iostream> //必备的头文件 using nam ...

  8. vue 甘特图(三):甘特图右侧内容拖动展示

    vue3 甘特图(三):甘特图右侧内容拖动展示内容 解决因多个项目周期跨度不同,在一页屏幕里展示不完全,需要通过拖动甘特图下方的滚动条,去查看对应时间段内的内容 拖拽滚动视图,展示对应时间甘特图 构思 ...

  9. 基于Effect的组件设计

    Effect的概念起源 从输入输出的角度理解Effect https://link.excalidraw.com/p/readonly/KXAy7d2DlnkM8X1yps6L 编程中的Effect起 ...

  10. [CISCN 2019华东南]Web11

    看到下面connection 里面的内容有一点像抓包出来的 就抓包试试 似乎感觉也没有什么用 看到这个东西,那么就想到改IP 添加X-Forwarded-For:127.0.0.1 发现这个IP随着我 ...