Answers to: ELEPPOND - EDITORIALhttps://discuss.codechef.com/questions/140708/eleppond-editorial<h1>PROBLEM LINK:</h1>
<p><a href="https://www.codechef.com/problems/ELEPPOND">Practice</a> <br>
<a href="https://www.codechef.com/COOK100A/problems/ELEPPOND">Contest: Division 1</a> <br>
<a href="https://www.codechef.com/COOK100B/problems/ELEPPOND">Contest: Division 2</a> </p>
<p><strong>Setter:</strong> <a href="https://www.codechef.com/users/dpraveen">Praveen Dhinwa</a> <br>
<strong>Tester:</strong> <a href="https://www.codechef.com/users/teja349">Teja Vardhan Reddy</a> <br>
<strong>Editorialist:</strong> <a href="https://www.codechef.com/users/taran_1407">Taranpreet Singh</a> </p>
<h1>DIFFICULTY:</h1>
<p>Medium-Hard</p>
<h1>PREREQUISITES:</h1>
<p>Observations, <a href="https://en.wikipedia.org/wiki/Maximum_flow_problem">Max Flow and Matching algorithms in Bipartite graphs</a> and <a href="https://en.wikipedia.org/wiki/K%C5%91nig%27s_theorem_(graph_theory)">Kőnig's theorem</a>. Understanding of Segment Tree is also required.</p>
<h1>PROBLEM:</h1>
<p>Given a pond of dimension $N*N$ with $N$ elephants standing at each side just outside pond, having their trunks expanded in the straight line without colliding with the trunk of the elephant standing at opposite side, Find out the minimum number of elephants to be removed in order to ensure that no elephant's trunk collide.</p>
<p>Further given that Trunk lengths for any side are in increasing or decreasing order only.</p>
<h1>QUICK EXPLANATION</h1>
<ul>
<li>Building graph with elephants as vertices and edges between pairs of elephants whose trunks collide. We can see, that we have a Bipartite graph.</li>
<li>Required answer is actually the maximum flow in a graph where vertices of one set are connected to the source while vertices present in the Second set are connected to sink, all edges having a unit capacity.</li>
<li>Naively doing so will lead to TLE, so, we will Build a Segment tree type flow network for that set of vertices connected to sink to reduce the number of edges. added to the graph. This work because trunk lengths of elephants are in increasing/decreasing order, assuring that one vertex of the first set will be connected to a range of consecutive vertices of the second set, for each side only. </li>
</ul>
<h1>EXPLANATION</h1>
<p>Let us consider Each elephant as a graph vertex and add edges between two vertices if the trunk of corresponding two elephants collides. Checking this can be referred in secret box below.</p>
<div class="hidden-block"> <div class="hide-bar view">View Content</div> <div class="hide-bar hide" style="display:none;">Hide Content</div> <div class="hidden-content" style="display:none;">
<p>An elephant in row x can meet an elephant in column y at position $(x, y)$ only. So, just check, if trunks of both elephants reach position $(x, y)$ or not.</p>
</div></div>
<p><strong>Lemma:</strong> Minimum number of vertices we need to remove to remove all edges is the Minimum number of elephants to remove.</p>
<p><strong>Reason:</strong> We see, that each edge represent an intersection of trunks of a pair of elephants. We need no intersection while removing the minimum number of elephants, so, Removing the minimum number of vertices to remove all edges is sufficient.</p>
<p>Now, Considering the property that No two opposite elephant's trunks can collide, we can divide vertices into two sets, one set containing elephants at North and South direction while other set containing elephants at east and west direction.</p>
<p>We know that Opposite elephants trunks do not collide, hence, there will be no edge with both end points in the same set, so we always have edges having one end in one set and opposite end in the second set. These type of graphs are also known as Bipartite Graphs.</p>
<p>So now, Our problem becomes to calculate Minimum Vertex Cover in the bipartite graph.</p>
<p>To calculate this, <a href="https://en.wikipedia.org/wiki/K%C5%91nig%27s_theorem_(graph_theory)">Kőnig's theorem</a> come to rescue, which states that the Minimum Vertex cover of a bipartite graph is the same as the maximal matching in a bipartite graph.</p>
<p>Finding the maximal matching can be done using Max Flow algorithms.</p>
<p>But the Number of edges in this approach is too much, leading to TLE verdict.</p>
<p>So, To Reduce Number of edges, we shall use the last property about trunk lengths mentioned in constraints, that lengths of trunks are always in increasing/decreasing order for all sides. Using this, It is not hard to see that Each vertex shall be incident to a consecutive range of vertices on a side.</p>
<p>Using this, let us organize the vertices in the second set in a special fashion, which uses a lesser number of edges, in Segment tree style. :D</p>
<p>If we remember segment tree correctly, we can see, that root consisted of whole range $[1, N]$, and as we moved downward, the range of vertices reduces till it gets reduces to 1.</p>
<div class="hidden-block"> <div class="hide-bar view">View Content</div> <div class="hide-bar hide" style="display:none;">Hide Content</div> <div class="hidden-content" style="display:none;">
<p><img alt="hidden text" src="https://discuss.codechef.com/upfiles/segment_tree.png"> </p>
</div></div>
<p>Suppose we have built the network, with all elements of the first set being connected to the source with edges having a unit capacity as well as vertices of the second set connected to sink with edges of unit capacity. Now, Let us make internal nodes, representing a range of nodes, with edge capacity being the number of vertices in the range of the child. The root of Segment Tree shall consist of whole range $[1, N]$. </p>
<p>The important property of segment tree that makes it useful for us today is, that we can represent any range $[L, R]$ having $1 \leq L \leq R \leq N$ using at most $logN$ nodes of segment tree. This allows us to connect any vertex of the first set with a range of vertices in the second set by connecting that vertices to at most $logN$ nodes only, leading to Number of edges being bounded by a multiple of $N*logN$, which is much better than $N*N$ edges in naive solution.</p>
<p>Since we have two sides in the second set, there will be two segment tree structures for efficiently adding edges. We can find the ranges naively too since Constraints allow $N^2$ pass over the combination of Row and Columns.</p>
<p>Hence, The final solution looks like: we can build the segment tree structures for both present in sides in the second set, Find ranges of vertices in the second set with which any vertex of the first set collides. Connect That vertex with all vertices in range using at most $logN$ additional edges and finally print the maximum flow in this Flow Network.</p>
<p><strong>Related Problem</strong> can be found <a href="http://codeforces.com/contest/1045/problem/A">here</a>.</p>
<h1>Time Complexity</h1>
<p>Initially, the pass over all combinations of row and column to calculate ranges take $O(N^2)$ time. Number of vertices in graph build are precisely $2+2*n+2*2^{\lceil log_2 N\rceil}$. The number of edges added in segment tree is of order $N*logN$ for each tree while Number of Edges added due to intersections is also $N*logN$, leading to Number of edges being a multiple of $N*logN$.</p>
<p>So, Assuming Dinic's Max flow algorithm is used, It is not hard to notice that there will be exactly $logN$ phases leading to $O(V*E*logN)$ time. Since we have $V$ of order $N$ and E of order $N*logN$, we get overall Time complexity $O(N^2*log^2N)$.</p>
<p>Proof of Number of vertices and edges in this Flow Network is left as an exercise.</p>
<h1>AUTHOR'S AND TESTER'S SOLUTIONS:</h1>
<p><a href="https://www.codechef.com/download/Solutions/COOK100/setter/ELEPPOND.cpp">Setter's solution</a><br>
<a href="https://www.codechef.com/download/Solutions/COOK100/tester/ELEPPOND.cpp">Tester's solution</a> <br>
<a href="https://www.codechef.com/download/Solutions/COOK100/editorialist/ELEPPOND.java">Editorialist's solution</a> </p>
<p>Feel free to Share your approach, If it differs. Suggestions are always welcomed. :)</p>enTue, 20 Nov 2018 04:41:07 +0530Answer by deva2802https://discuss.codechef.com/questions/140708/eleppond-editorial/140820<p>This is EXACTLY the same trick used as in bubblecup.</p>deva2802Tue, 20 Nov 2018 04:41:07 +0530https://discuss.codechef.com/questions/140708/eleppond-editorial/140820Answer by vijjji123https://discuss.codechef.com/questions/140708/eleppond-editorial/140815<p>I wanted to post an unofficial editorial for "BINSTR" but I'm afraid that someone will delete that editorial :( </p>vijjji123Tue, 20 Nov 2018 01:58:31 +0530https://discuss.codechef.com/questions/140708/eleppond-editorial/140815