CF2171D.Rae Taylor and Trees (easy version)
普及/提高-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
"To think a commoner would even fathom sitting next to me. Know your place!"
— Claire François
This is the easy version of the problem. The only difference between the easy and hard versions is that the hard version asks you to construct an example of a satisfactory tree.
As an Earth mage, Rae has mastered the spell of growing trees! But Manaria brags that she can grow a more impressive species of trees. Rae remembers that the most rare type of tree can be grown using a formula represented by a certain permutation — please help her construct it!
You are given a permutation∗ p of length n.
Determine if there exists an undirected tree with n vertices labeled 1,2,…,n, satisfying the following condition:
- Let u and v (1≤u<v≤n) be any two vertices connected by an edge. Then u appears before v in p.
∗A permutation of length n is an array that contains every integer from 1 to n exactly once, in any order.
“竟敢妄想坐在我身边,真是不知天高地厚!认清自己的位置吧!”
——克莱尔·弗朗索瓦
本题为简单版本。简单版与困难版的唯一区别在于:困难版要求你构造出一棵满足条件的树。
作为一位大地法师,蕾已精通培育树木的法术!但玛娜莉娅却夸口说自己能培育出更令人惊叹的树种。蕾想起,最稀有的树种可通过某个排列所表示的公式来培育——请帮她构造出这个排列!
你将得到一个长度为 n 的排列∗ p。
请判断:是否存在一棵包含 n 个顶点(编号为 1,2,…,n)的无向树,满足如下条件:
- 设 u 和 v(其中 1≤u<v≤n)是任意两个由一条边直接相连的顶点,则 u 在排列 p 中必须出现在 v 之前。
∗ 长度为 n 的排列是指一个包含从 1 到 n 的每个整数恰好一次的数组,各元素顺序任意。
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains a single integer n (2≤n≤2⋅105).
The second line of each test case contains n integers, p1,p2,…,pn (1≤pi≤n). It is guaranteed that all pi are distinct.
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)。
每个测试用例的第二行包含 n 个整数 p1,p2,…,pn(1≤pi≤n)。保证所有 pi 互不相同。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output on a single line "Yes" if there exists a tree satisfying the given condition, and "No" otherwise.
You may output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "YES", and "yeS" will be recognized as "Yes".
对于每个测试用例,如果存在一棵满足给定条件的树,则在一行中输出“Yes”;否则输出“No”。
您可以以任意大小写形式输出答案(大写或小写均可)。例如,字符串 “yEs”、“yes”、“YES” 和 “yeS” 均会被识别为 “Yes”。
输入输出样例
输入#1
9 6 1 3 4 5 2 6 4 3 4 1 2 5 4 3 5 1 2 4 1 2 3 4 7 4 3 5 7 6 2 1 6 2 4 6 1 3 5 3 2 1 3 4 2 4 1 3 6 4 2 6 5 1 3
输出#1
Yes No No Yes No Yes Yes Yes Yes
说明/提示
In the first example, we can construct the tree with the following edges:
- 3,1,
- 4,1,
- 6,5,
- 6,2,
- 6,1.
Then we have that
- 1<3, and 1 appears before 3 in p,
- 1<4, and 1 appears before 4 in p,
- 5<6, and 5 appears before 6 in p,
- 2<6, and 2 appears before 6 in p,
- 1<6, and 1 appears before 6 in p.
In the second example, it can be shown that there does not exist a tree satisfying the given constraints.
在第一个例子中,我们可以构造一棵具有以下边的树:
- 3,1,
- 4,1,
- 6,5,
- 6,2,
- 6,1。
此时我们有:
- 1<3,且 1 在排列 p 中出现在 3 之前,
- 1<4,且 1 在排列 p 中出现在 4 之前,
- 5<6,且 5 在排列 p 中出现在 6 之前,
- 2<6,且 2 在排列 p 中出现在 6 之前,
- 1<6,且 1 在排列 p 中出现在 6 之前。
在第二个例子中,可以证明不存在满足给定约束条件的树。
输入解题思路,AI测评打分。不知道怎么写?