atcoder#AGC027F. [AGC027F] Grafting
[AGC027F] Grafting
Score : points
Problem Statement
Snuke has found two trees and , each with vertices numbered to . The -th edge of connects Vertex and , and the -th edge of connects Vertex and . Also, all vertices of are initially painted white.
Snuke would like to perform the following operation on zero or more times so that coincides with :
- Choose a leaf vertex that is painted white. (Let this vertex be .)
- Remove the edge incident to , and add a new edge that connects to another vertex.
- Paint black.
Determine if can be made to coincide with , ignoring color. If the answer is yes, find the minimum number of operations required.
You are given cases of this kind. Find the answer for each of them.
Constraints
- All given graphs are trees.
Input
Input is given from Standard Input in the following format:
Each case is given in the following format:
Output
For each case, if can be made to coincide with ignoring color, print the minimum number of operations required, and print -1
if it cannot.
2
3
1 2
2 3
1 3
3 2
6
1 2
2 3
3 4
4 5
5 6
1 2
2 4
4 3
3 5
5 6
1
-1
- The graph in each case is shown below.
- In case , can be made to coincide with by choosing Vertex , removing the edge connecting and , and adding an edge connecting and . Note that Vertex is not a leaf vertex and thus cannot be chosen.
3
8
2 7
4 8
8 6
7 1
7 3
5 7
7 8
4 2
5 2
1 2
8 1
3 2
2 6
2 7
4
1 2
2 3
3 4
3 4
2 1
3 2
9
5 3
4 3
9 3
6 8
2 3
1 3
3 8
1 7
4 1
2 8
9 6
3 6
3 5
1 8
9 7
1 6
6
0
7
- may coincide with from the beginning.