No.363 Kadomatsu Cycle
タグ : / 解いたユーザー数 30
作問者 :
koyumeishi
/ テスター :
Problem Statement
A Kadomatsu sequence is a sequence $A_1, A_2, A_3$ consisting of $3$ elements that satisfies the following 2 conditions.
- $A_1, A_2, A_3$ are all distinct
- Among the $3$ elements, $A_2$ is the largest or $A_2$ is the smallest
For a graph in which vertex $i$ has value $Ai$, a cycle is called a Kadomatsu cycle if it satisfies the following 2 conditions.
- The cycle has 3 or more vertices
- Letting $u,v,w$ denote 3 consecutive vertices on the cycle, for all $u,v,w$, the sequence $A_u,A_v,A_w$ is a Kadomatsu sequence
There is a tree with $N$ vertices in which vertex $i$ has value $Ai$.
Adding a new edge connecting vertex $u$ and vertex $v$ ($u \neq v$) to this tree creates exactly one cycle.
Is the cycle created in this way a Kadomatsu cycle?
Answer this for each of the $Q$ edges.
Print "YES" if it is a Kadomatsu cycle and "NO" otherwise, one per line.
Input
$N$
$A_1$ $A_2$ $\dots$ $A_N$
$x_1$ $y_1$
$x_2$ $y_2$
$\vdots$
$x_{N-1}$ $y_{N-1}$
$Q$
$u_1$ $v_1$
$u_2$ $v_2$
$\vdots$
$u_Q$ $v_Q$
The 1st line gives the number of vertices $N$ of the tree.
The 2nd line gives the values $A_i$ of the vertices $i$, separated by spaces.
The following $N-1$ lines, starting from the 3rd line, give the edges. The $i$-th edge connects vertex $x_i$ and vertex $y_i$.
Line 3+N-1 gives the number $Q$ of edges to be newly added.
The following $Q$ lines, starting from line 3+N, give the edges to be newly added. The $i$-th edge connects vertex $u_i$ and vertex $v_i$.
Each of the $Q$ edges is added to the tree independently. The edge added $i$-th does not affect the graph for the $j$-th edge ($i \neq j$).
All input values are integers and satisfy the following constraints.
$2 \leq N \leq 10^5$
$1 \leq A_i \leq 10^5$
$1 \leq x_i,y_i \leq N$
$x_i \neq y_i$
The tree is guaranteed to be connected and to have no multiple edges or self-loops.
$1 \leq Q \leq 10^5$
$1 \leq u_i, v_i \leq N$
$u_i \neq v_i$
Since the amount of input and output is large, using the fastest possible I/O is recommended. Of the 4-second execution time limit, 2 seconds are intended to be for I/O.
Output
For each edge, print on one line "YES" if the cycle created by adding the new edge to the tree is a Kadomatsu cycle, and "NO" otherwise.
Sample
Sample 1
Input
7 1 5 4 6 3 4 3 1 2 2 3 3 4 2 5 1 6 6 7 4 4 5 4 7 5 7 3 5
Output
YES YES NO NO
The figure below shows the edges added to this tree. Note that each edge is added independently.
Sample 2
Input
7 1 5 4 6 3 4 3 1 2 2 3 3 4 4 5 5 6 6 7 5 1 2 1 5 3 7 6 1 1 4
Output
NO NO NO YES YES
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。