CF1238F.The Maximum Subtree

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Assume that you have kk one-dimensional segments s1,s2,…sks_1, s_2, \dots s_k (each segment is denoted by two integers — its endpoints). Then you can build the following graph on these segments. The graph consists of kk vertexes, and there is an edge between the ii-th and the jj-th vertexes (i≠ji \neq j) if and only if the segments sis_i and sjs_j intersect (there exists at least one point that belongs to both of them).

For example, if s1=[1,6],s2=[8,20],s3=[4,10],s4=[2,13],s5=[17,18]s_1 = [1, 6], s_2 = [8, 20], s_3 = [4, 10], s_4 = [2, 13], s_5 = [17, 18], then the resulting graph is the following:

A tree of size mm is good if it is possible to choose mm one-dimensional segments so that the graph built on these segments coincides with this tree.

You are given a tree, you have to find its good subtree with maximum possible size. Recall that a subtree is a connected subgraph of a tree.

Note that you have to answer qq independent queries.

假设你有 kk 条一维线段 s1,s2,…,sks_1, s_2, \dots, s_k(每条线段由两个整数——其端点——表示)。那么可以在这些线段上构造如下图:该图包含 kk 个顶点,当且仅当线段 sis_i 与 sjs_j 相交(即存在至少一个点同时属于这两条线段)时,第 ii 个顶点与第 jj 个顶点之间存在一条边(其中 i≠ji \neq j)。

例如,若 s1=[1,6],s2=[8,20],s3=[4,10],s4=[2,13],s5=[17,18]s_1 = [1, 6], s_2 = [8, 20], s_3 = [4, 10], s_4 = [2, 13], s_5 = [17, 18],则所得到的图如下所示:

大小为 mm 的树被称为“好的”,当且仅当可以选出 mm 条一维线段,使得基于这些线段所构造的图恰好等于该树。

给你一棵树,你需要找出它的“好”的子树中最大可能的大小。注意:子树是指树的一个连通子图。

请注意,你需要回答 qq 个相互独立的查询。

输入格式

The first line contains one integer qq (1≤q≤15⋅1041 \le q \le 15 \cdot 10^4) — the number of the queries.

The first line of each query contains one integer nn (2≤n≤3⋅1052 \le n \le 3 \cdot 10^5) — 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) denoting an edge between vertices xx and yy. It is guaranteed that the given graph is a tree.

It is guaranteed that the sum of all nn does not exceed 3⋅1053 \cdot 10^5.

第一行包含一个整数 qq(1≤q≤15⋅1041 \le q \le 15 \cdot 10^4)—— 查询的个数。

每个查询的第一行包含一个整数 nn(2≤n≤3⋅1052 \le n \le 3 \cdot 10^5)—— 树中顶点的数量。

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

保证所有查询中 nn 的总和不超过 3⋅1053 \cdot 10^5。

输出格式

For each query print one integer — the maximum size of a good subtree of the given tree.

对于每个查询,输出一个整数——给定树中好子树的最大大小。

输入输出样例

  • 输入#1

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

    输出#1

    8

说明/提示

In the first query there is a good subtree of size 88. The vertices belonging to this subtree are 9,4,10,2,5,1,6,3{9, 4, 10, 2, 5, 1, 6, 3}.

在第一次查询中,存在一个大小为 88 的好子树。属于该子树的顶点为 9,4,10,2,5,1,6,3{9, 4, 10, 2, 5, 1, 6, 3}。

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

首页