C. 树数树

    Type: Default File IO: tree 2000ms 512MiB

树数树

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

【题目描述】

牛牛有一棵 nn 个点的有根树,根为 11。

称一个长度为 mm 的序列 aa 是好的,当且仅当:

  • 对任意 i∈(1,m]i\in(1,m],aia_i 是 ai−1a_{i-1} 的祖先,或 ai−1a_{i-1} 是 aia_i 的祖先;
  • 对任意 1≤i<j≤m1\le i<j\le m,有 ai≠aja_i\ne a_j。

求最长的好序列长度。

【输入格式】

第一行一个正整数 TT,表示数据组数。

对于每组数据,第一行一个正整数 nn;接下来 n−1n-1 行,每行两个正整数 u,vu,v,表示树上的一条边。

【输出格式】

输出 TT 行,每行一个整数表示对应数据的答案。

【样例1输入】

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

【样例1输出】

7

【样例2】

见 tree2.in 和 tree2.ans。

【样例3】

见 tree3.in 和 tree3.ans。该样例满足特殊性质 A。

【样例4】

见 tree4.in 和 tree4.ans。该样例满足特殊性质 B。

【样例5】

见 tree5.in 和 tree5.ans。该样例满足特殊性质 C。

原下发包中的 tree1.in 和 tree1.ans 也一并保留。

【数据范围】

对于全部数据:1≤T≤51\le T\le5,2≤n≤1052\le n\le10^5,1≤u,v≤n1\le u,v\le n,u≠vu\ne v,输入保证是一棵树。

测试点 nn 的上界 其他限制
11--22 1010 无
33--66 20002000
77--1010 10510^5 特殊性质 A
1111--1212 特殊性质 B
1313--1414 特殊性质 C
1515--2020 无
  • 特殊性质 A:给出的树为链,其中 11 号点的度数为 11;
  • 特殊性质 B:给出的树为菊花,其中 11 号点的度数为 n−1n-1;
  • 特殊性质 C:给出的树为完全二叉树。

【文件输入输出】

本题使用文件输入输出:从 tree.in 读取输入,向 tree.out 输出答案。

【附件下载】

20260930-赛后改题

Not Claimed
Status
Done
Problem
4
Open Since
2026-10-4 12:00
Deadline
2026-10-12 23:59
Extension
24 hour(s)