Finding pairs

2.6

18 votes
Algorithms, Depth First Search, Graphs
Problem

You are given a rooted tree with N nodes and node 1 as a root. There is a unique path between any two nodes. Here, d(i,j) is defined as a number of edges in a unique path between nodes i and j.

You have to find the number of pairs (i, j) such that and d(i,j)=d(i,1)d(j,1).

Input format

  • The first line contains n denoting the integer.
  • Next n1 lines denote the edges of the tree.

Output format

Print a single integer denoting the number of pairs (i, j) such that and d(i,j)=d(i,1)d(j,1).

Constraints

1N105

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

All pairs follow the given condition: (1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4).

Editor Image

?