CF1297E.Modernization of Treeland
普及/提高-
通过率:0%
时间限制:5.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Treeland consists of n cities and n−1 two-way roads connecting pairs of cities. From every city, you can reach every other city moving only by the roads. You are right, the system of cities and roads in this country forms an undirected tree.
The government has announced a program for the modernization of urban infrastructure of some cities. You have been assigned to select an arbitrary subset of cities S to upgrade (potentially all the cities) that satisfies the following requirements:
- the subset of cities must be "connected", that is, from any city of the subset S you can get to any other city of the subset S by roads, moving only through cities from S,
- the number of "dead-ends" in S must be equal to the given number k. A city is a "dead-end" if it is the only city in S or connected to exactly one another city from S.
This shows one of the possible ways to select S (blue vertices) for a given configuration and k=4. Dead-ends are vertices with numbers 1, 4, 6 and 7.
Help Treeland upgrade its cities. Find any of the possible subsets S or determine that such a subset does not exist.
Treeland 由 n 座城市和 n−1 条双向道路组成,每条道路连接一对城市。从任意一座城市出发,仅通过道路即可到达其他任意一座城市。没错,该国的城市与道路系统构成一棵无向树。
政府已宣布一项针对部分城市基础设施现代化的计划。你被指派从中任选一个城市子集 S(可包含全部城市)进行升级,该子集需满足以下条件:
- 子集 S 必须是“连通的”,即:对于 S 中任意两座城市,均存在一条仅经过 S 中城市的道路路径将它们连通;
- S 中的“死胡同”城市数量必须恰好等于给定值 k。“死胡同”城市定义为:在 S 中是唯一城市,或在 S 中恰好只与另一座城市相连。
图中展示了对某一给定构型及 k=4 的一种可能的 S 选取方案(蓝色顶点)。死胡同城市为编号 1、4、6 和 7 的顶点。
请协助 Treeland 完成城市升级工作:找出任意一个满足条件的子集 S,或判定这样的子集不存在。
输入格式
The first line contains an integer t (1≤t≤104) — the number of test cases in the input. This is followed by the test cases themselves.
Each test case begins with a line that contains two integers n and k (2≤n≤3⋅105, 1≤k≤n) — the number of cities in Treeland and the number of "dead-end" cities required in the subset S.
This is followed by n−1 lines with road descriptions. Each road is given by two integers x and y (1≤x,y≤n; x=y) — the numbers of the cities that are connected by this road. It is guaranteed that from every city you can reach any other, moving only by the roads.
The sum of the values of n for all test cases in the input does not exceed 3⋅105.
第一行包含一个整数 t(1≤t≤104)—— 表示输入中测试用例的数量。随后是各测试用例本身。
每个测试用例以一行开始,该行包含两个整数 n 和 k(2≤n≤3⋅105,1≤k≤n)—— 分别表示树国(Treeland)中的城市数量,以及子集 S 中所需“死胡同”城市的数量。
接下来是 n−1 行,每行描述一条道路。每条道路由两个整数 x 和 y(1≤x,y≤n;x=y)给出—— 表示通过该道路相连的两座城市的编号。保证任意两座城市之间均可仅通过道路相互到达(即图是一棵树)。
所有测试用例的 n 值之和不超过 3⋅105。
输出格式
For each test case print Yes or No (in any case, upper or lower), depending on whether the answer exists or not. If the answer exists, then print an integer m (1≤m≤n) — the number of cities in the found subset. Then print m different numbers from 1 to n — the numbers of the cities in the found subset. City numbers can be printed in any order. If there are several answers, print any of them.
对于每个测试用例,根据答案是否存在,输出 Yes 或 No(大小写均可)。如果答案存在,则输出一个整数 m(1≤m≤n)—— 表示所找到子集中城市的数量;随后输出 m 个互不相同的、取值范围在 1 到 n 之间的整数 —— 表示所找到子集中各城市的编号。城市编号的输出顺序任意。若存在多个可行答案,输出任意一个即可。
输入输出样例
输入#1
4 10 4 4 5 5 2 2 1 1 3 1 9 9 10 2 7 7 8 5 6 4 3 1 2 2 3 3 4 5 3 1 2 1 3 1 4 1 5 4 1 1 2 2 4 2 3
输出#1
Yes 9 1 2 4 5 6 7 8 9 10 No Yes 4 1 3 4 5 Yes 1 4
输入解题思路,AI测评打分。不知道怎么写?