A pentagon problem
Practice
4.5 (2 votes)
Graphs
Dynamic programming
Algorithms
Trees
Problem
73% Success 372 Attempts 50 Points 5s Time Limit 256MB Memory 1024 KB Max Code
You are given a tree consisting of $$n$$ nodes. You are allowed to add an edge between any two nodes of the tree. Now, you are required to find the number of total distinct pentagons possible by adding an edge between any two nodes.
The side of the pentagon is defined as an edge between two nodes of the tree.
A tree is a connected graph without cycles.
Note: A polygon is unique if at least one side is different.
Since the answer can be very large, print it modulo \(10^9+7\).
Input format
- The first line of each test case contains an integer $$N$$ denoting the number of nodes in the tree.
- Each of the next $$N-1$$ lines contains two nodes say $$U$$ and $$V$$ denoting an edge between them.
Output format
Print a single integer denotes the number of pentagons possible.
Constraints
\(1 ≤ N ≤ 300000\\ 1 \le U, V \le N\)
Sample Input
6
1 2
2 3
3 4
4 5
5 6
Sample Output
2
Submissions
Please login to view your submissions
Similar Problems
Points:50
21 votes
Tags:
Dynamic ProgrammingAlgorithmsTreesMath
Points:50
Tags:
Medium-HardTrees
Points:50
Tags:
TreeDynamic ProgrammingAlgorithmsMedium-Hard
Editorial