CF1037D.Valid BFS?

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The BFS algorithm is defined as follows.

  1. Consider an undirected graph with vertices numbered from 11 to nn. Initialize qq as a new queue containing only vertex 11, mark the vertex 11 as used.
  2. Extract a vertex vv from the head of the queue qq.
  3. Print the index of vertex vv.
  4. Iterate in arbitrary order through all such vertices uu that uu is a neighbor of vv and is not marked yet as used. Mark the vertex uu as used and insert it into the tail of the queue qq.
  5. If the queue is not empty, continue from step 2.
  6. Otherwise finish.

Since the order of choosing neighbors of each vertex can vary, it turns out that there may be multiple sequences which BFS can print.

In this problem you need to check whether a given sequence corresponds to some valid BFS traversal of the given tree starting from vertex 11. The tree is an undirected graph, such that there is exactly one simple path between any two vertices.

BFS 算法定义如下:

  1. 考虑一个顶点编号为 11 到 nn 的无向图。初始化一个新队列 qq,其中仅包含顶点 11,并将顶点 11 标记为已访问。
  2. 从队列 qq 的队首取出一个顶点 vv。
  3. 输出顶点 vv 的编号。
  4. 以任意顺序遍历所有满足以下条件的顶点 uu:uu 是 vv 的邻接顶点,且尚未被标记为已访问。将顶点 uu 标记为已访问,并将其插入队列 qq 的队尾。
  5. 若队列非空,则返回步骤 2 继续执行。
  6. 否则算法结束。

由于在遍历每个顶点的邻接顶点时顺序可以任意选择,因此 BFS 可能输出多种不同的序列。

本题中,你需要判断给定的序列是否对应于以顶点 11 为起点、对给定树所进行的某次合法 BFS 遍历。所谓树,是指一种无向图,其中任意两个顶点之间恰好存在唯一一条简单路径。

输入格式

The first line contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) which denotes the number of nodes in the tree.

The following n−1n - 1 lines describe the edges of the tree. Each of them contains two integers xx and yy (1≤x,y≤n1 \le x, y \le n) — the endpoints of the corresponding edge of the tree. It is guaranteed that the given graph is a tree.

The last line contains nn distinct integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n) — the sequence to check.

第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示树中节点的数量。

接下来的 n−1n - 1 行描述树的边。每行包含两个整数 xx 和 yy(1≤x,y≤n1 \le x, y \le n),表示对应树边的两个端点。保证所给图是一棵树。

最后一行包含 nn 个互不相同的整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n),即待检验的序列。

输出格式

Print "Yes" (quotes for clarity) if the sequence corresponds to some valid BFS traversal of the given tree and "No" (quotes for clarity) otherwise.

You can print each letter in any case (upper or lower).

如果该序列对应于给定树的某个有效的 BFS 遍历,则输出 "Yes"(引号仅用于明确显示);否则输出 "No"(引号仅用于明确显示)。

每个字母可使用任意大小写形式输出(大写或小写均可)。

输入输出样例

  • 输入#1

    4
    1 2
    1 3
    2 4
    1 2 3 4

    输出#1

    Yes
  • 输入#2

    4
    1 2
    1 3
    2 4
    1 2 4 3

    输出#2

    No

说明/提示

Both sample tests have the same tree in them.

In this tree, there are two valid BFS orderings:

  • 1,2,3,41, 2, 3, 4,
  • 1,3,2,41, 3, 2, 4.

The ordering 1,2,4,31, 2, 4, 3 doesn't correspond to any valid BFS order.

两个样例测试用例中包含相同的树。

在此树中,存在两种合法的 BFS 遍历顺序:

  • 1,2,3,41, 2, 3, 4,
  • 1,3,2,41, 3, 2, 4。

而顺序 1,2,4,31, 2, 4, 3 并不对应任何合法的 BFS 遍历顺序。

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

首页