atcoder#ARC144E. [ARC144E] GCD of Path Weights
[ARC144E] GCD of Path Weights
Score : points
Problem Statement
You are given a directed graph with vertices and edges. The vertices are numbered . The -th edge is directed from Vertex to Vertex , where .
The beautifulness of a sequence of positive integers is defined as the maximum positive integer that satisfies the following:
- For every path () from Vertex to Vertex in , is a multiple of .
You are given an integer sequence . Find the maximum beautifulness of a sequence of positive integers such that . If the maximum beautifulness does not exist, print -1
.
Constraints
- if
- In the given graph , there is a path from Vertex to Vertex .
- or
Input
Input is given from Standard Input in the following format:
Output
Print the maximum beautifulness of a sequence of positive integers . If the maximum beautifulness does not exist, print -1
.
4 4
1 2
1 3
2 4
3 4
-1 3 7 -1
4
There are two paths from Vertex to Vertex : and . For instance, has a beautifulness of . Indeed, both and are multiples of .
4 5
1 2
1 3
2 4
3 4
1 4
-1 3 7 -1
1
There are three paths from Vertex to Vertex : , , and . For instance, has a beautifulness of .
4 4
1 2
1 3
2 4
3 4
3 -1 -1 7
-1
For instance, has a beautifulness of . Since you can increase the beautifulness of as much as you want, there is no maximum beautifulness.
5 5
1 3
3 5
2 3
3 4
1 4
2 -1 3 -1 4
9