#GL0000368. Ước chung lớn nhất của tổng láng giềng (Instant Noodles — GCD of Neighborhood Sums)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Ước chung lớn nhất của tổng láng giềng (Instant Noodles — GCD of Neighborhood Sums)
Instant Noodles — GCD of Neighborhood Sums
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a bipartite graph with left vertices and right vertices . Every right vertex has a positive integer weight .
For a nonempty subset of left vertices, let be the set of right vertices adjacent to at least one vertex in . Each right vertex is counted once, even if it has multiple neighbors in . Define
Find the GCD of over all nonempty subsets of the left side. If every such sum is zero, output zero.
Edges are distinct. PHOJ adaptation: unlike the original multi-test input, each input file contains one case only, without an initial test count .
Input
- Line 1: , where is the number of edges.
- Line 2: the weights of the right vertices.
- Next lines: , an edge between left vertex and right vertex .
Output
Print one integer: the GCD of the values over all nonempty left subsets .
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
All edge indices lie in and there are no duplicate edges.
Examples
Example 1
Input
2 4
1 1
1 1
1 2
2 1
2 2
Output
2
Explanation
Either left vertex reaches both right vertices, so every nonempty has . The GCD is .
Example 2
Input
4 7
36 31 96 29
1 2
1 3
1 4
2 2
2 4
3 1
4 3
Output
12
Explanation
Right vertices have left-neighbor sets , , , and respectively. Grouping equal neighbor sets gives total weights , , and . The answer is .