atcoder#AGC014E. [AGC014E] Blue and Red Tree

[AGC014E] Blue and Red Tree

Score : 14001400 points

Problem Statement

There is a tree with NN vertices numbered 11 through NN. The ii-th of the N1N-1 edges connects vertices aia_i and bib_i.

Initially, each edge is painted blue. Takahashi will convert this blue tree into a red tree, by performing the following operation N1N-1 times:

  • Select a simple path that consists of only blue edges, and remove one of those edges.
  • Then, span a new red edge between the two endpoints of the selected path.

His objective is to obtain a tree that has a red edge connecting vertices cic_i and did_i, for each ii.

Determine whether this is achievable.

Constraints

  • 2N1052 \leq N \leq 10^5
  • 1ai,bi,ci,diN1 \leq a_i,b_i,c_i,d_i \leq N
  • aibia_i \neq b_i
  • cidic_i \neq d_i
  • Both input graphs are trees.

Input

Input is given from Standard Input in the following format:

NN

a1a_1 b1b_1

:

aN1a_{N-1} bN1b_{N-1}

c1c_1 d1d_1

:

cN1c_{N-1} dN1d_{N-1}

Output

Print YES if the objective is achievable; print NO otherwise.

3
1 2
2 3
1 3
3 2
YES

The objective is achievable, as below:

  • First, select the path connecting vertices 11 and 33, and remove a blue edge 121-2. Then, span a new red edge 131-3.
  • Next, select the path connecting vertices 22 and 33, and remove a blue edge 232-3. Then, span a new red edge 232-3.
5
1 2
2 3
3 4
4 5
3 4
2 4
1 4
1 5
YES
6
1 2
3 5
4 6
1 6
5 1
5 3
1 4
2 6
4 3
5 6
NO