FIRESC - Editorial






Graph Theory, Union Find, Depth First Search


An Office has several people on which a relationship of being mutual friends is defined. Two people who are friends will always choose to accompany each other while escaping from fire.

Under these conditions, what are the maximum number of escape routes may be built such that none of them may be empty.

Also, each set of people who escape together, must have a captain assigned. Count the number of ways of assigning captains.


Let us define a graph whose vertices are people. An undirected edge connects two people who are friends.

On this graph, two vertices will be of the same color if they share an edge. This represents that the two people should have to go in the same fire escape route. We wish to maximize the number of colors used.

All the vertices in a connected component in this graph will have to be in the same color.

The above is easy to see because

  • If A and B are friends
  • and B and C are friends
  • Given that A and B should be the same color
  • and B and C should be the same color
  • A, B and C, all three should be the same color

This observation leads us to the following

The maximum number of fire escape routes that may be built is equal to the number of connected components.

The second part of the problem - Finding the ways to assign captains - is equal to the product of the number of vertices in each connected component. This can be derived by simple permutation / combination. (Since we need exactly 1 member from each set, the answer must be the product of the cardinality of the sets).


The number of connected components can be found by use of either of the following techniques

Depth First Search

Since this is an undirected graph, the number of times a Depth First Search starts from an unvisited vertex is equal to the number of connected components. We would like to store the graph as an adjacency list due to the large number of vertices (the number of edges are not too many).

Lets look at dfs implementation in pseudo-code. Both the Setter’s and Tester’s coe use DFS to solve the problem.

for u = 1 to N
    if u is not visited

    for all children v, of u
        if v is not visited

Union Find

Union Find or Disjoint Set Data structures allow you to merge two sets of items together dynamically and maintain for each item - to which set does it belongs. You can consume a Union Find data structure to join each pair of friends. At the end of this, just count the number of distinct components to get the answer.

for u = 1 to N

for e = 1 to M /* all edges */
    join-sets( e.first, e.second )

Let cc = Array of size N
for u = 1 to N
    cc[ find-set-root(u) ]++

for u = 1 to N
    if cc[u] > 0

There is one detail missing from the above two approaches. Handling of part 2 of the question. We wish to find the number of items in each connected component and find the product of these numbers.

  • In the dfs approach, maintaining a global “size” and incrementing it can solve the problem
    • or return the counts in the return value of dfs
  • In the disjoint set approach, as demonstrated in the pseudo-code, the array cc stores the number of items in each connected component at the end


Can be found here.


Can be found here.


If you’re having a problem with StackOverflow on Java, use BFS instead of DFS. I had a problem with DFS and my switch over fixed it.

Once i did the set-union of all given edges, i did a find-set (with path-compression) on all vertices. Now, the number of distinct set representatives gives the number of connected components. We can also get the cardinality of each set from this information.

Used exactly same solution in my submission as of union find , still got wrong answer

Can anyone tell where is the mistake??
Link to solution

1 Like

@gamabunta @tijoforyou Are you sure implementing with DFS would pass all test cases without TLE. Initially I did it with DFS but was getting TLE. Then I did it with disjoint sets. Yet again I got a TLE with a simple implementation of union. But when I changed my implementation to Union by Rank, I got an AC with a runtime of 1.02 sec.

Running DFS is getting TLE. Why?
Check my submissions:

Need help finding bug in my solution
Was getting WA :frowning:

I got accepted using BFS using queues…but the same code I tried to implement it with DFS using stacks it gave TLE…whereas the recursive version of DFS got AC.could you tell me the reason for the TLE…was my code going into infinite loop or just didn’t achieve the desired time limit.



Thanks in advance.

Can anyone please tell me where i went wrong?

I used disjoint sets and ackermann function to classify connected components.

I need help finding bug in my solution.
I use Union Find to solve this problem in Java, and was getting a WA.


can you tell me where my solution is failing?
my submission is

Can anyone tell me what is wrong with my approach and for which test case it fails… My submission id is

i have used same approach as gven in editorial but it is giving me wrong answer.anyone explain me…where i am wrong first in C is C SOLUTION and second is in JAVA is JAVA SOLUTION

1 Like

Hi all,

I dont know how to represent graph having large no of nodes to represent using adjacency list.
I know it requires hash table(as using linked list is not feasible here)
but i want to know how to implement.

I am getting wrong answer …
Can you give me failing case…

Can anyone plz tell me why I m getting a runtime error in my



I have used BFS instead of DFS but am getting a wrong answer. Could you please point out the mistake?

Hi, i used the Union Set Method , but for some reason it kept flagging it as Wrong Answer. I wrote a solution in C first, but thought that might be overflowing for some value, so i resorted to Python, in which overflowing will not be an issue, given the range.

My solution is here : (PYTH)

Can anyone tell me where it’s going wrong ? Thanks. I’m all out of ideas.
please tell me in which cases my code is getting WA…

please give me a case where my code gives wrong answer. I have used a different approach, but i want to know why its failing.