Answers to: FURGRAPH - editorialhttps://discuss.codechef.com/questions/80655/furgraph-editorial<h1>PROBLEM LINK:</h1>
<p><a href="http://www.codechef.com/problems/FURGRAPH">Practice</a><br>
<a href="http://www.codechef.com/APRIL16/problems/FURGRAPH">Contest</a></p>
<p><strong>Author:</strong> <a href="http://www.codechef.com/users/furko">Roman Furko</a><br>
<strong>Tester:</strong> <a href="http://www.codechef.com/users/xcwgf666">Sergey Kulik</a><br>
<strong>Editorialist:</strong> <a href="http://www.codechef.com/users/mugurelionut">Mugurel Ionut Andreica</a></p>
<h1>DIFFICULTY:</h1>
<p>HARD</p>
<h1>PREREQUISITES:</h1>
<p>Graphs,<a href="https://en.wikipedia.org/wiki/Treap">Treaps</a>,<a href="https://en.wikipedia.org/wiki/Segment_tree">Segment trees</a></p>
<h1>PROBLEM:</h1>
<p>Two players are playing a game on an undirected graph with weights on its edges. The players move in turns. At his turn, a player chooses an unmarked vertex and marks it with the player's color. The game ends when all the nodes have been marked. At the end of the game the score of each player is equal to the sum of the weights of those edges having both endpoints marked in the player's color. Let's denote by <strong>Score(i)</strong> the score of player <strong>i</strong>. During the game each of the two players tries to maximize the difference between their score and the score of the opponent (i.e. player <strong>i</strong> tries to maximize <strong>Score(i)-Score(3-i)</strong>).</p>
<p>You need to decide what is the maximum difference between the score of player 1 (i.e. the player making the first move) and that of player 2, assuming that both players play optimally.</p>
<p>In addition, the edges are added to the graph one by one and you need to find the maximum difference for each game played from scratch on the graph obtained after adding every edge.</p>
<h1>QUICK EXPLANATION:</h1>
<p>Let's define a weight for each node: <strong>w(i)</strong>=the sum of weights of all the edges incident with the node <strong>i</strong>. At their turn, the optimal move for each player is to mark the unmarked vertex <strong>i</strong> with the maximum value <strong>w(i)</strong>.</p>
<p>Let's consider these node weights sorted in descending order: <strong>w(p(1)) >= w(p(2)) >= ... >= w(p(N))</strong>. Player 1 will mark all the nodes <strong>p(i)</strong> with odd <strong>i</strong>, while player 2 will mark all the vertices <strong>p(j)</strong> with even <strong>j</strong>.</p>
<p>Let's compute the value $DIF=\sum_{i=1}^{2*i-1≤N}{w(p(2*i-1))}-\sum_{i=1}^{2*i≤N}{w(p(2*i))}$. <strong>DIF</strong> is equal to twice the maximum difference between the scores of player 1 and player 2. Why? It's easy to see that if an edge <strong>(i,j)</strong> has both endpoints marked with the same color, then its weight will be considered both in <strong>wp(i)</strong> and in <strong>wp(j)</strong>. However, if its endpoints have different colors, its weight will appear once with the sign <strong>+</strong> (for player 1) and once with the sign <strong>-</strong>, effectively contributing <strong>zero</strong> to <strong>DIF</strong>.</p>
<p>After the addition of every edge we need to update the weights of the nodes incident to it and then update <strong>DIF</strong>. In order to do this efficiently we need to use data structures which support logarithmic updates (e.g. treaps, segment trees).</p>
<h1>EXPLANATION:</h1>
<p>Let's see why the optimal strategy in a game is to always pick the unmarked vertex with the largest weight. As we saw, the difference between the score of the first player and that of the second player is twice the difference between the weights of the vertices selected by the first player and those selected by the second player. Thus, in order to maximize his score, a player will want to select vertices with larger weights. Moreover, if a player <strong>A</strong> doesn't select the unmarked vertex with the largest weight, then the other player (<strong>B</strong>) will be able to select it and, thus, player <strong>A</strong> will obtain a lower score, while player <strong>B</strong> will obtain a higher score. This is sufficient motivation for player <strong>A</strong> to pick the unmarked vertex with the largest weight.</p>
<p>We now have a way of solving the problem, but not in a very efficient way. After adding a new edge we update the weights of the endpoints of the edge, sort the vertices again (either in <strong>O(Nxlog(N))</strong> time, or in <strong>O(N)</strong> time, since we just need to change the position of at most two vertices) and then recompute <strong>DIF</strong>. This has a time complexity of <strong>O(MxNxlog(N))</strong> (or <strong>O(MxN)</strong>) and is too slow for obtaining 100 points.</p>
<p>In order to obtain 100 points we need to recompute <strong>DIF</strong> more efficiently. I will present here an approach based on using a segment tree.</p>
<p>We will create <strong>O(N+M)</strong> tuples <strong>(i,k,weight)</strong>, one for each time a vertex's weight <strong>w(i)</strong> is updated after adding a new edge. We will initially have <strong>N</strong> vertex tuples corresponding to the <strong>N</strong> initial vertices, all of them having weight zero. We also initialize all the values <strong>w(i)</strong> to <strong>0</strong>. Then we start adding the edges one by one. Let's assume that the current edge has index <strong>k</strong>, has cost <strong>c</strong> and connects the vertices <strong>i</strong> and <strong>j</strong>. If $i\neq j$ then we increment both <strong>w(i)</strong> and <strong>w(j)</strong> by <strong>c</strong> and then we add two new tuples <strong>(i,k,w(i))</strong> and <strong>(j,k,w(j))</strong>. If $i=j$ then we increment <strong>w(i)</strong> by <strong>2xc</strong> and we only add one tuple <strong>(i,k,w(i))</strong>.</p>
<p>Once we have all these tuples we sort them in decreasing order of the parameter <strong>weight</strong>. For each edge with index <strong>k</strong> we will maintain the positions of the two (or one) tuples added for this edge in the sorted order. Let these positions be <strong>pa(k)</strong> and <strong>pb(k)</strong>.</p>
<p>We will now construct a segment tree over the <strong>O(N+M)</strong> tuples, but only the <strong>N</strong> initial tuples will be <em>enabled</em>. Each node of the segment tree will contain two values: <strong>maxdif</strong>=the maximum difference that player 1 can obtain if he plays considering only the tuples enabled in this node's interval, and <strong>cnt</strong>=how many tuples are enabled in this node's interval. The answer we are interested in will always be available at the root of the segment tree.</p>
<p>We will now add edges one by one. At the same time we will maintain for each vertex <strong>i</strong> a value <strong>pos(i)</strong>=the position of the only enabled tuple corresponding to vertex <strong>i</strong> (this is because at each moment we will have <strong>exactly one</strong> tuple enabled for each vertex, i.e. <strong>N</strong> tuples enabled overall). Let's assume that the current edge is <strong>k</strong> and connects the vertices <strong>i</strong> and <strong>j</strong>. Let's consider the case $i\neq j$ first. We need to <em>disable</em> the tuples on the positions <strong>pos(i)</strong> and <strong>pos(j)</strong> and then enable the tuples on the positions <strong>pa(k)</strong> and <strong>pb(k)</strong>. Afterwards we set <strong>pos(i)=pa(k)</strong> and <strong>pos(j)=pb(k)</strong>.</p>
<p>The case $i=j$ is similar. We disable the tuple on the position <strong>pos(i)</strong>, enable the tuple on the position <strong>pa(k)</strong> and then set <strong>pos(i)=pa(k)</strong>.</p>
<p>Note that we perform <strong>O(N+M)</strong> <em>Enable</em> and <em>Disable</em> operations. In the pseudocode below you can see how to perform each of these operations in <strong>O(log(N+M))</strong> time using the segment tree.</p>
<pre>void UpdateSegmentTree(int node, int op) {
// node = segment tree leaf node corresponding to the updated tuple
// op = 1, for enabling the tuple; -1, for disabling the tuple
if (op < 0) segtree[node].maxdif = segtree[p].cnt = 0;
else {
// tuple(node) is the tuple corresponding to the segment tree leaf node *node*
segtree[node].maxdif = tuple(node).weight;
segtree[node].cnt = 1;
}
node >>= 1;
while (node >= 1) {
int left_child = (node << 1);
int right_child = left_child + 1;
segtree[node].cnt = segtree[left_child].cnt + segtree[right_child].cnt;
segtree[node].maxdif = segtree[left_child].maxdif;
if (segtree[left_child].cnt is odd) {
// Player 2 will make the first move in the tuples enabled in right_child.
segtree[node].maxdif -= segtree[right_child].maxdif;
} else {
// Player 1 will make the first move in the tuples enabled in right_child.
segtree[node].maxdif += segtree[right_child].maxdif;
}
node >>= 1;
}
}
</pre>
<p>The overall time complexity is <strong>(O(N+M)xlog(N+M))</strong>.</p>
<h1>AUTHOR'S, TESTER'S AND EDITORIALIST'S SOLUTIONS:</h1>
<p>Author's solution can be found <a href="http://www.codechef.com/download/Solutions/APRIL16/Setter/FURGRAPH.cpp">here</a>.<br>
Tester's solution can be found <a href="http://www.codechef.com/download/Solutions/APRIL16/Tester/FURGRAPH.cpp">here</a>.<br>
Editorialist's solution can be found <a href="http://www.codechef.com/download/Solutions/APRIL16/Editorialist/FURGRAPH.cpp">here</a>.</p>enMon, 18 Apr 2016 09:14:35 +0530Answer by noopershttps://discuss.codechef.com/questions/80655/furgraph-editorial/81028<p>can anybody please tell me why my solution is giving wrong answer ? I am following editorial approach.
link: <a href="https://www.codechef.com/viewsolution/9944587">https://www.codechef.com/viewsolution/9944587</a> are there any sidecases?</p>noopersMon, 18 Apr 2016 09:14:35 +0530https://discuss.codechef.com/questions/80655/furgraph-editorial/81028Answer by kaspershttps://discuss.codechef.com/questions/80655/furgraph-editorial/80855<p>I think test cases were weak for this problem my solution with O(n^m) got accepted with 100 points...
For each edge I just placed corresponding vertices on correct position and updated the answer.
Link: <a href="https://www.codechef.com/viewsolution/9892421">https://www.codechef.com/viewsolution/9892421</a> </p>kaspersMon, 11 Apr 2016 20:27:15 +0530https://discuss.codechef.com/questions/80655/furgraph-editorial/80855