Prime Path
Practice
4.2 (8 votes)
Algorithms
Depth first search
C++
Graphs
Problem
61% Success 1460 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code
Given a tree with \(N\) nodes numbered \(1\) to \(N\) and \(N - 1\) edges, rooted at node \(1\). Find the number of unordered pair of distinct vertices \((i,j)\) such that there exists exactly one prime number on the simple path between vertex \(i\) and \(j\).
Note:
- We only need to consider unordered pairs i.e. \((i,j) \) and \((j,i) \) are counted once.
- The values of nodes in the path will be the node numbers.
Input format
- The first line contains \(N\) denoting the number of vertices in the tree.
- The next \(N - 1\) lines contain two space-separated integers \(u\), \(v\) denoting an edge between vertex \(u \) and \(v\).
Output format
Print a single integer, denoting the number of such pairs \((i,j)\).
Constraints
\(1 \le N \le 10^5 \\ 1 \le u, v \le N\)
Submissions
Please login to view your submissions
Similar Problems
Points:50
5 votes
Tags:
GraphsAlgorithmsDepth First Search
Points:50
Tags:
AlgorithmsDepth First SearchGraphsHard
Points:50
2 votes
Tags:
AlgorithmsGraphsBinary SearchDepth First Search
Editorial