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 » Wiki » May 2011 Contest Problem Editorials

May 2011 Contest Problem Editorials

 

PRUNING (written by Zac Friggstad)

 

This can be solved with dynamic programming. Let w(u,v) denote the weight of the edge between u and v. Root the tree at an arbitrary node r. For nodes v,c within distance d of each other, let m(v,c) denote the maximum total weight of edges in the subtree rooted at v among all solutions that have c being the center of the component containing v. Then m(v,c) satisfies the following recurrence. To simplify notation, let k(u) denote the maximum over all m(u,c) where c ranges over nodes in the subtree rooted at u whose distance to u is at most d (i.e. not considering possible centers c that are not included in the subtree rooted at u).

- if v is a leaf, then m(v,c) = 0

- if v is not a leaf and either c = v or the path from v to c goes through the parent of v, then m(v,c) is the sum, over all children u of v, of the quantities

1) max(k(u), m(u,c) + w(u,v)) if the distance from u to c is at most d

2) k(u) if the distance from u to c exceeds d

- if v is not a leaf and c is in a subtree rooted at some child u' of v, then m(v,c) is the sum of w(u',v) + m(u', c) plus the sum, overall children u != u' of v, of the quantities listed in the previous point.

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------

HARMONY (written by Zac Friggstad)

 

Given an assignment of 0/1 values to edges of the graph, let w(C) be the number of edges on a cycle C that are assigned a 1 and let f(C) be the number of faces contained in C. Say that a cycle C is harmonious if w(C) = f(C) mod 2. If C is a cycle that bounds a single face, then f(C) = 1 so a necessary condition for all cycles being harmonious is that w(C) is odd for every cycle C that bounds a single face.

One of the main observations in this question is that this is also sufficient. Consider a cycle C and say it contains k = f(C) faces F_1, …, F_k. Let C_1, …, C_k denote the cycles that bound the respective faces. Then w(C) = W(C_1) + … + W(C_k) (mod 2) because every edge on C is counted only once and every edge contained in the region bounded by C is counted twice in the sum (once for each of the faces F_i, F_j that include the edge on C_i, C_j). Because the graph is has no bridges, then there is no edge that has the same face touching both sides. We have W(C_1) = … = W(C_k) = 1 (mod 2) since each C_i is the cycle bounding a single face so then w(C) = k = f(C) (mod 2), which is what we wanted to show.

If n was smaller, then we could simply set up a system of linear equations mod 2 where we have variables for the edges an one equation for each interior face and solve this using Gaussian elimination. However, n can be much larger so a better algorithm is needed. First, find any spanning tree T of the graph. Consider the "dual complement" of T. This is a graph T' whose nodes correspond to faces in the drawing of G (including the unbounded face) and two nodes in T' are adjacent if their corresponding faces in the drawing of G share a common edge that is not in the tree T. Let r be the node in T' that corresponds to the unbounded face of the drawing of G.

Now, T' is also connected and acyclic. The intuitive idea behind this is that if T' is not connected then faces in the drawing of G that are not in the same component as r in T' are separated from the other faces by a cycle whose edges are in T, which contradicts the fact that T is acyclic. Also, if T' has a cycle then we can draw a closed curve in the plane that stays entirely contained in the faces corresponding to such a cycle and passes only through edges that are not in T. But then these edges form a cut of G which contradicts the fact that any cut of a connected graph must contain at least one edge of any particular spanning tree.

To exploit this, assign arbitrary values to the edges in T (e.g. 0 for each such edge) and root T' at r. A leaf node x (excluding r if r has degree 1 in T') corresponds to a face in the drawing of G for which exactly one edge on the boundary has not been assigned a value. So, pick any such leaf x and assign whatever value is needed to have the corresponding face f(C) odd where C is the bounding cycle of this face. Then remove x from T' and recurse until T' consists of only the root r, in which case we are done. With care, this can be implemented to run in O(n log n) time.


You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------

BICKER (written by Zac Friggstad)

 

This is an NP-hard problem more commonly known as sparsest cut. One observation is that in each test case it is always possible to find an answer with value 2 according to the scoring mechanism. This follows from the observation that we can always cut at least 1/2 of the total d-values through a simple greedy algorithm. Process the nodes one at a time and, at each step, we can either place the current node in S or in T. Choose the one that separates the most d-value among the edges from v to nodes appearing in S or T; at least half of the d-value of these edges is then separated. Since each edge is "introduced" once and at least half of the d-value is separated at each step, then at least half of the total d-value is cut. This is a good starting point for a heuristic, but it completely ignores the q-values. There are examples (with optimum score close to 0) where this heuristic performs very poorly.

In general, the best approximation algorithm is O(sqrt(log n) * loglog n) [1]. If all d-values are 1 (i.e. d(u,v) = 1 for all u != v in V) then there is a slightly better O(sqrt(log n)) [2]approximation. However, they both require techniques from "semidefinite programming", a generalization of linear programming. More practically, there are linear programming based O(log n) approximations that are quite easy to implement given an LP solver [3,4].

1) S. Arora, J.R. Lee, and A. Naor, Euclidean distortion and the sparsest cut. Journal of the American Mathematical Society, 21:1-21, 2008.

2) S. Arora, S. Rao, and U. Vazirani, Geometry, flows, and graph-partitioning algorithms. Communications of the ACM, 51:96-105, 2008.

3) T. Leighton and S. Rao, An approximate max-flow min-cut theorem for uniform multicommodity flow problems with applications to  approximation algorithms. In proceedings of IEEE Foundations of Computing Science, 422-431, 1988.

4) N. Linial, E. London, and Y. Rabinovich, The geometry of graphs and some of its algorithm applications. Combinatorica, 15:215-245, 1995.

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------

TPRODUCT (written by AnhDQ)

 

The statement has directly shown the way to solve (by formulas) the problem, but it is not very easy to do since the result could be a very  large number. It is hardly to use big-number calculating to fit the given time-limit. The problem needs only the result by modulo of 1,000,000,007 but we can not compare the numbers by their remainders.

Fortunately we have an effective way to solve it by combining some mathematical theories as follows:

- At first, we can store the value of P_i by storing its logarithm instead;

- Then instead of doing a lot of multiplications (which causes very large results), we just do a series of additions between their logarithms (which are quite small real numbers);

- At last, we do tracing and calculate the needed result by modulo of 1,000,000,007; this work does not need big-number processing since we can take the remainders after every multiplication.

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------

PRDIVSUM (written by Anton Lunyov)

 

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------

BWALL (written by Snigdha Chandan)

 


You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------


Comments

  • Login or Register to post a comment.

I can't see the images

sushilnath @ 11 May 2011 04:21 PM

I can't see the images attached. Plz correct the bug.

Tree Product actually esaily

EgorK @ 11 May 2011 05:50 PM

Tree Product actually esaily fits in time limit using Java's BigInteger

if we use logarithm for TPROD

thechamp @ 11 May 2011 07:35 PM

if we use logarithm for TPROD we might lose precision. If judge used that solution to check, few solutions might have been rejected because of conflict in precision

What is the difference

k0stia @ 11 May 2011 07:42 PM

What is the difference between Problem Setter & Problem Tester ?

plss xpalin that how to use

dabbcomputers @ 11 May 2011 08:29 PM

plss xpalin that how to use matrix exponentation i derived the formula but not able code it..... because large value nd run time...

@Amritpal think of this

calc_saransh @ 11 May 2011 09:27 PM

@Amritpal

think of this matrix

{{1,4,3},{1,0,0},{0,1,0}} and try multiplying it with the matrix {f(2),f(1),f(0)}

 

you will notice the result is {f(3),f(2),f(1)}

 

here the first line is just the coefficients for the recurrence and the other lines is to shift the matrix down.. so that they can be used for the calculations later.. using this matrix we can fine any f(n) in O(log(n)) time

edit {1,4,2} :)

calc_saransh @ 11 May 2011 09:28 PM

edit {1,4,2} :)

@ admin can't we use

aayush123 @ 11 May 2011 09:58 PM

@ admin

can't we use (a*b)%m=((a%m)*(b%m))%m

??????????

The floating point approach

tomek @ 11 May 2011 10:37 PM

The floating point approach described for TPRODUCT should not have worked: it's not hard to build test cases where the logarithms come too close to be distinguished by standard floating point arithmetic. Take two large consecutive integers that have more than 64 bits, factor them into prime numbers and use these factors along two paths in the tree.

@Ayush@adminI did the same

ambujpandey @ 12 May 2011 01:16 AM

@Ayush
@admin
I did the same but got wrong answer on submission. Although I formulated some big test cases and checked the result of my program with a simple Jave program for Tree Product (which was accepted coz of BigInteger) and the output was same.

This concept does gives right answer. If we multiply 2 nos 10^9 and 10^9 it gives 10^18 which fits in long long. Then we take mod 1000000007 straight away. But prior to it, we divide it by 1000000007 and store the quotient and use this quotient as a priority check for the no.
Please correct me if I am wrong.


http://www.codechef.com/viewsolution/539482
This gave WA.

@admin plzzz tell me how can

aayush123 @ 12 May 2011 08:44 AM

@admin

plzzz tell me how can i deal with mod as i am trying to solve TREEPRODUCT by recursion.

I did not solve the Sum

sppraveen @ 12 May 2011 01:47 PM

I did not solve the Sum divides Product problem. Can anyone give pointers like - what all we need to solve this kind of problem. Is it just strong number theory or what? How to go about improving skills so that we could solve these type of problems in the future.

@Anton Lunyov. Three

mkagenius @ 12 May 2011 10:28 PM

@Anton Lunyov.

Three questions:

1. Why do you always paste a image , since its a wiki page , it should be editable text.

 (But thats understanble, may be because codechef don't have latex, idiot codechef)

2.Why do you put a square bracket inside a rounded one.It makes me think it is "box" function.

3.Actually i forgot the third one, but i will frame one.

Why do give these complicated tutorials, with lots and lots fo sigmas and square brackets.

(but even that is understanble , since there should be some difficukt mathematical problem in the whole set.)

No creature (dead or alive)

codecrux @ 13 May 2011 12:58 PM

No creature (dead or alive) is allowed to modify Anton Lunyov's editorial post.I think Codechef actually converts the text to image before putting it here :)

every problem was great :)

adu101992 @ 13 May 2011 07:52 PM

every problem was great :)

Admin should also provide

manoharsingh23 @ 6 Jun 2011 08:24 PM
Admin should also provide test files and output files after contest ends.
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