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

#LCA0000089. Thêm một cạnh và đi đúng số bước (1-Trees and Queries)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

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 nn-vertex tree receives independent queries (x,y,a,b,k)(x,y,a,b,k). Temporarily insert a new undirected edge x−yx-y absent from the original tree, and determine whether a walk from aa to bb using exactly kk edges exists. The walk may revisit vertices and edges. Remove the inserted edge after each query.

Input

First line: nn, then n−1n-1 edge lines u,vu,v. Then qq, followed by qq queries x,y,a,b,kx,y,a,b,k.

Output

Print YES if a walk exists, otherwise NO. Output is case-insensitive.

Subtasks

  • Subtask 1 (20 points): 3≤n≤353\le n\le 35; 1≤q≤351\le q\le 35; 1≤u,v,x,y,a,b≤n1\le u,v,x,y,a,b\le n; u≠vu\ne v, x≠yx\ne y; new edge x−yx-y is not an original edge; 1≤k≤1091\le k\le10^9; original edges form a tree.

  • Subtask 2 (30 points): 3≤n≤12003\le n\le 1200; 1≤q≤12001\le q\le 1200; 1≤u,v,x,y,a,b≤n1\le u,v,x,y,a,b\le n; u≠vu\ne v, x≠yx\ne y; new edge x−yx-y is not an original edge; 1≤k≤1091\le k\le10^9; original edges form a tree.

  • Subtask 3 (50 points): 3≤n≤1000003\le n\le 100000; 1≤q≤1000001\le q\le 100000; 1≤u,v,x,y,a,b≤n1\le u,v,x,y,a,b\le n; u≠vu\ne v, x≠yx\ne y; new edge x−yx-y is not an original edge; 1≤k≤1091\le k\le10^9; 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 1−31-3 then 3−23-2, exactly two steps. Query 4 can repeat a cycle to reach nine steps. Query 3 cannot reach the destination in exactly three steps.