PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000397. GCD trên cây sau mỗi cập nhật (GCD on Tree After Updates)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

GCD trên cây sau mỗi cập nhật (GCD on Tree After Updates)

GCD on Tree After Updates

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected tree has nn vertices, with a positive integer aia_i assigned to vertex ii. A tree is considered good if the GCD of the values at all its vertices equals 11.

You are given qq point updates. Each update i xi\ x sets ai=xa_i=x. Immediately after applying each update, determine whether there exists an edge whose removal splits the original tree into exactly two nonempty connected components, at least one of which is good.

You may choose any single edge to remove for each query; the updates are persistent, and the tree structure never changes. Both components need not be good.

Input

  • First line: nn.
  • Second line: nn integers a1,…,ana_1,\ldots,a_n.
  • Next n−1n-1 lines: undirected edges u,vu,v, forming a tree.
  • Next line: qq.
  • Next qq lines: pairs i,xi,x indicating assignments ai←xa_i\leftarrow x.

Output

After each update, print YES if a suitable edge exists, otherwise print NO.

Subtasks

  • Subtask 1 (20 points): 2≤n≤302\le n\le30, 1≤q≤301\le q\le30, 1≤ai,x≤1001\le a_i,x\le100, valid tree and update indices.

  • Subtask 2 (30 points): 2≤n≤5 0002\le n\le5\,000, 1≤q≤5 0001\le q\le5\,000, 1≤ai,x≤1091\le a_i,x\le10^9, valid tree and update indices.

  • Subtask 3 (50 points): 2≤n≤100 0002\le n\le100\,000, 1≤q≤100 0001\le q\le100\,000, 1≤ai,x≤10181\le a_i,x\le10^{18}, valid tree and update indices.

For every subtask, edge endpoints and update indices are in [1,n][1,n] and endpoints of an edge are distinct.

Examples

Example 1 (official)

Input

2
2 2
1 2
4
1 3
2 1
1 2
2 2

Output

NO
YES
YES
NO

Explanation

There is a single edge 1−21-2. After the first update, the values are (3,2)(3,2), so neither singleton component has GCD 11. After the second and third updates, at least one singleton has value 11. After the last update, both values equal 22, and no valid cut exists.

Example 2 (additional)

Input

3
6 10 15
1 2
2 3
3
1 1
2 1
1 6

Output

YES
YES
YES

Explanation

After the first update, the singleton vertex 11 has value 11. After the second update, both vertices 11 and 22 have value 11. After the third update, the component {2,3}\{2,3\} has GCD gcd⁡(1,15)=1\gcd(1,15)=1 and can be separated by deleting edge 1−21-2.