{"id":670,"date":"2013-01-30T14:49:48","date_gmt":"2013-01-30T06:49:48","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=670"},"modified":"2013-02-28T09:38:27","modified_gmt":"2013-02-28T01:38:27","slug":"srm-565","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/srm-565\/","title":{"rendered":"SRM 565"},"content":{"rendered":"<h2>DIV 1 Level 3. UnknownTree<\/h2>\n<h3>Brief description: <\/h3>\n<p>&#8230; \u7ed9\u5b9a 3 + n \u4e2a\u7ed3\u70b9\u548c\u4e09\u7ec4\u8ddd\u79bb\u6807\u53f7\u3002\u3002\u8868\u793a\u5176\u4ed6 n \u4e2a\u7ed3\u70b9\u5206\u522b\u5230\u8fd9 3 \u4e2a\u7ed3\u70b9\u7684\u8ddd\u79bb\u3002\u3002<br \/>\n\u3002\u3002\u95ee\u6240\u6709\u6ee1\u8db3\u8fd9\u4e09\u7ec4\u8ddd\u79bb\u6807\u53f7\u7684\u6811\u7684\u65b9\u6848\u603b\u6570\u3002\u3002<\/p>\n<p><!--more--><\/p>\n<h3>Analysis: <\/h3>\n<p>\uff08\u76f4\u63a5\u6284\u5b98\u65b9\u9898\u89e3\u4e86\u3002\u3002\u8fd9\u4e2a\u9898\u5bf9\u6211\u8fd8\u662f\u592a\u96be\u4e86\u3002\u3002a). \u5c31\u7b97\u77e5\u9053\u8981\u9010\u6b65 reduce \u5230 1 \u4e2a\u7279\u6b8a\u7ed3\u70b9\u7684\u60c5\u51b5\u3002\u3002\u4f46\u662f\u4e5f\u4e0d\u4f1a\u505a\u3002\u3002\u601d\u7ef4\u8fde\u8d2f\u6027\u592a\u5927\u3002\u3002b). \u6709\u4e00\u4e9b\u7ec6\u8282\u5c31\u7b97\u77e5\u9053\u5199\u9519\u4e86\u4e5f\u4e0d\u77e5\u9053\u600e\u4e48 cha &#8230;<\/p>\n<p><a href=\"http:\/\/apps.topcoder.com\/wiki\/display\/tc\/SRM+565\">http:\/\/apps.topcoder.com\/wiki\/display\/tc\/SRM+565<\/a><\/p>\n<blockquote><p>It is one problem with many complications. In cases like this, it may be a good idea to first try easier versions of the problem. For example, there are three special vertices: A, B, C. What if there was just one of them?<\/p>\n<h4>If there was only one special vertex<\/h4>\n<p>This easier problem is so easier, that it has in fact appeared as <a href=\"http:\/\/community.topcoder.com\/stat?c=problem_statement&#038;pm=10805\">a topcoder problem<\/a> before. In the division 1 medium slot. Let us take the special vertex A as the root of the tree. Then we just need to count, given a list of distances dist[i] of each vertex i to the root, the number of ways to set the edges.\n<\/p><\/blockquote>\n<p><a href=\"https:\/\/www.shuizilong.com\/house\/archives\/srm-474\/\">https:\/\/www.shuizilong.com\/house\/archives\/srm-474\/<\/a><\/p>\n<blockquote><p>\nThe solution is quite simple. Each vertex i must have only one parent (possibly the root). For each vertex i, the choices available for its parent are: a) The root. b) Any vertex j such that dist[j] < dist[i]. We can connect the vertex directly to the root, creating a single edge with weight dist[i] or we can connect it to another vertex that is dist[j] away to the root by creating an edge with weight (dist[i] - dist[j]). We cannot pick a vertex that has the same distance to root as i, because edges with weight 0 are not allowed. Note that these choices are independent from each other. This means that the result is equal to the product of (1 + number of vertices with dist[j] < dist[i]) for each i.\n\nAs you can see, this version is approachable. We could try to find a way to reduce the real problem to this version. It is not going to be simple. Let us first try to see what can we do when there are two special vertices.<\/p><\/blockquote>\n<pre class=\"brush: cpp; collapse: true; first-line: 1; light: false; title: f1(); toolbar: true; notranslate\" title=\"f1()\">\r\n    \/\/ The version with one root. d&#x5B;] holds the d to the root.\r\n    Int f1(VI&amp; d){\r\n        \/\/ Can be done in O(n), but O(n*n) is fast enough.\r\n        int n = SZ(d); Int res = 1; REP(i, n){\r\n            Int t = 1; REP(j, n) t += d&#x5B;j] &lt; d&#x5B;i];\r\n            res *= t;\r\n        }\r\n        return res;\r\n    }\r\n<\/pre>\n<blockquote>\n<h4>If there were two special vertices<\/h4>\n<p>Imagine a problem in which for each node, we know the distances to special vertices A and B. Let us use B as a root. The following observation is useful too: From the root (B), there may be many edges, but only one edge will be the edge that takes us closer to A. Let us call all the nodes reachable from this edge a branch; A&#8217;s branch.<\/p>\n<p><font color=\"red\">We can reinterpret the problem as deciding whether to place each of the nodes in A&#8217;s branch or outside A&#8217;s branch.<\/font> For the nodes outside A&#8217;s branch, we already know their distances to the root (B) the number of ways to count them is actually a instance of the subproblem that had only one special vertex. The nodes that belong to A&#8217;s branch, including A can be seen as a new instance of the two-special nodes problem: Just use the node that is closest to B as a new root.<\/p>\n<\/blockquote>\n<p><a href=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110001.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"700\" data-permalink=\"https:\/\/www.shuizilong.com\/house\/archives\/srm-565\/attachment\/700\/\" data-orig-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110001.png\" data-orig-size=\"638,425\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"d110001\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110001.png\" data-large-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110001.png\" src=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110001.png\" alt=\"\" width=\"638\" height=\"425\" class=\"aligncenter size-full wp-image-700\" \/><\/a><\/p>\n<blockquote><p>We cannot just try each possible combination of which nodes go to the branch and which do not. It would take too much time and there are many times in which the distances are such that it is not possible to take one of the decisions, or any. What can help simplify this problem is to assume that the minimum simple distance between A and B (AB) is known. This distance, combined with the known distances between each node to A and B actually determines whether or not the node belongs to the branch.<\/p><\/blockquote>\n<p><a href=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110002.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"701\" data-permalink=\"https:\/\/www.shuizilong.com\/house\/archives\/srm-565\/attachment\/701\/\" data-orig-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110002.png\" data-orig-size=\"188,194\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"d110002\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110002.png\" data-large-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110002.png\" src=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110002.png\" alt=\"\" width=\"188\" height=\"194\" class=\"aligncenter size-full wp-image-701\" \/><\/a><\/p>\n<blockquote>\n<p>Note that the dashed lines in the image do not necessarily represent single edges, but possibly multiple nodes connected by a path of edges that total each of<br \/>\nthe given distances. Assume that the node i is outside <b>A<\/b>&#8216;s branch. Then the distance from <b>A<\/b> to i must be equal to: <span class=\"segment\">AB<\/span> + <b>distanceB<\/b>[i]. If this equality is not true, then it is impossible for node i to be outside <b>A<\/b>&#8216;s branch. If the equality holds, it is impossible for node i to be<br \/>\ninside <b>A<\/b>&#8216;s branch. All of the distances used in the equality are fixed so there is actually no choice to make. <\/p>\n<p>There is an issue that might be easy to miss. It is that if the equality does not hold, there is a chance that the whole setting is not valid. If the equality does not hold, then we know that node i should belong to <b>A<\/b>&#8216;s branch, but there might be reasons for it to be impossible to have this node in that branch. If that happens, then we have a contradiction and the whole case is invalid. The following is an invalid case:<\/p>\n<\/blockquote>\n<p><a href=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110003.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"702\" data-permalink=\"https:\/\/www.shuizilong.com\/house\/archives\/srm-565\/attachment\/702\/\" data-orig-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110003.png\" data-orig-size=\"77,161\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"d110003\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110003.png\" data-large-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110003.png\" src=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110003.png\" alt=\"\" width=\"77\" height=\"161\" class=\"aligncenter size-full wp-image-702\" \/><\/a><\/p>\n<blockquote>\n<p>Imagine if the distances between i and <b>B<\/b> and between i and <b>A<\/b> were so low that it would not match what we know about <span class=\"segment\">AB<\/span>. This leads us to a contradiction, node i cannot be simultaneously so close to <b>A<\/b> and <b>B<\/b>. We want the sum of these two distances to be at least equal <span class=\"segment\">AB<\/span>. Note that this sum can be larger:<\/p>\n<\/blockquote>\n<p><a href=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110004.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"703\" data-permalink=\"https:\/\/www.shuizilong.com\/house\/archives\/srm-565\/attachment\/703\/\" data-orig-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110004.png\" data-orig-size=\"478,221\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"d110004\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110004.png\" data-large-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110004.png\" src=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110004.png\" alt=\"\" width=\"478\" height=\"221\" class=\"aligncenter size-full wp-image-703\" \/><\/a><\/p>\n<blockquote>\n<p>Those both cases are valid, in each of them the sum of the distances is greater than <span class=\"segment\">AB<\/span>.<\/p>\n<p>That is the way in which fixing <span class=\"segment\">AB<\/span> to a value can help us, as it alone decides which nodes belong outside and inside <b>A<\/b>&#8216;s branch. Then we<br \/>\ncan calculate the results for both sub-problems. Note that one of the new sub-problems is a new instance of the original problem: Given <b>A<\/b> and (a new) <b>B<\/b>, and the distances of each of the other nodes to those vertices, calculate the number of ways to assign the edges. This time, the new distance between the new <b>A<\/b> and <b>B<\/b> can be found from the previous ones. There will be a point in which the new <b>B<\/b> will be equal to <b>A<\/b>. This is the base case for the recursion. Then just verify that the distances match (else it is an invalid case). Then solve the 1 special vertex version.<\/p>\n<\/blockquote>\n<pre class=\"brush: cpp; collapse: true; first-line: 1; light: false; title: f2(); toolbar: true; notranslate\" title=\"f2()\">\r\n    Int f2(VII&amp; d){\r\n\r\n        int n = SZ(d); if (n&gt;1 &amp;&amp; d&#x5B;0].fi == d&#x5B;1].fi) return 0;\r\n        FOR(i, 1, n) d&#x5B;i].fi -= d&#x5B;0].fi;\r\n\r\n        VI bO; if (!d&#x5B;0].se){\r\n            FOR(i, 1, n){\r\n                if (d&#x5B;i].fi != d&#x5B;i].se) return 0;\r\n                bO.PB(d&#x5B;i].fi);\r\n            }\r\n            return f1(bO);\r\n        }\r\n\r\n        VII bA; int ba = d&#x5B;0].se; FOR(i, 1, n){\r\n            int ia = d&#x5B;i].se, ib = d&#x5B;i].fi;\r\n            if (ia - ba == ib) bO.PB(ib);\r\n            else {\r\n                if (ia + ib &lt; ba) return 0;\r\n                bA.PB(d&#x5B;i]);\r\n            }\r\n        }\r\n\r\n        return f1(bO) * f2(bA);\r\n    }\r\n<\/pre>\n<blockquote>\n<h4>Three special vertices<\/h4>\n<p>When we have three special vertices, things get more complicated. Let us try to reuse the logic about branches. Then we need to think of<br \/>\na single node <b>i<\/b> that divides the graph in such a way that <b>A<\/b>, <b>B<\/b> and <b>C<\/b> will each have its own branch. Basically the idea is as follows:<\/p>\n<\/blockquote>\n<p><a href=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110005.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"704\" data-permalink=\"https:\/\/www.shuizilong.com\/house\/archives\/srm-565\/attachment\/704\/\" data-orig-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110005.png\" data-orig-size=\"229,240\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"d110005\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110005.png\" data-large-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110005.png\" src=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110005.png\" alt=\"\" width=\"229\" height=\"240\" class=\"aligncenter size-full wp-image-704\" \/><\/a><\/p>\n<blockquote>\n<p>Once again, the dashed lines could possibly represent whole paths of nodes. This assumption that there<br \/>\nis a node i that behaves as a center for the graph is not always true. There are special cases like the following:<\/p>\n<\/blockquote>\n<p><a href=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110006.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"705\" data-permalink=\"https:\/\/www.shuizilong.com\/house\/archives\/srm-565\/attachment\/705\/\" data-orig-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110006.png\" data-orig-size=\"521,160\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"d110006\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110006.png\" data-large-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110006.png\" src=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110006.png\" alt=\"\" width=\"521\" height=\"160\" class=\"aligncenter size-full wp-image-705\" \/><\/a><\/p>\n<blockquote>\n<p>In those cases, one of the special nodes lies in the middle of the path between the other two. These special cases do not allow there<br \/>\nto be a single node <b>i<\/b> that behaves as the center. Let us treat the two variations of the problem separately.<\/p>\n<h4>Variation with a fixed center i<\/h4>\n<p>This time, we assume that one of the non-special vertices <b>i<\/b> acts like a center. We can just try each of the O(n) such vertices as a potential center and sum all the ways to make the tree using that center. Note that for each tree, at most one of the vertices can do this. This vertex is the unique intersection between the paths <span class=\"segment\">AB<\/span>, <span class=\"segment\">AC<\/span> and <span class=\"segment\">BC<\/span>.<\/p>\n<p>Then we can just use the same logic as the 2-special vertices case. We can use <b>i<\/b> as the root. There is a branch for each of <b>A<\/b>, <b>B<\/b> or <b>C<\/b> and some vertices lying outside the three branches. There is a decision to make: A vertex can go to one of the branches or outside. The outside vertices then make a instance of the version of the problem with 1 special node. More importantly: each of the branches and all its nodes makes a version of the problem with 2 special nodes. For example, <b>C<\/b>&#8216;s branch can be reduced to the 2 nodes problem with <b>A<\/b>=C and <b>B<\/b>=i.<\/p>\n<p>What is left is to decide where each vertex should go. This time we already know the distances between <b>i<\/b> and <b>A<\/b>, <b>i<\/b> and <b>B<\/b> and <b>i<\/b> and <b>C<\/b>. This will yield four cases.<\/p>\n<p>If we were to assume a node j lies outside all of the branches, the following happens:<\/p>\n<\/blockquote>\n<p><a href=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110007.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"706\" data-permalink=\"https:\/\/www.shuizilong.com\/house\/archives\/srm-565\/attachment\/706\/\" data-orig-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110007.png\" data-orig-size=\"238,246\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"d110007\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110007.png\" data-large-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110007.png\" src=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110007.png\" alt=\"\" width=\"238\" height=\"246\" class=\"aligncenter size-full wp-image-706\" \/><\/a><\/p>\n<p>(distanceA[j] &#8211; distanceA[i]) must be equal to (distanceB[j] &#8211; distanceB[i]) and (distanceC[j] &#8211; distanceC[i]). These values will be equal to the distance between j and i. If these subtractions are all equal (and greater than 0) then node j <i>must<\/i> lie outside all of the branches and its distance to i is known (useful when solving the 1-special node case).<\/p>\n<p>The other case is when node i belongs to one of the three branches. Let us use <b>C<\/b>&#8216;s branch as an example. The other cases are similar by symmetry.<\/p>\n<p><a href=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110008.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"707\" data-permalink=\"https:\/\/www.shuizilong.com\/house\/archives\/srm-565\/attachment\/707\/\" data-orig-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110008.png\" data-orig-size=\"272,270\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"d110008\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110008.png\" data-large-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110008.png\" src=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110008.png\" alt=\"\" width=\"272\" height=\"270\" class=\"aligncenter size-full wp-image-707\" \/><\/a><\/p>\n<blockquote><p>This time, (distanceA[j] &#8211; distanceA[i]) must be equal to (distanceB[j] &#8211; distanceB[i]). (But different to (distanceC[j] &#8211; distanceC[i]), else it would be the other case). This would mean that the node must belong to C&#8217;s branch. The distance between i and j is (distanceA[j] &#8211; distanceA[i]) and the distance between i and C is distanceC[i]. Which allows us to solve the 2 special nodes case. Note that the sum of the distances between j and i and between j and C must be at least distanceC[i], similar to the condition in the 2 special nodes case (Its solution can do this check for us).<\/p>\n<p>If instead (distanceA[j] &#8211; distanceA[i]) and (distanceC[j] &#8211; distanceC[i]) were equal, then node j must lie inside B&#8217;s branch. If (distanceB[j] &#8211; distanceB[i]) and (distanceC[j] &#8211; distanceC[i]) are equal then j must be inside A&#8217;s branch. What happens if all of the subtractions are different? This would mean the case is invalid as the node j cannot lie outside of the branches and inside any of them.<\/p>\n<p>This approach should count all the trees in which one of the nodes that are not special acts as a center.<\/p><\/blockquote>\n<pre class=\"brush: cpp; collapse: true; first-line: 1; light: false; title: f31(); toolbar: true; notranslate\" title=\"f31()\">\r\n    Int f31(VI&amp; dA, VI&amp; dB, VI&amp; dC){\r\n\r\n        Int res; int n = SZ(dA); REP(o, n){\r\n\r\n            VII bA, bB, bC; VI bO;\r\n\r\n            bA.PB(MP(0, dA&#x5B;o])), bA.PB(MP(dA&#x5B;o], 0));\r\n            bB.PB(MP(0, dB&#x5B;o])), bB.PB(MP(dB&#x5B;o], 0));\r\n            bC.PB(MP(0, dC&#x5B;o])), bC.PB(MP(dC&#x5B;o], 0));\r\n\r\n            bool valid = true; REP(i, n) if (i != o){\r\n                int da = dA&#x5B;i] - dA&#x5B;o], db = dB&#x5B;i] - dB&#x5B;o], dc = dC&#x5B;i] - dC&#x5B;o];\r\n                if (da &gt; 0 &amp;&amp; da == db &amp;&amp; db == dc) bO.PB(da);\r\n                else if (db &gt; 0 &amp;&amp; db == dc) bA.PB(MP(db, dA&#x5B;i]));\r\n                else if (dc &gt; 0 &amp;&amp; dc == da) bB.PB(MP(dc, dB&#x5B;i]));\r\n                else if (da &gt; 0 &amp;&amp; da == db) bC.PB(MP(da, dC&#x5B;i]));\r\n                else {\r\n                    valid = false;\r\n                    break;\r\n                }\r\n            }\r\n\r\n            if (valid){\r\n                SRT(bA), SRT(bB), SRT(bC);\r\n                res += f1(bO) * f2(bA) * f2(bB) * f2(bC);\r\n            }\r\n        }\r\n\r\n        return res;\r\n    }\r\n<\/pre>\n<blockquote>\n<h4>C is between A and B<\/h4>\n<p>The cases in which one of the special nodes lies between the other two are different, but can all be solved with the same logic. Let us assume <b>C<\/b> is the vertex that is between <b>A<\/b> and <b>B<\/b>. The main complication is that the distances between <b>A<\/b> and <b>C<\/b> and <b>B<\/b> and <b>C<\/b> are unknown. For now let us assume they are known. Let us use <b>C<\/b> as root. This once again allows us to divide nodes by branches. A node i can belong to <b>A<\/b>&#8216;s branch, <b>B<\/b>&#8216;s branch and outside. Then we can again reduce to the 2-nodes case or the 1-node case.<\/p>\n<p>This time, if distanceC[i] is equal to (distanceA[i] &#8211; <span class=\"segment\">AC<\/span>) and (distanceA[i] &#8211; <span class=\"segment\">AB<\/span>), then the vertex lies outside both branches. If distanceC[i] is only equal to  (distanceA[i] &#8211; <span class=\"segment\">AC<\/span>), then the node lies in <b>B<\/b>&#8216;s branch. If distanceC[i] is only equal to  (distanceB[i] &#8211; <span class=\"segment\">AB<\/span>), then i lies inside <b>A<\/b>&#8216;s branch.<\/p>\n<\/blockquote>\n<p><a href=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110009.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"708\" data-permalink=\"https:\/\/www.shuizilong.com\/house\/archives\/srm-565\/attachment\/708\/\" data-orig-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110009.png\" data-orig-size=\"603,182\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"d110009\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110009.png\" data-large-file=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110009.png\" src=\"http:\/\/apps.topcoder.com\/wiki\/download\/attachments\/95748765\/d110009.png\" alt=\"\" width=\"603\" height=\"182\" class=\"aligncenter size-full wp-image-708\" \/><\/a><\/p>\n<blockquote>\n<p>This time, if distanceC[i] is equal to (distanceA[i] &#8211; <span class=\"segment\">AC<\/span>) and (distanceA[i] &#8211; <span class=\"segment\">AB<\/span>), then the vertex lies outside both branches. If distanceC[i] is only equal to  (distanceA[i] &#8211; <span class=\"segment\">AC<\/span>), then the node lies in <b>B<\/b>&#8216;s branch. If distanceC[i] is only equal to  (distanceB[i] &#8211; <span class=\"segment\">AB<\/span>), then i lies inside <b>A<\/b>&#8216;s branch.<\/p>\n<p>This only works because we assumed to know <span class=\"segment\">AC<\/span> and <span class=\"segment\">BC<\/span>. Unfortunately, we cannot just try all possible values from 1 to 10<sup>12<\/sup> for them. The final observation is to notice that there are only few candidates for valid pairs (<span class=\"segment\">AC<\/span>, <span class=\"segment\">BC<\/span>), and they depend on the already-known distances from each node and <b>A<\/b>, <b>B<\/b> and <b>C<\/b>.<\/p>\n<p>Remember how the distances <span class=\"segment\">AC<\/span> and <span class=\"segment\">BC<\/span> determine a node&#8217;s placement? (In which branch). It can work the other<br \/>\nway. For each node i, try the possibilities: Place it outside both branches, in <b>A<\/b>&#8216;s branch or in <b>B<\/b>&#8216;s branch. Each of these settings will require a specific<br \/>\npair (<span class=\"segment\">AC<\/span>, <span class=\"segment\">BC<\/span>) to work.<\/p>\n<p>For example, if we assume node i is outside both branches, then distanceC[i] must be equal to (distanceA[i] &#8211; <span class=\"segment\">AC<\/span>) and (distanceA[i] &#8211; <span class=\"segment\">AB<\/span>), thus we can just get AC and AB from those equations.<\/p>\n<p>There are O(n) candidates for pairs (<span class=\"segment\">AC<\/span>, <span class=\"segment\">BC<\/span>) we can just try them all and sum the results for each. Two trees with<br \/>\ndifferent distances (<span class=\"segment\">AC<\/span>, <span class=\"segment\">BC<\/span>) will always be different.<\/p>\n<\/blockquote>\n<pre class=\"brush: cpp; collapse: true; first-line: 1; light: false; title: f32(); toolbar: true; notranslate\" title=\"f32()\">\r\n    Int f32(VI&amp; dL, VI&amp; dR, VI&amp; dM){\r\n\r\n        set&lt;PII&gt; L; int n = SZ(dM); REP(i, n){\r\n            L.insert(MP(abs(dL&#x5B;i]-dM&#x5B;i]), abs(dR&#x5B;i]-dM&#x5B;i])));\r\n            if (dL&#x5B;i]-dM&#x5B;i]&gt;0) L.insert(MP(dL&#x5B;i]-dM&#x5B;i], dR&#x5B;i]+dM&#x5B;i]));\r\n            if (dR&#x5B;i]-dM&#x5B;i]&gt;0) L.insert(MP(dL&#x5B;i]+dM&#x5B;i], dR&#x5B;i]-dM&#x5B;i]));\r\n        }\r\n\r\n        Int res; ECH(it, L){\r\n\r\n            int lm = it-&gt;fi, mr = it-&gt;se;\r\n            VII bA, bB; VI bO;\r\n\r\n            bA.PB(MP(0, lm)), bA.PB(MP(lm, 0));\r\n            bB.PB(MP(0, mr)), bB.PB(MP(mr, 0));\r\n\r\n            bool valid = true; REP(i, n){\r\n                int dl = dL&#x5B;i] - lm, dr = dR&#x5B;i] - mr, dm = dM&#x5B;i];\r\n                if (dm == dl &amp;&amp; dm == dr) bO.PB(dm);\r\n                else if (dl == dm) bB.PB(MP(dm, dR&#x5B;i]));\r\n                else if (dr == dm) bA.PB(MP(dm, dL&#x5B;i]));\r\n                else {\r\n                    valid = false;\r\n                    break;\r\n                }\r\n            }\r\n\r\n            if (valid) {\r\n                SRT(bA), SRT(bB);\r\n                res += f1(bO) * f2(bA) * f2(bB);\r\n            }\r\n        }\r\n\r\n        return res;\r\n    }\r\n<\/pre>\n<p><a href=\"http:\/\/paste.ubuntu.com\/5572292\/\">http:\/\/paste.ubuntu.com\/5572292\/<\/a><\/p>\n<style type=\"text\/css\">\nspan.segment {\ntext-decoration: overline;\nfont-weight: bold;\n}\n<\/style>\n","protected":false},"excerpt":{"rendered":"<p>DIV 1 Level 3. UnknownTree Brief description: &#8230; \u7ed9\u5b9a 3 + n \u4e2a\u7ed3\u70b9\u548c\u4e09\u7ec4\u8ddd\u79bb\u6807\u53f7\u3002\u3002\u8868\u793a\u5176\u4ed6 n \u4e2a\u7ed3\u70b9\u5206\u522b\u5230\u8fd9 3 \u4e2a\u7ed3\u70b9\u7684\u8ddd\u79bb\u3002\u3002 \u3002\u3002\u95ee\u6240\u6709\u6ee1\u8db3\u8fd9\u4e09\u7ec4\u8ddd\u79bb\u6807\u53f7\u7684\u6811\u7684\u65b9\u6848\u603b\u6570\u3002\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[17],"tags":[],"class_list":["post-670","post","type-post","status-publish","format-standard","hentry","category-topcoder"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-aO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=670"}],"version-history":[{"count":1,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/670\/revisions"}],"predecessor-version":[{"id":699,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/670\/revisions\/699"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}