Given an integer N, the task is to construct a tree such that sum of for all ordered pairs (u, v) is maximum where u != v. Print the maximum possible sum.
Input: N = 4 Output: 26 1 / 2 / 3 / 4 For node 1, 1*2 + 1*2 + 1*1 = 5 For node 2, 2*1 + 2*2 + 2*1 = 8 For node 3, 2*1 + 2*2 + 2*1 = 8 For node 4, 1*1 + 1*2 + 1*2 = 5 Total sum = 5 + 8 + 8 + 5 = 26 Input: N = 6 Output: 82
Approach: We know that sum of the degree of all nodes in a tree is (2 * N) – 2 where N is the number of nodes in the tree. As we have to maximize the sum so we have to minimize the number of leaf nodes as the leaf nodes have the minimum degree among all the nodes of the tree and the tree will be of the form:
1 / 2 / ... / N
where only the root and the only leaf node will have a degree of 1 and all the other nodes will have degree 2.
Below is the implementation of the above approach:
- Number of trees whose sum of degrees of all the vertices is L
- Minimum Operations to make value of all vertices of the tree Zero
- Possible edges of a tree for given diameter, height and vertices
- Make a tree with n vertices , d diameter and at most vertex degree k
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Sum of pairwise products
- Maximum Sum of Products of Two Arrays
- Count of distinct graphs that can be formed with N vertices
- Angle between 3 given vertices in a n-sided regular polygon
- Number of Simple Graph with N Vertices and M Edges
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices
- Find two vertices of an isosceles triangle in which there is rectangle with opposite corners (0, 0) and (X, Y)
- Find the area of quadrilateral when diagonal and the perpendiculars to it from opposite vertices are given
- Number of occurrences of a given angle formed using 3 vertices of a n-sided regular polygon
- Number of triangles formed by joining vertices of n-sided polygon with one side common
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.