The Extra Node

View as PDF

Submit solution

Points: 17 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem type

Given two trees A and B, the tree A has N nodes, numbered from 1 to N, while the tree B has N+1 nodes, numbered from 1 to N+1. Bob found that if he removes one node from the tree B, the two trees will have the same structure. Can you help Bob find the extra node in the tree B? If there are multiple nodes in the tree B which can sastify the requirement, output the minimum one in tree B.

Input Specification

The first line of input contains an integer N (1 \le N \le 10^5).

Each of the following N-1 lines contains two integers u and v (1 \le u, v \le N), indicating an edge in the tree A.

Each of the following N lines contains two integers u and v (1 \le u, v \le N+1), indicating an edge in the tree B.

Output Specification

Output one integer, the index of the extra node in tree B so that the two trees will be structurally identical after removing the extra node. If there are multiple answers, output the one with the minimum number in tree B.

Constraints

Subtask Points Additional constraints
1 20 N \le 50.
2 30 N \le 2\,000.
3 50 No additional constraints.

Sample Input

5
1 2
2 3
1 4
1 5
1 2
2 3
3 4
4 5
3 6

Sample Output

1

Comments

There are no comments at the moment.