CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • May Cook-Off
    • May Long 2012
    • April Cook-Off
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » April 2010 Contest » Lighting the shop

Lighting the shop

Problem code: LIGHTING

  • All Submissions

All submissions for this problem are available.

The exciting World Cup 2010's summer is coming! And Johnny's family shop is busy preparing a colorful lighting board for the summer.

The board has a rectangular shape of size M x N. There are certain positions on the board which Johnny wants to put in light bulbs.

Johnny wants to decorate the board by colorful light bulbs. To make the board look attractive, he wants the bulbs in the same column or row to have different colors! On the other hand, to preserve the harmony look, Johnny doesn't want the bulbs to have many different colors.

Write a program to help Johnny decorate the boards in such a way that the number of color used is minimum.

Input

The first line contains a number t (about 15), the number of test cases. Each test case has the following form.

The first line contains two numbers m, n (1 <= m, n <= 700). Each line in the next m lines contains n characters '0' or '1' representing the board, in which '1' denotes a place which Johnny wants to put in a light bulb.

Each test case's input is separated by a blank line.

Output

For each test case, print the output in the following format.

The first line contains a number p which is the minimum number of used colors.

The ith line in the next m lines contain n integers, in which the jth number is the color index of the light bulb in the corresponding place, or 0 if the place does not need a light bulb. The colors are numbered from 1 to p. Your program can print any correct solution.

Print a blank line after each test case's output.

Example

Input
1

4 4
1001
1101 
1011
1001

Output
4
1 0 0 2 
2 1 0 3 
3 0 1 4 
4 0 0 1 


Date: 2010-03-11
Time limit: 5

s
Source limit: 50000
Languages: C C99 strict C++ 4.0.0-8 C++ 4.3.2 PAS gpc PAS fpc JAVA NICE JAR C# C#2 NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp SCALA HASK ERL CAML CLPS PRLG WSPC BF ICK JS


  • Submit

Comments

  • Login or Register to post a comment.

""Each line in the next m

sumitb @ 2 Apr 2010 07:12 PM

""Each line in the next m lines contains n characters '0' or '1'""

Can this program take input as:

1 0 0 1

1 1 0 1

1 0 1 1

1 0 0 1

or it is need to be without spaces..

reply soon admin

Please specify the output

lkssharma @ 2 Apr 2010 08:22 PM

Please specify the output clearly or the input format .

 

@Sumit No. @Lalit, Sumit

i0exception @ 2 Apr 2010 09:06 PM

@Sumit No.

@Lalit, Sumit Check Sample I/O.

Is this Output Format correct

mudit_jaju @ 2 Apr 2010 11:30 PM

Is this Output Format correct (given m=5, n=4):

 

4

1 2 3 4

3 0 0 2

2 4 1 0

0 3 2 1

4 1 0 3

---------> Blank line here before the next test case starts

 

 

@Aniruddha It is working

lkssharma @ 3 Apr 2010 07:31 PM

@Aniruddha It is working correct for Sample I/O ...but i m not able to submit.

hmmm, I did every thing

mohan krishna @ 3 Apr 2010 09:18 PM

hmmm,

I did every thing which i can to reduce my time complexity but still it is the same T L E...

can anyone help me out on this plz...?

@mohan ..same here

lkssharma @ 3 Apr 2010 11:48 PM

@mohan ..same here

Greetings admin, My code runs

mtk @ 4 Apr 2010 02:24 AM

Greetings admin,

My code runs for large input such as 50x50 on my laptop(intel processor) but while submitting i'm getting SIGKILL error, may I know what is this error and what is about to create such a error in my code.

Thanks in advance

It's hard to say what SIGKILL

writerAPR10 @ 4 Apr 2010 08:05 PM

It's hard to say what SIGKILL is exactly, it is a error signal produced by C++. Your program must contain bugs somewhere.

@Stephen, How on earth did

balakrishnan_v @ 5 Apr 2010 03:29 AM

@Stephen,

How on earth did you solve these problems so quickly?

It's a holiday weekend :P

triplem @ 5 Apr 2010 04:10 AM

It's a holiday weekend :P

Can anyone confirm that for a

shameed @ 5 Apr 2010 10:57 AM

Can anyone confirm that for a matrix of 10x10 with 1's in all locations, there are a total of 22 min colors needed.

Similarly for 100x100 with 1's in all locations, there are a min of 128 colors.

I am getting Wrong Answer, trying to verify if the data is correct.

Thanks.

The data is correct. Extra

triplem @ 5 Apr 2010 11:04 AM

The data is correct. Extra test cases can't be provided.

Just to clarify: Stephen

akuegel @ 5 Apr 2010 11:49 AM

Just to clarify: Stephen means the judge data is correct, and I agree. So if you get wrong answer, your program must be wrong.

@Sohail I think the minimum

mtk @ 5 Apr 2010 12:48 PM

@Sohail

I think the minimum number of color counted by you are wrong.

Any say in that......

@Md Tareque Khan Discussing

triplem @ 5 Apr 2010 12:50 PM

@Md Tareque Khan

Discussing test cases will get you disqualified.

@stephen Hey, do u know

mtk @ 5 Apr 2010 01:04 PM

@stephen

Hey, do u know anything about SIGKILL error. what shoud I do. The program rns fine on my laptop for big inputs too.

@stephen Hey, do u know

mtk @ 5 Apr 2010 01:04 PM

@stephen

Hey, do u know anything about SIGKILL error. what shoud I do. The program rns fine on my laptop for big inputs too.

@stephen Hey, do u know

mtk @ 5 Apr 2010 01:04 PM

@stephen

Hey, do u know anything about SIGKILL error. what shoud I do. The program rns fine on my laptop for big inputs too.

@stephen Hey, do u know

mtk @ 5 Apr 2010 01:04 PM

@stephen

Hey, do u know anything about SIGKILL error. what shoud I do. The program rns fine on my laptop for big inputs too.

@stephen Hey, do u know

mtk @ 5 Apr 2010 01:04 PM

@stephen

Hey, do u know anything about SIGKILL error. what shoud I do. The program rns fine on my laptop for big inputs too.

@stephen Did you mean my data

shameed @ 5 Apr 2010 07:47 PM

@stephen

Did you mean my data is correct or the judge data is correct?  Just need clarification.

Sorry I am new here.  I just

shameed @ 5 Apr 2010 10:42 PM

Sorry I am new here.  I just read the FAQ.  My question might fall under prohibited rules.  You don't need to answer it.

@Md Khan: please don't post

writerAPR10 @ 6 Apr 2010 12:13 AM

@Md Khan: please don't post repeated messages :) Try to google about SIGKILL, there's may be some information

@Sohaild: The judge data should be correct as some users have successfully solve the problem.

sry for the repeating

mtk @ 6 Apr 2010 12:39 AM

sry for the repeating comments.........

actually I had a network problem, so that might have caused such a nuissance.......

@Writer I don't know why

shameed @ 6 Apr 2010 02:14 AM

@Writer

I don't know why people think I am doubting the judge data.  I am not.  I was just trying to verify some data points so that I can isolate if the problem is in my algorithm or simply in input/output.  And I have already found some bugs and have fixed them all and now it is not saying Wrong Answer any more.  However I am now getting TLE, even though I have optimized it a lot.

As mentioned discussing test

triplem @ 6 Apr 2010 02:47 AM

As mentioned discussing test cases is not allowed. It is your job to debug your program, not other people's.

Input -> The first line

Roopesh.kumar.h @ 7 Apr 2010 11:05 AM

Input -> The first line contains two numbers m, n (1 <= m, n <= 700).

Are these numbers space separated while giving input?

Of course they are.. if there

triplem @ 7 Apr 2010 11:08 AM

Of course they are.. if there was no space between them you couldn't tell what m and n were at all. Look at the sample input.

Stephen Merriman Hi,have u

heeropunjab @ 7 Apr 2010 01:13 PM

Stephen Merriman

Hi,have u use any standard algorithm for this Problem

I'll tell you in 5 days.

triplem @ 7 Apr 2010 01:38 PM

I'll tell you in 5 days.

hey i m getting

rbnishant @ 7 Apr 2010 08:13 PM

hey i m getting error

/sources/tested.cpp: In function 'int main()': /sources/tested.cpp:4: error: 'cout' was not declared in this scope

 

can anyone suggest what to do with this error......

Stephen Merriman Can u telll

heeropunjab @ 7 Apr 2010 08:42 PM

Stephen Merriman

Can u telll me how much line of code your program is?plz


@nishant: I think you should

shameed @ 7 Apr 2010 08:56 PM

@nishant:

I think you should use std::cout instead.

@Mark Greve You made my 90

triplem @ 9 Apr 2010 11:20 AM

@Mark Greve

You made my 90 something submissions to a problem last contest look like nothing ;)

@Stephen Merriman I will tell

gmark @ 9 Apr 2010 01:08 PM

@Stephen Merriman

I will tell the story later :)

Guys, it's good to see you

DevGeeK @ 10 Apr 2010 12:38 AM

Guys, it's good to see you talking here. I want to ask you a few questions, which are not connected with the contest or problems. How can i become great programmer like you ? What is the formula to success in algorithmic problem solving ? Where did you learned all those stuff, and how can i learn them ? Thank you :)

solve more and more problems

mcsharma1990 @ 10 Apr 2010 05:08 PM

solve more and more problems ...thats all...

Let G be the graph such that

balakrishnan_v @ 13 Apr 2010 07:48 PM

Let G be the graph such that each cell in the matrix is a node and the corresponding cells in the same column and row are its neighbours. Notice that vertex coloring G is equivalent to edge-coloring L(G) where L(G) is the line-graph of G.

If every entry in the mXn matrix were a 1, then the line graph of G is a K(m,n), i.e. a fully connected bipartite graph.It is easy to edge-color a fully connected bipartite graph. If some of the elements were zeros, we again get a bipartite graph. However, this time it is not as easy to edge color this graph as was the case of K(m,n).  To get started with edge-coloring of general bipartite multi-graphs, one might want to look at some of the links available here.

I would recommend looking

gmark @ 13 Apr 2010 10:53 PM

I would recommend looking into algorithms that are based on some sort of augmentation instead of going for divide-and-conquer approaches using Euler tour and perfect bipartite matching as subroutines. Even though the asymptotic performance is much worse for the augmentation approaches (I even doubt it would run in time on a just a few worst case test cases), it works a lot better on the test cases for this problem. I spent like 130 submissions on getting the divide-and-conquer approach to run in time... But I do see that others have managed to squeeze it inside the time limit.

My augmenting algorithm timed

pieguy @ 14 Apr 2010 12:54 AM

My augmenting algorithm timed out, so I switched to divide and conquer, only to eventually discover that my problem was actually that I was doing output too slow.  So I ended up with a hybrid, doing augmentation until the maximum degree is a power of 2, then switching to divide and conquer.  I also added a special case for when the maximum degree was equal to the larger dimension of the grid, which appeared to catch at least one big test case.

The bottleneck of

thocevar @ 14 Apr 2010 01:03 AM

The bottleneck of augmentation approach was checking if endpoints of an edge have a common free color. The lengths of paths which need to be augmented was surprisingly small (around 10 on my 200x200 cases). AND-ing bitmasks of free colors gave me a 16x speed-up which was good enough.

Usually in order to decrease

shameed @ 14 Apr 2010 01:22 AM

Usually in order to decrease the execution time (increase the speed) you have to pay penalty in terms of memory used.  Hats off to Adrian, he achieved it with the least time and least memory.

I used the augmenting path

triplem @ 14 Apr 2010 03:08 AM

I used the augmenting path approach. Namely, greedily fill in cells using the smallest colour that hasn't been used in the row/column. If no colour is available, find the smallest unused in the row (x), and smallest unused in the column (y). Fill this cell with an x; replace the x in the column with a y; replace the y in that row with an x, etc.

To find the smallest in each row/column/combined I used bitmasks; an array of 24 ints (each storing 30 bits) was enough to pass in time, allowing you to skip 30 colours at a time.

I also used the augmenting

akuegel @ 14 Apr 2010 02:27 PM

I also used the augmenting path approach, however I didn't use the greedy part because as Stephen already said, it is expensive to find which color is unused in row x and column y. Instead I determined an unused color c in row x. If this color does not occur in column y, we are done. Otherwise find a color c2 which does not occur in column y. If this color does not occur in row x, again we are done. Otherwise use augmenting path method and switch colors c and c2. I found out by doing a random_shuffle on the available colors for each column in the beginning, the augmenting path method takes about 4.8 steps on average for each cell on a 700 x 700 grid with all 1s.

Btw, this problem was already posed with smaller limits in an UVA contest: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&cat...

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com