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

×

FIRESC - Editorial

14
2

PROBLEM LINKS

Practice
Contest

DIFFICULTY

SIMPLE

PREREQUISITES

Graph Theory, Union Find, Depth First Search

PROBLEM

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.

QUICK EXPLANATION

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).

EXPLANATION

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
        connected_components++
        dfs(u)

dfs(u)
    mark_visited(u)
    for all children v, of u
        if v is not visited
            dfs(v)

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
    initialize-new-set-with-1-item(u)

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
        connected_components++

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

SETTER'S SOLUTION

Can be found here.

TESTER'S SOLUTION

Can be found here.

This question is marked "community wiki".

asked 12 Mar '13, 06:04

gamabunta's gravatar image

1★gamabunta
2.3k128183169
accept rate: 14%

I used weighted quick union for this, which can be improved further using path compression. My solution : http://www.codechef.com/viewplaintext/1869801

(13 Mar '13, 09:38) xonix_monty2★

http://www.codechef.com/viewsolution/1939836 getting wrong ans..really pissed off :( please find the error...thanx in advance...

(15 Mar '13, 21:27) notaloser2★

I m getting SIGSEGV but i dont know why i am getting this... intializing like this mark=vector<int>(n,0) i am getting correct answer.. but when i try to do vector<int> mart(n,0) i am getting SIGSEGV.. what is wrong with this...plz answer me.. http://www.codechef.com/viewsolution/4372844

(24 Jul '14, 14:47) vishnu018233★

Please help . Getting Wrong answers for even test case!!! link to my code -> http://ideone.com/lU40Sl

link

answered 19 May '13, 18:53

al3x1784's gravatar image

2★al3x1784
041416
accept rate: 0%

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 http://www.codechef.com/viewsolution/1910690 and second is in JAVA is JAVA SOLUTION http://www.codechef.com/viewsolution/1907414

link

answered 13 Mar '13, 20:23

tarnado111's gravatar image

2★tarnado111
012
accept rate: 0%

They both have the same mistake. You do not handle possible overflow in this statement

total = ((total % N)*(count % N) % N;

As you can see, because N is 1000000007, the result of the product might exceed the range of 32-bit integers. You must cast total as long long in C to solve the issue. Cast total to long in JAVA to get AC.

(15 Mar '13, 19:23) gamabunta1★

Yes right... after changing the logic to total = ((total % N)*(count % N) % N;.. my code also worded.. huh.. http://www.codechef.com/viewsolution/1927944

(16 Mar '13, 00:54) ashishcheema3★

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.

link

answered 12 Mar '13, 08:32

kullalok's gravatar image

2★kullalok
1.5k112236
accept rate: 14%

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. http://www.codechef.com/viewsolution/1871116

link

answered 12 Mar '13, 12:36

tijoforyou's gravatar image

3★tijoforyou
4.2k52364
accept rate: 15%

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

http://www.codechef.com/viewsolution/1905165

link

answered 12 Mar '13, 13:26

sahil90's gravatar image

2★sahil90
1
accept rate: 0%

1

Replace c[p[i]]++ by c[find(i)]++
p[i] is not always the actual root of the disjoint set containing i.
Namely, after link operation.

(12 Mar '13, 16:30) anton_lunyov ♦6★

@ anton : Thanks a lot..

(13 Mar '13, 14:31) sahil902★

@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. http://www.codechef.com/viewsolution/1894308

link

answered 12 Mar '13, 13:35

rahulp0491's gravatar image

3★rahulp0491
1
accept rate: 0%

Running DFS is getting TLE. Why? Check my submissions: http://www.codechef.com/viewplaintext/1881613

link

answered 12 Mar '13, 15:16

bugkiller's gravatar image

3★bugkiller
8.6k194898
accept rate: 9%

You have loop for(int j=i; j<n; j++) after each call to dfs. In the case of no edges in the graph you will have O(N^2) solution. Obviously it should get TLE :)

(12 Mar '13, 16:18) anton_lunyov ♦6★

@anton_lunyov >> So, if I consider the case of graph consisting of no edges separately, will it run within TL?

(12 Mar '13, 16:29) bugkiller3★

No. The graph could have many components, say 90000, and you still will get TLE. Also why you are creating the array visited at each dfs run? This is the worst thing to do. It should be created only once and filled by false before global dfs loop

(12 Mar '13, 16:48) anton_lunyov ♦6★

and that is why my code was 270M :(

(12 Mar '13, 17:04) bugkiller3★

@anton_lunyov >> can you give some large test files, like you did for CHEFHACK?

(12 Mar '13, 17:06) bugkiller3★
showing 5 of 6 show all

Need help finding bug in my solution http://www.codechef.com/viewplaintext/1933093. Was getting WA :(

link

answered 12 Mar '13, 18:04

amit5148's gravatar image

2★amit5148
122
accept rate: 0%

Use ret = ((long long)ret * c) % 1000000007;

(12 Mar '13, 21:31) anton_lunyov ♦6★

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.

TLE CODE: http://www.codechef.com/viewplaintext/1901407

AC CODE: http://www.codechef.com/viewplaintext/1889867

Thanks in advance.

link

answered 12 Mar '13, 18:47

rahul_nexus's gravatar image

3★rahul_nexus
7741923
accept rate: 13%

edited 12 Mar '13, 18:50

Can anyone please tell me where i went wrong?

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

http://www.codechef.com/viewsolution/1907314

link

answered 12 Mar '13, 22:39

ubergeek's gravatar image

2★ubergeek
613
accept rate: 0%

1

Try this test case

1
4 3
1 2
1 3
4 3

You print

2 4

The answer should be

1 4

Can you see how?

(15 Mar '13, 19:52) gamabunta1★

Thank you so much! :)

(12 Apr '13, 00:38) ubergeek2★

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

http://www.codechef.com/viewsolution/1937788

Thanks.

link

answered 13 Mar '13, 02:48

elly66's gravatar image

0★elly66
11
accept rate: 0%

edited 13 Mar '13, 02:49

Checkout the fixes here.

I could point out two bugs

  • You have to find uniques in the id array. You are simply parsing over it and inserting 'i' instead of find(i) in the HashSet.
  • Multiplying everything and storing in a long will potentially overflow. It is fixed by storing the result modulo 1000000007 along the way.
(15 Mar '13, 19:45) gamabunta1★

can you tell me where my solution is failing? my submission is http://www.codechef.com/viewsolution/1909473

link

answered 13 Mar '13, 14:50

abhisht7's gravatar image

3★abhisht7
303
accept rate: 0%

Can anyone tell me what is wrong with my approach and for which test case it fails..... My submission id is http://www.codechef.com/viewsolution/1913075

link

answered 13 Mar '13, 19:34

vivekjnv93's gravatar image

3★vivekjnv93
1513512
accept rate: 0%

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.

link

answered 13 Mar '13, 20:25

aayush123's gravatar image

2★aayush123
1
accept rate: 0%

(14 Mar '13, 00:55) xonix_monty2★

I am getting wrong answer ......... Can you give me failing case..... http://www.codechef.com/viewsolution/1930220

link

answered 13 Mar '13, 22:07

saj1919's gravatar image

4★saj1919
915511
accept rate: 0%

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

link

answered 16 Mar '13, 18:26

progpassion's gravatar image

2★progpassion
112
accept rate: 0%

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

http://www.codechef.com/viewsolution/1926182

link

answered 17 Mar '13, 11:22

nash007's gravatar image

2★nash007
1
accept rate: 0%

Hey, am still getting WA. Can you give me any border test case? http://www.codechef.com/viewsolution/1954203

(21 Mar '13, 12:04) umangkedia3★

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 : http://www.codechef.com/viewsolution/1908378 (PYTH)

Can anyone tell me where it's going wrong ? Thanks. I'm all out of ideas.

link

answered 17 Mar '13, 20:33

ashmew2's gravatar image

2★ashmew2
1112
accept rate: 0%

http://www.codechef.com/viewsolution/1892955 please tell me in which cases my code is getting WA..........

link

answered 18 Mar '13, 15:36

vabsbhole's gravatar image

3★vabsbhole
1
accept rate: 0%

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. http://www.codechef.com/viewsolution/1909473

link

answered 18 Mar '13, 16:19

abhisht7's gravatar image

3★abhisht7
303
accept rate: 0%

Any border test cases? I am still getting WA http://www.codechef.com/viewsolution/1954203

link

answered 20 Mar '13, 19:17

umangkedia's gravatar image

3★umangkedia
11
accept rate: 0%

@anton_lunyov >> Please clarify this:

for (VI::iterator v = a[u].begin(); v != a[u].end(); ++v) {
    // *v is the adjacent vertex itself
    if (!mark[*v]) {
        // if it was not visited before we move to it
        dfs(*v);
    }
}

In the loop part, the begin() and end() functions are called each time an iteration occurs, so isn't it time consuming? Instead if we just store

x = a[u].begin()

and

y = a[u].end()

before entering the loop and then use x and y, do we save some time?

link

answered 22 Mar '13, 22:49

bugkiller's gravatar image

3★bugkiller
8.6k194898
accept rate: 9%

I think all that the .begin() function does is accessing elements on well known positions that are known ahead of time and thus takes constant time, and that gives you no advantage, but maybe doing such modification on the .end() part might help, Im not sure if the gains would be significant though

(23 Mar '13, 00:46) kuruma2★

Can anybody explain what is the runtime of Union/Find approach. I used union by size approach.

link

answered 04 Apr '13, 17:27

rahul_code's gravatar image

3★rahul_code
4124
accept rate: 0%

Plz help me out in this..i thought a lot on this problem and finally did it..though i got a WA i can't figure out what was wrong with my code...i wrote a c program which can be seen at
http://www.codechef.com/viewsolution/1900728

link

answered 04 Apr '13, 18:21

harsh93's gravatar image

3★harsh93
317914
accept rate: 0%

please suggest me in whatcase my code giving wrong answer.....http://www.codechef.com/viewsolution/1976048

link

answered 04 Apr '13, 20:03

hellboy_86's gravatar image

3★hellboy_86
-213
accept rate: 0%

y *= size[i];

Here the value of y can cross the limit of int and hence wrong answer. Make y long long int and you should be fine.

(04 Apr '13, 20:30) rahul_code3★

url:- http://www.codechef.com/viewsolution/1930307

I m getting WA cna someone help in finding a test case where my code fails??

thank u in advance....:)

link

answered 12 Apr '13, 23:23

shub96346's gravatar image

3★shub96346
1346
accept rate: 0%

can any one tell me whats wrong with this code..

its running on my pc but giving compilation error http://ww2.codechef.com/viewsolution/2076377

link

answered 01 May '13, 18:48

tablet007's gravatar image

3★tablet007
11
accept rate: 0%

well sorry for reviving such an old topic but there seems to be an error with my implementation,i dont know why i am getting a TLE here is my submission http://www.codechef.com/viewsolution/2779075 i have used a dfs and a linked list approach could anyone illuminate my mistake.

link

answered 07 Oct '13, 21:18

varuntinkle's gravatar image

3★varuntinkle
11431012
accept rate: 0%

link

answered 05 Aug, 01:09

chauhan_1111's gravatar image

1★chauhan_1111
1
accept rate: 0%

link

answered 16 Aug, 00:56

akhilesh0502's gravatar image

3★akhilesh0502
1
accept rate: 0%

Answer is hidden as author is suspended. Click here to view.

answered 16 Aug, 03:54

armen99's gravatar image

0★armen99
(suspended)
accept rate: 0%

Can someone please point out the mistake in this code?

Code-(https://www.codechef.com/viewsolution/15077459)

Thanks :)

link

answered 22 Aug, 21:53

ameyanator's gravatar image

4★ameyanator
0
accept rate: 0%

such a nice problem, thank you codechef.

link

answered 25 Sep, 23:39

kishynivas_13's gravatar image

3★kishynivas_13
0
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×12,340
×801
×474
×82
×20

question asked: 12 Mar '13, 06:04

question was seen: 11,171 times

last updated: 25 Sep, 23:39