Solutions/Hints For The Problems: 1. Traffic Safe City (Points: 300)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Solutions/Hints for the Problems

1. Trac Safe City (Points: 300)


It can be modelled as a simple weighted undirected graph G, with the intersections as the vertices, roads as the edges and the weight of each edge is the trac coecient (as dened in the problem). This can be solved by reduction to Max-Flow problem for directed graphs. The algorithm is as follows: 1. Compute the sum of the weights of all the edges. If sum is not equal to |V | 1, then condition (1) given in the problem is violated, else continue. 2. Fix any one node (call it u). 3. Iterate over all other vertices v V . 4. Construct a di-graph H with u as the source and v as the sink (with the method described below), and compute max-ow f in H. 5. If f |V | 1, then condition (2) given in the problem is violated, else continue. 6. Construct a di-graph H with u as the sink and v as the source (with the method described below), and compute max-ow f in H. 7. If f |V | 1, then condition (2) given in the problem is violated, else continue. 8. If none of the above conditions are violated, then the graph is trac safe. Construction of Di-Graph: Let i be the source and j be the sink. 1) For every edge (a, b) G with weight w, construct two edges (a, b) and (b, a) in H with weights w/2. 2) For all a V i, j, add an edge (a, j) with weight 1. 3) For all a V i, add an edge (i, a) with weight
k

w(a, k)/2.

Claim: There exists a violated set S with u S and v (V S) if and only if max ow in some H |V | 1. Proof Outline: Consider the capacity of the cut S and use the max-ow min-cut theorem. Using Edmond-Karp will clear upto 2nd test-case, however using Dinitz or any preow push based algorithm will clear all cases.

2. Save Me Please, Doctor! (Points: 100)


The sequence basically gives us the Thue-Morse sequence. The sequence can be generated in several ways and one of them is using the following grammars. 0 01 and 1 10 with starting symbol 0. The sequence generated is S0 = 0 S1 = 01 S2 = 0110 S3 = 01101001 and so on.. We can clearly see the k th element of Sn is independent of n. Moreover the k th element is merely the parity of set bits in the binary representation of k. So, a0 (000b ) = 0%2 = 0, a1 (001b ) = 1%2 = 1, a2 (010b ) = 1%2 = 1, a3 (011b ) = 2%2 = 0, a4 (100b ) = 1%2 = 1, a5 (101b ) = 2%2 = 0, a6 (110b ) = 2%2 = 0, a7 (111b ) = 3%2 = 1, any Sn . Using suitable BIGINT data staructure and the Kerninghans Parity algorithm which takes O(number of bits set), the problem can be solved within the time limits. [1] http://mathworld.wolfram.com/Thue-MorseSequence.html [2] http://en.wikipedia.org/wiki/ThueMorse sequence irrespective of n. Hence the sequence a0 , a1 , a2 , a3 , ... a2n generates

3. Repairing the Roof (Points: 300)


This problem is a variant of the famous Domino Tiling Problem. We can dene a simple function f which counts how many ways there are to tile any closed gure. The algorithm is based on recursive calls which calculate f for the two subshapes made by removing the vertical and horizontal dominoes that cover the upper-left most square and adds them together. Although the combinatorial explosion makes this algorithm extremely inecient. Improvements can be made by memoizing using hash-tables etc. However its still pretty inecient. The best solution is obtained by reducing the problem to perfect matching counting in simple graphs. Create a vertex v in G for every 1 1 uncovered area on the roof. Add an edge between two vertices if they can be covered by a single domino (1 2 or 2 1 tile). Now count the number of perfect matchings of the graph G. However, the problem of determining the number of perfect matchings in a given graph is #P Complete. But a remarkable theorem of Kasteleyn states that the number of perfect matchings in a planar graph can be computed exactly in polynomial time via the FKT algorithm. The

graph G generated above is planar and a lattice graph. Hence we can nd a valid orientation of G without actually computing the spanning tree and dual graph in O(V ). Then simply calculate the absolute value of the Pfaan (square root of the determinant) of the (1, 1, 0) adjacency matrix of G. [1] Y2K Problem of Dominoes and Tatami Carpeting from Puzzlers Tribute A Feast for the Mind [2] http://en.wikipedia.org/wiki/FKT algorithm [3] www.cs.bris.ac.uk/montanar/presentations/matchings.pdf

4. Round and Round (Points: 200)


This can be easily solved using a good hash function to store the points being covered. Hash all the points covered and check whether a new point has already been visited previously. This clears all the test cases within the time limits.

5. Abducted!!! (Points: 100)


The problem comes down to maximising the function f = ax2 + by 2 + cxy + dx + ey with the constraints gx + hy j, x 0, y 0 in integer domain. The brute force solution checks all possible points within the constraints, but is very expensive. Since the function is strictly increasing in x and y, we can check only the points on the constraint boundary to get a much more ecient solution. When the line is skewed, check along the axis which has lesser points on boundary constraints. This would result in evaluating the function for lesser number of points.

6. Mr. Bean Helping Out! Finally! (Points: 200)


f (n) = 1 n 0 + 1 n 1 + + 1 n n

The following relation can be derived using a little mathematics. f (n) = 1 + (n + 1)f (n 1) 2n

You can successively compute the value in the form p/q using customised BIGINT datastructures and previously stored results. This reduces the complexity of calculation signicantly.

7. Thrown Out of the Party! (Points: 200)


In graph theoretic terms, a perfect elimination ordering in a graph is an ordering of the vertices of the graph such that, for each vertex v, v and the neighbors of v that occur after v in the order form a clique. Also, a graph is chordal if each of its cycles of four or more nodes has a chord, which is an edge joining two nodes that are not adjacent in the cycle. A very nice result by (Fulkerson & Gross, 1965) proves that a graph is chordal if and only if it has a perfect elimination ordering. So you are required to identify whether the given graph is chordal or not. There are several ways to identify whether a grah is chordal or not in O(n) time. Any such implementation will clear all the test cases. [1] http://www.lsi.upc.es/valiente/graph-00-01-e.pdf

8. Garden Disaster (Points: 200)


Represent each chord in set N using the start and end angle of the chord (start < end). So each chord can be considered as an interval. Now if you represent each chord in set M using startM and endM angles of the chord (startM < endM ), they intersect if and only if EITHER (start < startM and end < endM and end > startM ) OR (end > endM and start > startM and endM > start). We pre-process the N chords in the following way: 1. Sort the chords according to their start points in ascending order and store it in arrStart. 2. Have N arrays arrStart[1..N] where array arrStart[i] will have the rst i elements of arrStart sorted according to their end points in ascending order. 3. Sort the chords according to their end points in descending order and store it in arrEnd. 4. Have N arrays arrEndStart[1..N] where arrEndStart[i] will have the rst i elements in arrEnd sorted according to their start points in ascending order. For each point in M do the following: 1. Find no. of intervals in arrStart with start < startM and let it be i. 2. Find no. of intervals in arrStartEnd[i] with end < endM and let it be X. 3. Find no. of intervals in arrEnd with end < startM and let it be Y . 4. So number of chords with (start < startM and end < endM and end > startM ) is X Y.

Similarly nd number of chords with (end > endM and start > startM and endM > start). The preprocessing with take O(N 2 ) time, and then each query of the M chords will take O(logN ) time, so total time is O(N 2 + M logN ). The naive approach takes O(M N ) time.

9. Dinner with Sachin (Points: 200)


From the language of the problem, it is clear that one can take any subset of rules and apply it in some order. This means that the same rule need not be applied twice. This immediately transforms the problem into an N -depth tree. The root node is the start conguration, C0 = [0 1 2 ... M 1]. It has N children C1 , C2 , .... CN . C1 represents the conguration reached by application of R0 on C0 , C2 represents the conguration reached by application of R1 on C0 and so on. Now we dont need to apply R0 on C1 or any of its children in this sequence. Thus we have N children at depth 1, N (N 1) children at depth 2, ... and N ! children at depth N . Also, we have generated all sequences of length 1(N sequences), length 2(N (N 1) sequences), ... length N (N ! sequences). Now we can easily see that a branch and bound DFS will fetch us the minimum number of rules to reach a target conguration. A correct implementation of DFS BnB should qualify our 1st test case and fetch you 10% marks. Now, we present two simple conditions to improve the pruning percentage. Heuristic 1: We calculate a heuristic value at tree nodes after certain depth. Count the minimum number of transpositions (swaps) required to convert the intermediate node to the target node. The maximum of such minimum number of transpositions is M 1. Now, if the sum of transpositions of rules (transposition of a rule is calculated to be the minimum number of swaps to convert [0 1 2 ... M 1] to the sequence [R[0], R[1], ..., R[M 1]]) which are yet to be explored on that particular branch of the tree is less than the minimum number of swaps to convert the intermediate node to target node, we drop the subtree at that intermediate node as the target can never be reached by any sequence of the left over rules. Note that the certain depth which we mentioned earlier means that calculate this heuristic only if the left over rules have the sum of their transpositions less than M 1. Heuristic 2: Count the number of rules which have odd number of transpositions. Now, if at an intermediate node, the path from root to it has used all the rules having odd number of transpositions, and the minimum number of transpositions required to convert this intermediate node to target node is odd, then prune the subtree at that intermediate node as it is evident that the odd number of transpositions cant be reached using remaining even transposition rules. Implementation of either Heuristic 1 or 2 will fetch you 30% more marks, and the implementation of both heuristics or any other heuristic better than this will fetch you the full marks.

10. Showtime! (Points: 300)

The problem boils down to the following statement: Given a set of rays parallel to y-axis and a point, does there exist a line through the point which intersects all the rays. Using dual plane transformations: Point (a, b) transforms to Line y = ax b. So, a line y = mx + c transforms to a point (m, c), and the rays get transformed into half planes. The problem gets transformed in the dual plane into: Given a set of half planes and a line, does there exist a point on the line which lies in each of the half planes. (i.e. does the line intersect the convex region formed by the intersection of the half planes) So to nd the convex region from the half planes: One way of doing this is to nd the dual plane of this plane, (the original plane). The intersection of the upper half planes is computed by computing the upper convex hull of corresponding points. Similarly intersection of lower half planes can be computed. Then their intersection is computed. This takes O(nlogn) time. Now we have set of points of the convex polygon (Convex open spaces are excluded according to the problem statement). We have to nd if the query line intersects the polygon. We have N points v0 , v1 , ... vN . Determine if the line (v0 , vn/2 ) intersects the query line.

Case 1: They do not intersect. In this case the line we are interested in is parallel and hence can intersect at most one half of the polygons v0 , v1 , ..., vn/2 and v0 , v(n/2) , ... vn . So we can recurse into that half of the polygon. Case 2: They do intersect. There are 3 sub-cases: Case 2.1 : The intersection is between the points v0 and vn/2 We are done. The line intersects the polygon. Case 2.2 : The intersection is closer to v0 (that is, outside the polygon on v0 s side). Determine if the query line intersects with the line (v0 , v1 ). If it does not then we are done, the line does not intersect the polygon. If it does, nd the intersection and let it be p. If p and v1 are on the same side of the line (v0 , vn/2 ) then recurse into the half of the polygon, v0 , v1 , ..., vn/2 , otherwise recurse to the half v0 , vn/2 , ... vn . Case 2.3 : The intersection is closer to vn/2 . Deal it as case 2.2. Each query takes log(n) time.

You might also like