Confuzzle
Time limit3sMemory limit1024 MB
Given a tree whose vertices carry labels, find the minimum distance between two vertices that share the same label.
- Level
Medium7 of 10
- Topics
- Tree, DFS, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
You are given an unweighted tree with vertices. The distance between two vertices in the tree is defined as the number of edges on the path between them.
Each vertex has a number written on it, and at least two vertices are guaranteed to have the same number. Find the smallest possible distance between a pair of vertices that have the same number written on them.
Input
The first line contains the number of vertices in the tree, . ()
The second line contains integers , the distinct values of the vertices. ()
The next lines each contain two integers and describing an edge of the tree. This means that an edge connects vertex and vertex . (, )
Output
Print the smallest distance between a pair of vertices that have the same value.