Light Up the Bulbs
Practice
4.1 (14 votes)
Disjoint data structures
Data structures
Basics of disjoint data structures
Problem
87% Success 2328 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Demon has a warehouse and he wants to light up the warehouse by lighting N bulbs on the ceiling. Consider the ceiling as a coordinate axis and Demon knows the X - Y coordinates of the bulbs that need to be put at the ceiling. Now the bulbs need to be connected with each other using wires. Demon wants to know the minimum sum of the square of the length of wires required to connect the bulbs.

Note: If bulb A and B are connected, bulb B and bulb C are connected, then bulb A and C are also connected.

Input Format:
The first line contains an integer N - the number of bulbs needed to light up the warehouse. The next N lines contain two space-separated integers x and y - representing the coordinates of the bulb.

Output Format:
Output the minimum length of wire required to connect the light bulbs.

Constraints:
1≤N≤1000
0≤x,y≤1000

Sample Input
3
0 10 
10 0
0 0
Sample Output
200
Explanation

We can connect the bulbs A(10, 0) and B(0, 0) which will require the length of 10 units. Similarly, we can connect the bulbs B(0, 0) and C(0, 10) which will require the length of 10 units. Therefore, the total length required to connect the bulbs is 102 + 102 = 200 units.

Code Editor

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Submissions
Please login to view your submissions
Similar Problems
Points:30
12 votes
Tags:
Arrays and StringsData StructuresDisjoint Data StructuresDisjoint SetDisjoint setGraphsMedium
Points:30
8 votes
Tags:
Data StructuresDisjoint Data StructuresDisjoint SetDisjoint setImplementationMedium
Points:30
4 votes
Tags:
Data StructuresDisjoint setMediumQueue
Editorial

Login to unlock the editorial