1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-02 22:13:28 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2022-05-02 23:44:12 +08:00
parent 7ea03594b3
commit 2a71c78585
4790 changed files with 11696 additions and 10944 deletions

View File

@@ -12,7 +12,7 @@
"translatedContent": "<p>给你一个数组 <code>pairs</code> ,其中 <code>pairs[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> ,并且满足:</p>\n\n<ul>\n\t<li><code>pairs</code> 中没有重复元素</li>\n\t<li><code>x<sub>i</sub> < y<sub>i</sub></code></li>\n</ul>\n\n<p>令 <code>ways</code> 为满足下面条件的有根树的方案数:</p>\n\n<ul>\n\t<li>树所包含的所有节点值都在 <code>pairs</code> 中。</li>\n\t<li>一个数对 <code>[x<sub>i</sub>, y<sub>i</sub>]</code> 出现在 <code>pairs</code> 中 <strong>当且仅当</strong><strong> </strong><code>x<sub>i</sub></code> 是 <code>y<sub>i</sub></code> 的祖先或者 <code>y<sub>i</sub></code> 是 <code>x<sub>i</sub></code><sub> </sub>的祖先。</li>\n\t<li><strong>注意:</strong>构造出来的树不一定是二叉树。</li>\n</ul>\n\n<p>两棵树被视为不同的方案当存在至少一个节点在两棵树中有不同的父节点。</p>\n\n<p>请你返回:</p>\n\n<ul>\n\t<li>如果 <code>ways == 0</code> ,返回 <code>0</code> 。</li>\n\t<li>如果 <code>ways == 1</code> ,返回 <code>1</code> 。</li>\n\t<li>如果 <code>ways > 1</code> ,返回 <code>2</code> 。</li>\n</ul>\n\n<p>一棵 <strong>有根树</strong> 指的是只有一个根节点的树,所有边都是从根往外的方向。</p>\n\n<p>我们称从根到一个节点路径上的任意一个节点(除去节点本身)都是该节点的 <strong>祖先</strong> 。根节点没有祖先。</p>\n\n<p> </p>\n\n<p><strong>示例 1</strong></p>\n<img src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/01/09/trees2.png\" style=\"width: 208px; height: 221px;\" />\n<pre>\n<b>输入:</b>pairs = [[1,2],[2,3]]\n<b>输出:</b>1\n<b>解释:</b>如上图所示,有且只有一个符合规定的有根树。\n</pre>\n\n<p><strong>示例 2</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/01/09/tree.png\" style=\"width: 234px; height: 241px;\" />\n<pre>\n<b>输入:</b>pairs = [[1,2],[2,3],[1,3]]\n<b>输出:</b>2\n<b>解释:</b>有多个符合规定的有根树,其中三个如上图所示。\n</pre>\n\n<p><strong>示例 3</strong></p>\n\n<pre>\n<b>输入:</b>pairs = [[1,2],[2,3],[2,4],[1,5]]\n<b>输出:</b>0\n<b>解释:</b>没有符合规定的有根树。</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 <= pairs.length <= 10<sup>5</sup></code></li>\n\t<li><code>1 <= x<sub>i </sub>< y<sub>i</sub> <= 500</code></li>\n\t<li><code>pairs</code> 中的元素互不相同。</li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 144,
"likes": 145,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
@@ -149,7 +149,7 @@
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"12.6K\", \"totalSubmission\": \"18K\", \"totalAcceptedRaw\": 12561, \"totalSubmissionRaw\": 17983, \"acRate\": \"69.8%\"}",
"stats": "{\"totalAccepted\": \"12.6K\", \"totalSubmission\": \"18.1K\", \"totalAcceptedRaw\": 12628, \"totalSubmissionRaw\": 18097, \"acRate\": \"69.8%\"}",
"hints": [
"Think inductively. The first step is to get the root. Obviously, the root should be in pairs with all the nodes. If there isn't exactly one such node, then there are 0 ways.",
"The number of pairs involving a node must be less than or equal to that number of its parent.",