atcoder#AGC014E. [AGC014E] Blue and Red Tree
[AGC014E] Blue and Red Tree
Score : points
Problem Statement
There is a tree with vertices numbered through . The -th of the edges connects vertices and .
Initially, each edge is painted blue. Takahashi will convert this blue tree into a red tree, by performing the following operation 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 and , for each .
Determine whether this is achievable.
Constraints
- Both input graphs are trees.
Input
Input is given from Standard Input in the following format:
:
:
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 and , and remove a blue edge . Then, span a new red edge .
- Next, select the path connecting vertices and , and remove a blue edge . Then, span a new red edge .
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