#LCA0000089. Thêm một cạnh và đi đúng số bước (1-Trees and Queries)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Thêm một cạnh và đi đúng số bước (1-Trees and Queries)
1-Trees and Queries
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
An -vertex tree receives independent queries . Temporarily insert a new undirected edge absent from the original tree, and determine whether a walk from to using exactly edges exists. The walk may revisit vertices and edges. Remove the inserted edge after each query.
Input
First line: , then edge lines . Then , followed by queries .
Output
Print YES if a walk exists, otherwise NO. Output is case-insensitive.
Subtasks
-
Subtask 1 (20 points): ; ; ; , ; new edge is not an original edge; ; original edges form a tree.
-
Subtask 2 (30 points): ; ; ; , ; new edge is not an original edge; ; original edges form a tree.
-
Subtask 3 (50 points): ; ; ; , ; new edge is not an original edge; ; original edges form a tree.
Examples
Input
5
1 2
2 3
3 4
4 5
5
1 3 1 2 2
1 4 1 3 2
1 4 1 3 3
4 2 3 3 9
5 2 3 3 9
Output
YES
YES
NO
YES
NO
Explanation
Query 1 uses new edge then , exactly two steps. Query 4 can repeat a cycle to reach nine steps. Query 3 cannot reach the destination in exactly three steps.