New Start day 9: 08/10/2020 Codechef October Long DDIMMST* (note included)

question (source:https://www.codechef.com/OCT20B/problems/DDIMMST)

D-Dimensional MST

You are given  points in -dimensional space. The  point has  coordinates - 

Consider a weighted undirected complete graph with these N points as its vertices. The weight of the edge between points  and is 

Find the weight of the maximum spanning tree of this graph.

Input:

  • The first line contains two integers 
  • The i-th of the next  lines contains a description of the i-th point. It has  integers: 

Output:

Output a single line which contains a single integer - the Maximum Spanning Tree Weight

Constraints




Subtasks

  • 10 points, N5000

  • 90 points, 

Sample Input:

2 2  
1 1  
2 2  

Sample Output:

2

My Logic:

One can observe that the given points form a complete graph, i.e. every point is connected to each other, and the weight of each edge is calculated by the function given in the question.

We will try to do the subtask for the question since the constrains look a bit large. We just need to find the shape of the spanning tree that gives us the maximum spanning tree. Then we can answer for full size input.

For Subtask

At first, I thought that taking the distance between each point from the furthest point would do the trick. For this, I did this. For every point, I took the distance from it to each point. I thought this would give me a maximum spanning tree. But it is giving the wrong answer for some reason.

Now I will try using Kruskal's algorithm to find the minimum spanning tree using negative weights that will give me the maximum spanning tree.

Negative Krukshal giving Time limit. 

After analysis of Time taking processes, I eliminated vectors and used pairs instead and that helped a lot. My subtasks are done.

important note* Use pairs instead of vectors to store small segments. Much faster.

Also, the edge process which is of O(n^2) is not taking as much time for 100points.

Comments

Popular posts from this blog

LeetCode 3Sum

New start Day 2 (1/10/2020) codeforces 492B Vanya and Lantern

Heap (min)