mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
181 lines
21 KiB
JSON
181 lines
21 KiB
JSON
|
{
|
||
|
"data": {
|
||
|
"question": {
|
||
|
"questionId": "3645",
|
||
|
"questionFrontendId": "3373",
|
||
|
"boundTopicId": null,
|
||
|
"title": "Maximize the Number of Target Nodes After Connecting Trees II",
|
||
|
"titleSlug": "maximize-the-number-of-target-nodes-after-connecting-trees-ii",
|
||
|
"content": "<p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, labeled from <code>[0, n - 1]</code> and <code>[0, m - 1]</code>, respectively.</p>\n\n<p>You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code> and <code>m - 1</code>, respectively, where <code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the first tree and <code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the second tree.</p>\n\n<p>Node <code>u</code> is <strong>target</strong> to node <code>v</code> if the number of edges on the path from <code>u</code> to <code>v</code> is even. <strong>Note</strong> that a node is <em>always</em> <strong>target</strong> to itself.</p>\n\n<p>Return an array of <code>n</code> integers <code>answer</code>, where <code>answer[i]</code> is the <strong>maximum</strong> possible number of nodes that are <strong>target</strong> to node <code>i</code> of the first tree if you had to connect one node from the first tree to another node in the second tree.</p>\n\n<p><strong>Note</strong> that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>Input:</strong> <span class=\"example-io\">edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]]</span></p>\n\n<p><strong>Output:</strong> <span class=\"example-io\">[8,7,7,8,8]</span></p>\n\n<p><strong>Explanation:</strong></p>\n\n<ul>\n\t<li>For <code>i = 0</code>, connect node 0 from the first tree to node 0 from the second tree.</li>\n\t<li>For <code>i = 1</code>, connect node 1 from the first tree to node 4 from the second tree.</li>\n\t<li>For <code>i = 2</code>, connect node 2 from the first tree to node 7 from the second tree.</li>\n\t<li>For <code>i = 3</code>, connect node 3 from the first tree to node 0 from the second tree.</li>\n\t<li>For <code>i = 4</code>, connect node 4 from the first tree to node 4 from the second tree.</li>\n</ul>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2024/09/24/3982-1.png\" style=\"width: 600px; height: 169px;\" /></div>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>Input:</strong> <span class=\"example-io\">edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]]</span></p>\n\n<p><strong>Output:</strong> <span class=\"example-io\">[3,6,6,6,6]</span></p>\n\n<p><strong>Explanation:</strong></p>\n\n<p>For every <code>i</code>, connect node <code>i</code> of the first tree with any node of the second tree.</p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2024/09/24/3928-2.png\" style=\"height: 281px; width: 500px;\" /></div>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 <= n, m <= 10<sup>5</sup></code></li>\n\t<li><code>edges1.length == n - 1</code></li>\n\t<li><code>edges2.length == m - 1</code></li>\n\t<li><code>edges1[i].length == edges2[i].length == 2</code></li>\n\t<li><code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code></li>\n\t<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>\n\t<li><code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code></li>\n\t<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> < m</code></li>\n\t<li>The input is generated such that <code>edges1</code> and <code>edges2</code> represent valid trees.</li>\n</ul>\n",
|
||
|
"translatedTitle": null,
|
||
|
"translatedContent": null,
|
||
|
"isPaidOnly": false,
|
||
|
"difficulty": "Hard",
|
||
|
"likes": 60,
|
||
|
"dislikes": 4,
|
||
|
"isLiked": null,
|
||
|
"similarQuestions": "[{\"title\": \"Find Minimum Diameter After Merging Two Trees\", \"titleSlug\": \"find-minimum-diameter-after-merging-two-trees\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]",
|
||
|
"exampleTestcases": "[[0,1],[0,2],[2,3],[2,4]]\n[[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]]\n[[0,1],[0,2],[0,3],[0,4]]\n[[0,1],[1,2],[2,3]]",
|
||
|
"categoryTitle": "Algorithms",
|
||
|
"contributors": [],
|
||
|
"topicTags": [
|
||
|
{
|
||
|
"name": "Tree",
|
||
|
"slug": "tree",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
},
|
||
|
{
|
||
|
"name": "Depth-First Search",
|
||
|
"slug": "depth-first-search",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
},
|
||
|
{
|
||
|
"name": "Breadth-First Search",
|
||
|
"slug": "breadth-first-search",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
}
|
||
|
],
|
||
|
"companyTagStats": null,
|
||
|
"codeSnippets": [
|
||
|
{
|
||
|
"lang": "C++",
|
||
|
"langSlug": "cpp",
|
||
|
"code": "class Solution {\npublic:\n vector<int> maxTargetNodes(vector<vector<int>>& edges1, vector<vector<int>>& edges2) {\n \n }\n};",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Java",
|
||
|
"langSlug": "java",
|
||
|
"code": "class Solution {\n public int[] maxTargetNodes(int[][] edges1, int[][] edges2) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Python",
|
||
|
"langSlug": "python",
|
||
|
"code": "class Solution(object):\n def maxTargetNodes(self, edges1, edges2):\n \"\"\"\n :type edges1: List[List[int]]\n :type edges2: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Python3",
|
||
|
"langSlug": "python3",
|
||
|
"code": "class Solution:\n def maxTargetNodes(self, edges1: List[List[int]], edges2: List[List[int]]) -> List[int]:\n ",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "C",
|
||
|
"langSlug": "c",
|
||
|
"code": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* maxTargetNodes(int** edges1, int edges1Size, int* edges1ColSize, int** edges2, int edges2Size, int* edges2ColSize, int* returnSize) {\n \n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "C#",
|
||
|
"langSlug": "csharp",
|
||
|
"code": "public class Solution {\n public int[] MaxTargetNodes(int[][] edges1, int[][] edges2) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "JavaScript",
|
||
|
"langSlug": "javascript",
|
||
|
"code": "/**\n * @param {number[][]} edges1\n * @param {number[][]} edges2\n * @return {number[]}\n */\nvar maxTargetNodes = function(edges1, edges2) {\n \n};",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "TypeScript",
|
||
|
"langSlug": "typescript",
|
||
|
"code": "function maxTargetNodes(edges1: number[][], edges2: number[][]): number[] {\n \n};",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "PHP",
|
||
|
"langSlug": "php",
|
||
|
"code": "class Solution {\n\n /**\n * @param Integer[][] $edges1\n * @param Integer[][] $edges2\n * @return Integer[]\n */\n function maxTargetNodes($edges1, $edges2) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Swift",
|
||
|
"langSlug": "swift",
|
||
|
"code": "class Solution {\n func maxTargetNodes(_ edges1: [[Int]], _ edges2: [[Int]]) -> [Int] {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Kotlin",
|
||
|
"langSlug": "kotlin",
|
||
|
"code": "class Solution {\n fun maxTargetNodes(edges1: Array<IntArray>, edges2: Array<IntArray>): IntArray {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Dart",
|
||
|
"langSlug": "dart",
|
||
|
"code": "class Solution {\n List<int> maxTargetNodes(List<List<int>> edges1, List<List<int>> edges2) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Go",
|
||
|
"langSlug": "golang",
|
||
|
"code": "func maxTargetNodes(edges1 [][]int, edges2 [][]int) []int {\n \n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Ruby",
|
||
|
"langSlug": "ruby",
|
||
|
"code": "# @param {Integer[][]} edges1\n# @param {Integer[][]} edges2\n# @return {Integer[]}\ndef max_target_nodes(edges1, edges2)\n \nend",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Scala",
|
||
|
"langSlug": "scala",
|
||
|
"code": "object Solution {\n def maxTargetNodes(edges1: Array[Array[Int]], edges2: Array[Array[Int]]): Array[Int] = {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Rust",
|
||
|
"langSlug": "rust",
|
||
|
"code": "impl Solution {\n pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> Vec<i32> {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Racket",
|
||
|
"langSlug": "racket",
|
||
|
"code": "(define/contract (max-target-nodes edges1 edges2)\n (-> (listof (listof exact-integer?)) (listof (listof exact-integer?)) (listof exact-integer?))\n )",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Erlang",
|
||
|
"langSlug": "erlang",
|
||
|
"code": "-spec max_target_nodes(Edges1 :: [[integer()]], Edges2 :: [[integer()]]) -> [integer()].\nmax_target_nodes(Edges1, Edges2) ->\n .",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Elixir",
|
||
|
"langSlug": "elixir",
|
||
|
"code": "defmodule Solution do\n @spec max_target_nodes(edges1 :: [[integer]], edges2 :: [[integer]]) :: [integer]\n def max_target_nodes(edges1, edges2) do\n \n end\nend",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
}
|
||
|
],
|
||
|
"stats": "{\"totalAccepted\": \"5K\", \"totalSubmission\": \"8.8K\", \"totalAcceptedRaw\": 5029, \"totalSubmissionRaw\": 8800, \"acRate\": \"57.1%\"}",
|
||
|
"hints": [
|
||
|
"Compute an array <code>even</code> where <code>even[u]</code> is the number of nodes at an even distance from node <code>u</code>, for every <code>u</code> of the first tree.",
|
||
|
"Compute an array <code>odd</code> where <code>odd[u]</code> is the number of nodes at an odd distance from node <code>u</code>, for every <code>u</code> of the second tree.",
|
||
|
"<code>answer[i] = even[i]+ max(odd[1], odd[2], …, odd[m - 1])</code>"
|
||
|
],
|
||
|
"solution": null,
|
||
|
"status": null,
|
||
|
"sampleTestCase": "[[0,1],[0,2],[2,3],[2,4]]\n[[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]]",
|
||
|
"metaData": "{\n \"name\": \"maxTargetNodes\",\n \"params\": [\n {\n \"name\": \"edges1\",\n \"type\": \"integer[][]\"\n },\n {\n \"type\": \"integer[][]\",\n \"name\": \"edges2\"\n }\n ],\n \"return\": {\n \"type\": \"integer[]\"\n }\n}",
|
||
|
"judgerAvailable": true,
|
||
|
"judgeType": "large",
|
||
|
"mysqlSchemas": [],
|
||
|
"enableRunCode": true,
|
||
|
"enableTestMode": false,
|
||
|
"enableDebugger": true,
|
||
|
"envInfo": "{\"cpp\": [\"C++\", \"<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\\r\\n\\r\\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\"], \"java\": [\"Java\", \"<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>\"], \"python\": [\"Python\", \"<p><code>Python 2.7.18</code>.</p>\\r\\n\\r\\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\\\"https://docs.python.org/2/library/array.html\\\" target=\\\"_blank\\\">array</a>, <a href=\\\"https://docs.python.org/2/library/bisect.html\\\" target=\\\"_blank\\\">bisect</a>, <a href=\\\"https://docs.python.org/2/library/collections.html\\\" target=\\\"_blank\\\">collections</a>. If you need more libraries, you can import it yourself.</p>\\r\\n\\r\\n<p>For Map/TreeMap data structure, you may use <a href=\\\"http://www.grantjenks.com/docs/sortedcontainers/\\\" target=\\\"_blank\\\">sortedcontainers</a> library.</p>\\r\\n\\r\\n<p>Note that Python 2.7 <a href=\\\"https://www.python.org/dev/peps/pep-0373/\\\" target=\\\"_blank\\\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>\"], \"c\": [\"C\", \"<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n\\r\\n<p>For hash table operations, you may use <a href=\\\"https://troydhanson.github.io/uthash/\\\" target=\\\"_blank\\\">uthash</a>. \\\"uthash.h\\\" is included by default. Below are some examples:</p>\\r\\n\\r\\n<p><b>1. Adding an item to a hash.</b>\\r\\n<pre>\\r\\nstruct hash_entry {\\r\\n int id; /* we'll use this field as the key */\\r\\n char name[10];\\r\\n UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>2. Looking up an item in a hash:</b>\\r\\n<pre>\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n struct hash_entry *s;\\r\\n HASH_FIND_INT(users, &user_id, s);\\r\\n return s;\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>3. Deleting an item in a hash:</b>\\r\\n<pre>\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n HASH_DEL(users, user); \\r\\n}\\r\\n</pre>\\r\\n</p>\"], \"csharp\": [\"C#\", \"<p><a href=\\\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12\\\" target=\\\"_blank\\\">C# 12</a> with .NET 8 runtime</p>\"], \"javascript\": [\"JavaScript\", \"<p><code>Node.js 20.10.0</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.4.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/blob/v5/README.md\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\\\"https:
|
||
|
"libraryUrl": null,
|
||
|
"adminUrl": null,
|
||
|
"challengeQuestion": null,
|
||
|
"__typename": "QuestionNode"
|
||
|
}
|
||
|
}
|
||
|
}
|