0% found this document useful (0 votes)
38 views1 page

Data Structure Question

You are a renowned con planning an escape route from cities connected as nodes in a tree. You are given the number of cities (N) and distance between pairs of cities (K). The input describes routes connecting cities without repetition. The output is the number of unique city pairs separated by exactly K route units. For the sample, N=6, K=3, and the number of qualifying pairs is 4.

Uploaded by

rivu rockstar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views1 page

Data Structure Question

You are a renowned con planning an escape route from cities connected as nodes in a tree. You are given the number of cities (N) and distance between pairs of cities (K). The input describes routes connecting cities without repetition. The output is the number of unique city pairs separated by exactly K route units. For the sample, N=6, K=3, and the number of qualifying pairs is 4.

Uploaded by

rivu rockstar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

You are a world-renowned con. It’s time to theft.

You marked N number of cities around India in


such a manner that when all the routes between the cities are connected, it resembles as that of
a tree where the cities are nodes and the edges are routes. And also, all the routes between the
two cities are of the same length, in here take it as 1 unit. As you are trying to figure out the
escape routes, you want to know how many cities are separated by K units through those
routes. Note that pairs (v, u) and (u, v) are considered to be the same pair.
Input:
The first line contains two integers N and K (1 ≤ N ≤ 50000, 1 ≤ K ≤ 500) — the number of cities
and the required distance between the two cities.
Next N - 1 lines describe the routes which connect the two cities as "ai bi" (without the quotes)
(1 ≤ ai, bi ≤ n, ai ≠ bi), where ai and bi are the cities connected by the i-th route. All given routes
are different.
Output:
Print a single integer — the number of distinct pairs of the cities which have a distance of
exactly K between them.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use
the cin, cout streams or the %I64dspecifier.
Sample Input Case:
63
12
13
41
53
63

Sample Output Case:


4
Explanation:
After plotting the cities as the trees we can see there are 4 pairs of cities, I.e. (2,5) , (2,6) , (4,5),
(4,6) whose route have exactly 3 units of length between them.

You might also like