A15221 | Passable Paths (easy version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
This is an easy version of the problem. The only difference between an easy and a hard version is in the number of queries.
Polycarp grew a tree from $n$ vertices. We remind you that a tree of $n$ vertices is an undirected connected graph of $n$ vertices and $n-1$ edges that does not contain cycles.
He calls a set of vertices passable if there is such a path in the tree that passes through each vertex of this set without passing through any edge twice. The path can visit other vertices (not from this set).
In other words, a set of vertices is called passable if there is a simple path that passes through all the vertices of this set (and possibly some other).
For example, for a tree below sets $\{3, 2, 5\}$ , $\{1, 5, 4\}$ , $\{1, 4\}$ are passable, and $\{1, 3, 5\}$ , $\{1, 2, 3, 4, 5\}$ are not.
Polycarp asks you to answer $q$ queries. Each query is a set of vertices. For each query, you need to determine whether the corresponding set of vertices is passable.
Polycarp grew a tree from $n$ vertices. We remind you that a tree of $n$ vertices is an undirected connected graph of $n$ vertices and $n-1$ edges that does not contain cycles.
He calls a set of vertices passable if there is such a path in the tree that passes through each vertex of this set without passing through any edge twice. The path can visit other vertices (not from this set).
In other words, a set of vertices is called passable if there is a simple path that passes through all the vertices of this set (and possibly some other).
For example, for a tree below sets $\{3, 2, 5\}$ , $\{1, 5, 4\}$ , $\{1, 4\}$ are passable, and $\{1, 3, 5\}$ , $\{1, 2, 3, 4, 5\}$ are not.
Polycarp asks you to answer $q$ queries. Each query is a set of vertices. For each query, you need to determine whether the corresponding set of vertices is passable.
输入格式
The first line of input contains a single integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — number of vertices.
Following $n - 1$ lines a description of the tree..
Each line contains two integers $u$ and $v$ ( $1 \le u, v \le n$ , $u \ne v$ ) — indices of vertices connected by an edge.
Following line contains single integer $q$ ( $1 \le q \le 5$ ) — number of queries.
The following $2 \cdot q$ lines contain descriptions of sets.
The first line of the description contains an integer $k$ ( $1 \le k \le n$ ) — the size of the set.
The second line of the description contains $k$ of distinct integers $p_1, p_2, \dots, p_k$ ( $1 \le p_i \le n$ ) — indices of the vertices of the set.
It is guaranteed that the sum of $k$ values for all queries does not exceed $2 \cdot 10^5$ .
Following $n - 1$ lines a description of the tree..
Each line contains two integers $u$ and $v$ ( $1 \le u, v \le n$ , $u \ne v$ ) — indices of vertices connected by an edge.
Following line contains single integer $q$ ( $1 \le q \le 5$ ) — number of queries.
The following $2 \cdot q$ lines contain descriptions of sets.
The first line of the description contains an integer $k$ ( $1 \le k \le n$ ) — the size of the set.
The second line of the description contains $k$ of distinct integers $p_1, p_2, \dots, p_k$ ( $1 \le p_i \le n$ ) — indices of the vertices of the set.
It is guaranteed that the sum of $k$ values for all queries does not exceed $2 \cdot 10^5$ .
输出格式
Output $q$ lines, each of which contains the answer to the corresponding query. As an answer, output "YES" if the set is passable, and "NO" otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
输入输出样例
输入 #1
5 1 2 2 3 2 4 4 5 5 3 3 2 5 5 1 2 3 4 5 2 1 4 3 1 3 5 3 1 5 4
输出 #1
YES NO YES NO YES
输入 #2
5 1 2 3 2 2 4 5 2 4 2 3 1 3 3 4 5 3 2 3 5 1 1
输出 #2
YES NO YES YES
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted