Finding pairs
Practice
2.6 (18 votes)
Algorithms
Depth first search
Graphs
Problem
89% Success 3243 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code
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 \(n - 1\) 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
\(1 \le N \le 10^5\)
Submissions
Please login to view your submissions
Similar Problems
Points:20
39 votes
Tags:
AlgorithmsApprovedBreadth First SearchEasyGraphsOpen
Points:20
33 votes
Tags:
AlgorithmsDepth First SearchEasyGraphsTrees
Points:20
121 votes
Tags:
ApprovedEasyMathNumber theory
Editorial