GCD on directed graph

3.5

18 votes
Algorithms, Graphs, Greatest common divisor, Medium, Sieve, gcd, scc
Problem

You are given a directed graph with n nodes and m edges. The nodes are numbered from 1 to n, and node i is assigned a cost ci. The cost of a walk on the graph is defined as the greatest common divisor of the costs of the vertices used in the walk. Formally, the cost of a walk v1,v2,,vk is given by gcd(cv1,cv2,,cvk). Note that vi's need not be distinct. Find the cost of the walk with minimum cost.

The walk might consist of single vertex.

Input

  • The first line contains two integers, n, and m.
  • The next line contains n space separated integers, ith of which is equal to ci
  • Each of the next m lines contain two integers, u, and v, denoting a directed edge from u to v.

Output

  • Print one integer containing the cost of the walk with minimum cost.

Constraints

  • 1n,m,ci105
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

You can choose the walk 123 to get a gcd of 2

Editor Image

?