CF1695D1.Tree Queries (Easy Version)

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The only difference between this problem and D2 is the bound on the size of the tree.

You are given an unrooted tree with nn vertices. There is some hidden vertex xx in that tree that you are trying to find.

To do this, you may ask kk queries v1,v2,…,vkv_1, v_2, \ldots, v_k where the viv_i are vertices in the tree. After you are finished asking all of the queries, you are given kk numbers d1,d2,…,dkd_1, d_2, \ldots, d_k, where did_i is the number of edges on the shortest path between viv_i and xx. Note that you know which distance corresponds to which query.

What is the minimum kk such that there exists some queries v1,v2,…,vkv_1, v_2, \ldots, v_k that let you always uniquely identify xx (no matter what xx is).

Note that you don't actually need to output these queries.

本题与 D2 的唯一区别在于树的大小限制。

你被给定一棵包含 nn 个顶点的无根树。该树中存在某个隐藏顶点 xx,你需要找出它。

为此,你可以提出 kk 次查询 v1,v2,…,vkv_1, v_2, \ldots, v_k,其中每个 viv_i 均为树中的一个顶点。在你完成全部查询后,你将获得 kk 个数 d1,d2,…,dkd_1, d_2, \ldots, d_k,其中 did_i 表示 viv_i 与 xx 之间最短路径上的边数。注意,你知道每个距离 did_i 对应的是哪一个查询 viv_i。

求最小的 kk,使得存在一组查询 v1,v2,…,vkv_1, v_2, \ldots, v_k,能让你无论 xx 是哪一个顶点,总能唯一确定 xx。

注意:你无需实际输出这些查询。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001 \le t \le 100). Description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤20001 \le n \le 2000) — the number of vertices in the tree.

Each of the next n−1n-1 lines contains two integers xx and yy (1≤x,y≤n1 \le x, y \le n), meaning there is an edges between vertices xx and yy in the tree.

It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of nn over all test cases does not exceed 20002000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1001 \le t \le 100)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤20001 \le n \le 2000)—— 表示树中顶点的数量。

接下来的 n−1n-1 行中,每行包含两个整数 xx 和 yy(1≤x,y≤n1 \le x, y \le n),表示树中顶点 xx 与顶点 yy 之间存在一条边。

保证所给的边构成一棵树。

保证所有测试用例的 nn 值之和不超过 20002000。

输出格式

For each test case print a single nonnegative integer, the minimum number of queries you need, on its own line.

对于每个测试用例,在单独一行中输出一个非负整数,即所需的最少查询次数。

输入输出样例

  • 输入#1

    3
    1
    2
    1 2
    10
    2 4
    2 1
    5 7
    3 10
    8 6
    6 1
    1 3
    4 7
    9 6

    输出#1

    0
    1
    2

说明/提示

In the first test case, there is only one vertex, so you don't need any queries.

In the second test case, you can ask a single query about the node 11. Then, if x=1x = 1, you will get 00, otherwise you will get 11.

在第一个测试用例中,只有一个顶点,因此你不需要进行任何查询。

在第二个测试用例中,你可以对节点 11 进行一次查询。此时,若 x=1x = 1,你将得到 00;否则,你将得到 11。

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

首页