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
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » July 2010 » Happy Days

Happy Days

Problem code: HAPPY

  • All Submissions

All submissions for this problem are available.

Johnny has a pool in his garden. There are several islands in the pool. Some islands are connected by bridges. Any bridge can be removed. Every day Johnny removes some bridges so that there is only one way from any island to any other. In the evening he returns removed bridges to their places. Also he has some favorite bridges which he never removes. Johnny will be happy if he is able to make a configuration of bridges on the given day which he has never made before. You have to count the amount of days he will be happy. Of course, if the favorite bridges themselves don't satisfy the happiness condition Johnny will not be happy for even single day.

Input

The first line of input file contains number t the number of test cases. Then the description of each test case follows. The first line of each test case contains number n the number of islands. Islands are numbered with integers from 1 to n. Then n lines follow each containing n characters defining the connectivity matrix of those islands. Character in column x of line y will be 1 if the islands with numbers x and y are connected and 0 otherwise. The next line is number p the number of favorite bridges. The next p lines contain the pairs of islands that are connected by favorite bridges.

Output

For each test case print the number of days Johnny will be happy in this situation.

Constraints

1 <= t <= 5
2 <= n <= 30
1 <= p <= min(6, n-1)

Example

Input:
1
4
0111
1011
1101
1110
2
1 2
3 4

Output:
4


Author: spooky
Date Added: 3-05-2010
Time Limit: 4 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

The bridge from Island 1 to

AnoopNarang @ 1 Jul 2010 07:23 PM

The bridge from Island 1 to Island 2  is same bridge that is from island 2 to island 1 ...... or there are 2 distinct bridges ..??

It's the same ..

ahnikhil @ 1 Jul 2010 08:52 PM

It's the same ..

There is only one bridge

Tool @ 1 Jul 2010 08:57 PM

There is only one bridge between two islands. If there were two, the answer would be 8 (4 pairs of islands not connected by favorites * 2 bridges for each pair = total number of bridges that could be left alone i.e. 8)

are we assured that the

thealgorist @ 1 Jul 2010 11:11 PM

are we assured that the answer will fit in long long? Or is that information intendedly not given?

is it legal to use the code

thealgorist @ 2 Jul 2010 08:05 AM

is it legal to use the code library of someone else??? with proper acknowledgement of the author of that code library.

I think judge should

white_king @ 2 Jul 2010 10:59 AM

I think judge should explicitly say if the given matrix is symmatric.

Please mention if the answer

shettynamit @ 2 Jul 2010 02:53 PM

Please mention if the answer will fit in 64 bit integer. The number of JAVA submissions indicate that 64 bit is not enough. Plz clarify!

On submitting my solution,

shettynamit @ 2 Jul 2010 04:51 PM

On submitting my solution, the judge says " :( internal error occured in our system " .... is this a problem with my program or the judge?

On submitting my solution,

shettynamit @ 2 Jul 2010 04:51 PM

On submitting my solution, the judge says " :( internal error occured in our system " .... is this a problem with my program or the judge?

@NAMIT .D. SHETTY It's

spooky @ 2 Jul 2010 05:03 PM

@NAMIT .D. SHETTY

It's actually a part of a problem for you to derive if the answer will fit into any standard datatype

"Internal error occured in our system" is the problem of the judge. You should just resubmit I assume.

seems the internal error

shettynamit @ 2 Jul 2010 05:18 PM

seems the internal error issue is fixed.....

@Ivan - thnx 4 the reply :)

I am getting TLE, but on my

shiplu @ 3 Jul 2010 01:18 AM

I am getting TLE, but on my pc i have tested my code with 5 test cases of 30 nodes complete graph, its runs <2 sec :| , i dont know what to do ............

anyone plz explain how the

root5 @ 3 Jul 2010 04:19 AM

anyone plz explain how the answer is four...with proper path

@Satyarth: you can remove any

moshiur @ 3 Jul 2010 02:37 PM

@Satyarth:

you can remove any three of the remaining 4 unfavorite edges and the resulting graph will be a tree. So, the answer is four.

Are the bridges

srihari @ 4 Jul 2010 12:34 AM

Are the bridges bidirectional?

Can someone give me a input

bhathiya @ 4 Jul 2010 03:33 AM

Can someone give me a input file to be tested? Judge says my answer is wrong, but there's no way for me to check it without a good test file. :-(

Codechef, can you please show an input of more than one test case?

That is against the rules of

triplem @ 4 Jul 2010 04:47 AM

That is against the rules of the content; please do not ask people to break the rules.

"Of course, if the favorite

jimbo @ 4 Jul 2010 07:07 AM

"Of course, if the favorite bridges themselves don't satisfy the happiness condition Johnny will not be happy for even single day."

I am not exactly sure what that is saying. Is it saying that if you need to remove a favorite bridge in order to make a new path, it will not make him happy?

yes. It is saying that if it

mcsharma1990 @ 4 Jul 2010 10:10 AM

yes. It is saying that if it is always necessary to remove atleast 1 bridge from favorite bridges then he will never be happy.

I learnt Java to solve this

cgy4ever @ 4 Jul 2010 04:23 PM

I learnt Java to solve this problem!

This is my first Java Program , not long but coding long time , LOL.

HAPPY made me UNHAPPY for 2

coders1122 @ 5 Jul 2010 06:42 PM

HAPPY made me UNHAPPY for 2 days. :|

@Team Cyclone Am an atheist

sumeet_7_in @ 5 Jul 2010 11:54 PM

@Team Cyclone

Am an atheist but i do not do blasphemy.

Myabe you have no respect for a coding competetion and all, but if others are doing it why dont you refrain from ruining the 'fun' of it by posting solutions.


And try to be a bit rational, by open source and by saying competetions should be 'team work' you are basically saying that 22 players should shoot goal in the same goal post for 1st half and other goal post in second half by working as a 'team'.




@ sumeet +1 it is too bad

dabbcomputers @ 6 Jul 2010 10:03 PM
@ sumeet +1 it is too bad to post solution...... @admin Team cyclone should be banned from codechef.

can the graph be

Amit Karmakar @ 7 Jul 2010 12:11 AM

can the graph be disconnected? If it is what is the happy days count?

"Every day Johnny removes some bridges so that there is only one way from any island to any other."

Does it mean there is always one way from any island to any other or if islands are connected they must be connected with a single path???

There must be exactly one way

triplem @ 7 Jul 2010 03:01 AM

There must be exactly one way between each pair of islands, not 'at most one'.

phew... 20 submissions.. so

calc_saransh @ 9 Jul 2010 12:35 PM

phew... 20 submissions.. so many TLEs so many wrongs.. but finally it works.. feels great... :)

1---2----------3--------5 |  

v_new.c @ 9 Jul 2010 05:23 PM

1---2----------3--------5

|            |

5

 

in this graph  2 3 5 makes a cycle

is answer is 2 or 3 for happy day.

The example shown not at all

Juned123 @ 9 Jul 2010 10:29 PM

The example shown not at all clarifies the problem. The matrix shows all the islands are interconnected.

And the output shown is just the number of unfavourite bridges.

So can somone plz explain with a better example.

is the graph is complete

v_new.c @ 11 Jul 2010 12:13 AM

is the graph is complete graph

mean every island is connected to every island?

No, the problem statement

triplem @ 11 Jul 2010 04:36 AM

No, the problem statement does not say the graph has to be a complete graph.

Please give, atleast one more

Gaurav Rai @ 11 Jul 2010 05:17 AM

Please give, atleast one more test case.

i tried to use principle of

akantev @ 11 Jul 2010 08:10 PM

i tried to use principle of inclusion exclusion... but somehow ended up with WA everytime. Could someone please tell me whats the flaw in my argument?

Let F be the set of favorite edges. For every set S subseteq F, define N(S) as the number of spanning trees in G that preserve at least the edges in S.

We need the number of spanning trees that preserve exactly all edges in F.I used PIE for this calculation. Is this method right?

Now that the contest is over,

Juned123 @ 11 Jul 2010 09:03 PM

Now that the contest is over, can anyone explain the problem with a better test case?

@vivek.. red

mcsharma1990 @ 11 Jul 2010 10:17 PM

@vivek.. red this http://en.wikipedia.org/wiki/Kirchhoff's_theorem

but the given problem is not a direct application of this theorem since you always have to select some paticular edges.

to solve this problem you first remove all the edges which are NOT favourite, then the remaining set of nodes can be considered as a forest.(If this does not reduce to a forest then ans will be zero) Now consider every tree as a single node  (total no.of new nodes will be n-p)  and no.of edges between two nodes will be equal to the no. of connections between their corresponding trees.

Now finally you have a new multi graph, no. of spanning trees of which you can find using kirchoff theorem.

sorry for spelling mistakes.

mcsharma1990 @ 11 Jul 2010 10:17 PM

sorry for spelling mistakes. :)

@mcsharma thanks .... yes i

akantev @ 11 Jul 2010 11:35 PM

@mcsharma thanks ....

yes i got that... but whats wrong with the principle of inclusion exclusion? sorry i didnt define it correctly above.... here is what i intended to:

Let F be the set of favorite edges. For every set S subset of  F, define N(S) as the number of spanning trees in G that dont preserve at least the edges in S.

N(S) is computable using kirchoffs theorem.

Now we need the number of spanning trees that dont miss any edge, call it n.

Then, by Inclusion Exclusion , n = N(phi) - N(subset of F having 1 element) + N(subset of F having 2 elements) - ...

Is this wrong? I tried it for simple cases (eg complete graphs with 0/1 edge fixed etc. ,) it works

@vivek: That should be n =

MichaelD @ 11 Jul 2010 11:58 PM

@vivek: That should be n = N(phi) - sum of N(S) over all subsets S of F having 1 element + sum of N(S) over all subsets S of F having 2 elements -...

Equivalently, n = sum of N(S)(-1)^|S|, over all subsets S of F.

yes thats what i mean in the

akantev @ 12 Jul 2010 08:24 AM

yes thats what i mean in the last post.... sorry if it wasnt clear .... so that means it was an implementation mistake or something similar :(

Yes, it could be anything. An

MichaelD @ 12 Jul 2010 12:30 PM

Yes, it could be anything. An easy logical mistake to make would be to forget that N(S)=0 if G-S is disconnected.

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 algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm 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 programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were 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 challenges 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 coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

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. You can also host a coding contest for your institute on CodeChef, organize an algorithm event 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