CF1919D.01 Tree
提高+/省选-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There is an edge-weighted full binary tree with n leaves. A full binary tree is defined as a tree where every non-leaf vertex has exactly 2 children. For each non-leaf vertex, we label one of its children as the left child and the other as the right child.
The binary tree has a very strange property. For every non-leaf vertex, one of the edges to its children has weight 0 while the other edge has weight 1. Note that the edge with weight 0 can be connected to either its left or right child.
You forgot what the tree looks like, but luckily, you still remember some information about the leaves in the form of an array a of size n. For each i from 1 to n, ai represents the distance† from the root to the i-th leaf in dfs order‡. Determine whether there exists a full binary tree which satisfies array a. Note that you do not need to reconstruct the tree.
† The distance from vertex u to vertex v is defined as the sum of weights of the edges on the path from vertex u to vertex v.
‡ The dfs order of the leaves is found by calling the following dfs function on the root of the binary tree.
dfs_order = []
function dfs(v):
if v is leaf:
append v to the back of dfs_order
else:
dfs(left child of v)
dfs(right child of v)
dfs(root)
存在一棵带边权的满二叉树,其含有 n 个叶子节点。满二叉树定义为:每个非叶子节点恰好有两个子节点。对每个非叶子节点,我们将其两个子节点分别标记为左子节点和右子节点。
该二叉树具有一个非常特殊的性质:对每个非叶子节点,连接它与其两个子节点的两条边中,一条边的权重为 0,另一条边的权重为 1。注意:权重为 0 的边可以连接到左子节点或右子节点中的任意一个。
你已忘记了该树的具体结构,但幸运的是,你还记得关于叶子节点的部分信息,这些信息以一个长度为 n 的数组 a 的形式保存。对每个从 1 到 n 的 i,ai 表示在 DFS 序‡中第 i 个叶子节点到根节点的距离†。请判断是否存在一棵满足数组 a 的满二叉树。注意:你无需重构该树。
† 顶点 u 到顶点 v 的距离定义为从 u 到 v 的路径上所有边的权重之和。
‡ 叶子节点的 DFS 序通过在二叉树根节点上调用如下 dfs 函数得到:
dfs_order = []
function dfs(v):
if v is leaf:
append v to the back of dfs_order
else:
dfs(left child of v)
dfs(right child of v)
dfs(root)
输入格式
Each test contains multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n (2≤n≤2⋅105) — the size of array a.
The second line of each test case contains n integers a1,a2,…,an (0≤ai≤n−1) — the distance from the root to the i-th leaf.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含一个整数 t(1≤t≤104),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105),表示数组 a 的大小。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(0≤ai≤n−1),其中 ai 表示从根节点到第 i 个叶子节点的距离。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
For each test case, print "YES" if there exists a full binary tree which satisfies array a and "NO" otherwise.
You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer).
对于每个测试用例,如果存在一棵满足数组 a 的满二叉树,则输出 "YES";否则输出 "NO"。
你可以以任意大小写形式输出每个字母(例如,"YES"、"Yes"、"yes"、"yEs" 均被视为正确答案)。
输入输出样例
输入#1
2 5 2 1 0 1 1 5 1 0 2 1 3
输出#1
YES NO
说明/提示
In the first test case, the following tree satisfies the array.

In the second test case, it can be proven that there is no full binary tree that satisfies the array.
在第一个测试用例中,以下树满足该数组。

在第二个测试用例中,可以证明不存在满足该数组的满二叉树。
输入解题思路,AI测评打分。不知道怎么写?