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∗^{\text{∗}} pp of length nn.

Determine if there exists an undirected tree with nn vertices labeled 1,2,…,n1, 2, \dots, n, satisfying the following condition:

  • Let uu and vv (1≤u<v≤n1\leq {\color{red}{u \lt v}} \leq n) be any two vertices connected by an edge. Then uu appears before vv in pp.

∗^{\text{∗}}A permutation of length nn is an array that contains every integer from 11 to nn exactly once, in any order.

“竟敢妄想坐在我身边,真是不知天高地厚!认清自己的位置吧!”

——克莱尔·弗朗索瓦

本题为简单版本。简单版与困难版的唯一区别在于:困难版要求你构造出一棵满足条件的树。

作为一位大地法师,蕾已精通培育树木的法术!但玛娜莉娅却夸口说自己能培育出更令人惊叹的树种。蕾想起,最稀有的树种可通过某个排列所表示的公式来培育——请帮她构造出这个排列!

你将得到一个长度为 nn 的排列∗^{\text{∗}} pp。

请判断:是否存在一棵包含 nn 个顶点(编号为 1,2,…,n1, 2, \dots, n)的无向树,满足如下条件:

  • 设 uu 和 vv(其中 1≤u<v≤n1\leq {\color{red}{u \lt v}} \leq n)是任意两个由一条边直接相连的顶点,则 uu 在排列 pp 中必须出现在 vv 之前。

∗^{\text{∗}} 长度为 nn 的排列是指一个包含从 11 到 nn 的每个整数恰好一次的数组,各元素顺序任意。

输入格式

The first line contains a single integer tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052\leq n\leq 2\cdot 10^5).

The second line of each test case contains nn integers, p1,p2,…,pnp_1, p_2, \dots, p_n (1≤pi≤n1\leq p_i\leq n). It is guaranteed that all pip_i are distinct.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4)——测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052\leq n\leq 2\cdot 10^5)。

每个测试用例的第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \dots, p_n(1≤pi≤n1\leq p_i\leq n)。保证所有 pip_i 互不相同。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot 10^5。

输出格式

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{3, 1},
  • 4,1{4, 1},
  • 6,5{6, 5},
  • 6,2{6, 2},
  • 6,1{6, 1}.

Then we have that

  • 1<31 \lt 3, and 11 appears before 33 in pp,
  • 1<41 \lt 4, and 11 appears before 44 in pp,
  • 5<65 \lt 6, and 55 appears before 66 in pp,
  • 2<62 \lt 6, and 22 appears before 66 in pp,
  • 1<61 \lt 6, and 11 appears before 66 in pp.

In the second example, it can be shown that there does not exist a tree satisfying the given constraints.

在第一个例子中,我们可以构造一棵具有以下边的树:

  • 3,1{3, 1},
  • 4,1{4, 1},
  • 6,5{6, 5},
  • 6,2{6, 2},
  • 6,1{6, 1}。

此时我们有:

  • 1<31 \lt 3,且 11 在排列 pp 中出现在 33 之前,
  • 1<41 \lt 4,且 11 在排列 pp 中出现在 44 之前,
  • 5<65 \lt 6,且 55 在排列 pp 中出现在 66 之前,
  • 2<62 \lt 6,且 22 在排列 pp 中出现在 66 之前,
  • 1<61 \lt 6,且 11 在排列 pp 中出现在 66 之前。

在第二个例子中,可以证明不存在满足给定约束条件的树。

输入解题思路,AI测评打分。不知道怎么写?

首页