#P3806. Existence of a Pair at Distance k in a Tree
Existence of a Pair at Distance k in a Tree
Existence of a Pair at Distance k in a Tree
You are given a tree with n nodes. A tree is an undirected connected acyclic graph. In addition, you are given an integer k. Your task is to determine whether there exists a pair of distinct nodes whose distance is exactly k.
The distance between two nodes is defined as the number of edges in the unique path connecting them. In particular, for any two nodes u and v, the distance is the length of the path connecting u and v.
Formally, given a tree with n nodes and an integer k, check if there exist two distinct nodes u and v such that:
If such a pair exists, output YES; otherwise, output NO.
inputFormat
The first line contains two integers n and k — the number of nodes in the tree and the required distance, respectively.
Each of the following n - 1 lines contains two integers u and v, indicating that there is an edge between node u and node v in the tree. It is guaranteed that the given graph is a tree.
Note: The nodes are numbered from 1 to n.
outputFormat
Output a single line containing YES if there exists a pair of nodes whose distance is exactly k, and NO otherwise.
sample
2 1
1 2
YES