You are not logged in. Please login at to post your questions!


JABO - Editorial






Disjoint Sets, Simple Math


You are given a Jam Board with R rows and C columns. A row of the jambaord contains a group of 5 points arranged vertically, which are connected with the same metal plate. So you have a grid of 5*R rows and C columns.

You are to simulate the following 4 operations

  • Add a wire connecting two grid points.
  • Give potential to a grid point.
  • Remove potential from a grid point.
  • Check if there is potential difference between two grid points. (Since potential difference leads to LED being on)


This would be an entirely different problem if we were to allow for removal of a wire that connects two grid points. Since that is not allowed, the problem reduces to maintaining disjoint sets of groups. Also known as Union Find. We have to make a tiny modification though - to maintain the number of sources providing potential to each group.

The number of sources are easy to maintain.

  • Augment the Join method in your Disjoing Sets data structure to add and store the potential on the root for the whole group.
  • Addition and Removal of a source of potential for a group are simply performed at the root of the group.


Let us assume U is a disjoint set data structure.

  • U.init(N) creates a node N and assigns it an independent group.
  • U.Root(N) returns a unique identifier for the group to which N belongs.
  • U.join(A,B) joins the groups which contain A, an which contain B respectively.
    • After calling U.join(A,B), U.Root(A) will be equal to U.Root(B)

We know that the amortized complexity of performing T operations of U is O(U * iA(N)), where iA is the inverse of the Ackermann function (and practivally very very small for very large values of N as well).

We use U and build aU, which helps us maintain the number of sources without any additional complexity.

procedure: aU.init(N)       // O(1)
    U.init(N)                   // O(1)
    sources[N] = 0              // O(1)

aU.Root(N) simply returns U.Root(N). Of course, we do not have to do any additional calculations for maintaining the number of sources while finding the Root.

We add the number of sources of potential in the Join method.

procedure: aU.join(A,B)
    if U.Root(A) has become parent of U.Root(B)
        sources[ U.Root(A) ] += sources[ U.Root(B) ]
        sources[ U.Root(B) ] += sources[ U.Root(A) ]

Now, addition / deletion and lookup of the number of sources of potential on a grid node N can be done by incrementing / decrementing and lookup of sources[ aU.Root(N) ]

It remains to mention that considering all grid nodes as separate groups - and joining groups of 5 initially - may create massive blobs of in-memory arrays that might cause thrashing (and slow code). Thus, you can optimize your code by calculating the row-number that a given grid point belongs to and working on the that number. Now, there can be at most 500*2500 sets.


Can be found here.


Can be found here.

This question is marked "community wiki".

asked 12 Nov '12, 18:46

gamabunta's gravatar image

accept rate: 14%

edited 12 Nov '12, 18:46

i have done exactly the same thing. Can you point out the error in this solution.

(13 Nov '12, 18:49) anshumanrai2★

I'm interested - if we can remove wires is there elegant solution for this?


answered 12 Nov '12, 18:49

betlista's gravatar image

3★betlista ♦♦
accept rate: 11%

(12 Nov '12, 21:12) svm112★

[This doesn't fits in comment so I am posting it as answer]

@saikrishna173 : You are getting run time error (SIGSEGV to be exact), because you are allocating too much memory on stack.

set B[500][2500];

This line of yours is the source of error.

Solutions -

  1. Allocate this 2D array on heap. Just move this line out of main()
  2. You can also use 'new' operator to allocate on heap. Be sure to deallocate using 'delete'.
  3. Use std::vector. It is the best solution, as it is the most C++ish way of doing it.

Some might ask why did it compile in first place. I think, that's because compiler doesn't know how much stack memory would be available.

And don't think the reason of error was using too much memory, you didn't.


answered 13 Nov '12, 16:26

vinayak%20garg's gravatar image

4★vinayak garg
accept rate: 18%

@vinayak garg still getting run time error.Can you change my submission and check the submission??

(13 Nov '12, 18:37) saikrishna1733★

@saikrishna173 I checked your code. It is giving SIGSEGV in set* head(set& A) function. I couldn't fix it, because it is related to your logic. I suggest you review your logic, and rewrite a simpler code this time. Your code is rather difficult to read.

(15 Nov '12, 22:55) vinayak garg4★

@vinayak garg Thanks a lot sir..!! Is it the case with every test case or few test cases

(21 Nov '12, 03:29) saikrishna1733★

@betlista how much memory can we allocate because in a tutorial of lucky9 it's been said that we could allocate int[5000][5000] memory


answered 14 Nov '12, 18:58

pankajb23's gravatar image

accept rate: 0%


You are absolutely correct, that's the problem with @saikrishna173's code, when I moved set B[500][2500]; before main it is without RE

I do not know exact size of stack, here is written, that default is 8MB (on linux) -

(14 Nov '12, 19:01) betlista ♦♦3★

@belista can u give the url of the specified code

(14 Nov '12, 20:16) pankajb232★

Yet again it doesn't fit in comment.

@saikrishna173: I tested it against one test file only. The SIGSEGV you are getting is not because of corner case. It is failing on average case.

One reason I can think of error - You are using vector (as suggested by me), and you are storing the pointers of its elements. Don't assume anything about underlying implementation of vectors. You should use iterators to access its elements, not pointers.

As I have said before, you need to simplify. To store "parent", instead of using pointer, you should use a integer offset.

Some general comments (not related to RE)-

  1. Use scanf instead of your own input function.
  2. Macros - Never use them.
  3. Indentation- Please indent using A-Style or K&R style.
  4. Avoid unnecessary usage of pointers

answered 21 Nov '12, 16:47

vinayak%20garg's gravatar image

4★vinayak garg
accept rate: 18%

edited 21 Nov '12, 18:07

Thanks a lot....!!!

(22 Nov '12, 19:17) saikrishna1733★ Can anyone help me why i am getting run time error with this code? Thanks in advance


answered 13 Nov '12, 14:49

saikrishna173's gravatar image

accept rate: 9%

I think, that correct output for

2 2 2

is ON, what returns your code? On ideone it fails on TLE - .

(13 Nov '12, 19:17) betlista ♦♦3★

@betlista My code returns ON. And by the way i am unable to open the url u posted.

(14 Nov '12, 18:11) saikrishna1733★

@saikrishna173 that's because there was dot at the end...

on my PC your code is not running, without input it fails on SIGSEGV too

(14 Nov '12, 18:45) betlista ♦♦3★

see @pankajb23's answer, you have problem with memory, move set before main

(14 Nov '12, 19:02) betlista ♦♦3★

@betlista Sorry check this submission. Even then I am getting TLE,i tried moving set before main also.

(14 Nov '12, 21:42) saikrishna1733★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 12 Nov '12, 18:46

question was seen: 7,950 times

last updated: 22 Nov '12, 19:17