欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1328E.Tree Queries
Tree Queries
CF1328E · Tree Queries
- 难度:1900
- 标签:dfs and similar、graphs、trees
- 链接:https://codeforces.com/problemset/problem/1328/E
- 时间限制:2 seconds 内存限制:256 megabytes
- 出现位置:Day29-阶段模拟8-高压综合卷
英文原题面
Statement
You are given a rooted tree consisting of vertices numbered from to . The root of the tree is a vertex number . A tree is a connected undirected graph with edges. You are given queries. The -th query consists of the set of distinct vertices . Your task is to say if there is a path from the root to some vertex such that each of the given vertices is either belongs to this path or has the distance to some vertex of this path.
Input
The first line of the input contains two integers and (, ) — the number of vertices in the tree and the number of queries. Each of the next lines describes an edge of the tree. Edge is denoted by two integers and , the labels of vertices it connects ). It is guaranteed that the given edges form a tree. The next lines describe queries. The -th line describes the -th query and starts with the integer () — the number of vertices in the current query. Then integers follow: (), where is the -th vertex of the -th query. It is guaranteed that all vertices in a single query are distinct. It is guaranteed that the sum of does not exceed ().
Output
For each query, print the answer — "YES", if there is a path from the root to some vertex such that each of the given vertices is either belongs to this path or has the distance to some vertex of this path and "NO" otherwise.
样例
样例 1
输入:
10 6
1 2
1 3
1 4
2 5
2 6
3 7
7 8
7 9
9 10
4 3 8 9 10
3 2 4 6
3 2 1 5
3 4 8 2
2 6 10
3 5 4 7
输出:
YES
YES
YES
YES
NO
NO
样例解释(英文原文)
The picture corresponding to the example:
Consider the queries. The first query is . The answer is "YES" as you can choose the path from the root to the vertex . Then vertices belong to the path from to and the vertex has distance to the vertex which also belongs to this path. The second query is . The answer is "YES" as you can choose the path to the vertex . Then the vertex has distance to the vertex which belongs to this path and the vertex has distance to the vertex which belongs to this path. The third query is . The answer is "YES" as you can choose the path to the vertex and all vertices of the query belong to this path. The fourth query is . The answer is "YES" as you can choose the path to the vertex so vertices and both have distance to the vertex which belongs to this path and the vertex has distance to the vertex which belongs to this path. The fifth and the sixth queries both have answer "NO" because you cannot choose suitable vertex .
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |