CF1833G.Ksyusha and Chinchilla
普及+/提高
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Ksyusha has a pet chinchilla, a tree on n vertices and huge scissors. A tree is a connected graph without cycles. During a boring physics lesson Ksyusha thought about how to entertain her pet.
Chinchillas like to play with branches. A branch is a tree of 3 vertices.
The branch looks like this.
A cut is the removal of some (not yet cut) edge in the tree. Ksyusha has plenty of free time, so she can afford to make enough cuts so that the tree splits into branches. In other words, after several (possibly zero) cuts, each vertex must belong to exactly one branch.
Help Ksyusha choose the edges to be cut or tell that it is impossible.
克斯尤莎有一只宠物南美栗鼠、一棵包含 n 个顶点的树,以及一把巨大的剪刀。树是一种无环的连通图。在一次无聊的物理课上,克斯尤莎思考如何让她的宠物开心起来。
南美栗鼠喜欢玩树枝。所谓“树枝”,指一棵包含 3 个顶点的树。
这就是“树枝”的样子。
一次“剪切”是指移除树中某条(尚未被剪切过的)边。克斯尤莎有大量空闲时间,因此她可以进行足够多次剪切,使得整棵树最终被分割成若干棵“树枝”。换言之,在进行若干次(可能为零次)剪切之后,每个顶点必须恰好属于一棵“树枝”。
请帮助克斯尤莎选出需要剪切的边;若不可能实现,请说明这一点。
输入格式
The first line contains a single integer t (1≤t≤104) — number of testcases.
The first line of each testcase contains a single integer n (2≤n≤2⋅105) — the number of vertices in the tree.
The next n−1 rows of each testcase contain integers vi and ui (1≤vi,ui≤n) — the numbers of vertices that the i-th edge connects.
It is guaranteed that this set of edges forms a tree. It is also guaranteed that the sum of n over all testcases does not exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)—— 树中顶点的数量。
每个测试用例的接下来 n−1 行,每行包含两个整数 vi 和 ui(1≤vi,ui≤n)—— 表示第 i 条边所连接的两个顶点的编号。
保证这些边构成一棵树。同时保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
Print the answer for each testcase.
If the desired way to cut the tree does not exist, print −1.
Otherwise, print an integer k — the number of edges to be cut. In the next line, print k different integers ei (1≤ei<n) — numbers of the edges to be cut. If k=0, print an empty string instead.
If there are several solutions, you can print any.
对每个测试用例,输出答案。
如果不存在满足要求的砍伐树的方式,则输出 −1。
否则,先输出一个整数 k —— 需要砍断的边的数量;在下一行中,输出 k 个互不相同的整数 ei(1≤ei<n)—— 需要砍断的边的编号。若 k=0,则输出一个空行。
若存在多种解法,输出任意一种即可。
输入输出样例
输入#1
4 9 1 2 4 3 7 9 5 4 4 6 3 2 8 7 1 7 6 1 2 1 3 4 3 1 5 6 1 6 1 2 3 2 3 4 4 5 6 5 5 1 3 5 3 5 2 3 4
输出#1
2 2 8 -1 1 3 -1
输入#2
4 2 1 2 3 1 2 3 1 6 1 2 3 1 3 4 3 5 6 1 9 2 6 6 9 9 1 9 7 1 8 7 3 8 5 4 7
输出#2
-1 0 1 2 2 4 3
说明/提示
The first testcase in first test.
第一个测试用例中的第一个测试。
输入解题思路,AI测评打分。不知道怎么写?