#GL0000397. GCD trên cây sau mỗi cập nhật (GCD on Tree After Updates)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 vertices, with a positive integer assigned to vertex . A tree is considered good if the GCD of the values at all its vertices equals .
You are given point updates. Each update sets . 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: .
- Second line: integers .
- Next lines: undirected edges , forming a tree.
- Next line: .
- Next lines: pairs indicating assignments .
Output
After each update, print YES if a suitable edge exists, otherwise print NO.
Subtasks
-
Subtask 1 (20 points): , , , valid tree and update indices.
-
Subtask 2 (30 points): , , , valid tree and update indices.
-
Subtask 3 (50 points): , , , valid tree and update indices.
For every subtask, edge endpoints and update indices are in 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 . After the first update, the values are , so neither singleton component has GCD . After the second and third updates, at least one singleton has value . After the last update, both values equal , 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 has value . After the second update, both vertices and have value . After the third update, the component has GCD and can be separated by deleting edge .