LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Tree Diameter

MediumProblem #70
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

One search from outpost 1 measures how far things are from outpost 1, which is not the same question.

A national park has n outposts joined by n−1 trails. Every outpost can be reached from every other one, and between any two of them there is exactly one route - the trails form a tree.

The rangers want to know the worst case. Somewhere in the park there is a pair of outposts whose route is longer than any other, and they want to know how many trails that route uses. That number is the diameter of the tree.

Write a program that finds it.

Input

First line of input will be a single integer t - the number of testcases.
The first line of each testcase contains a single integer n - the number of outposts.
Each of the next n−1 lines contains two integers u and v - a trail joining outpost u and outpost v, walkable in both directions.

Outposts are numbered 1 to n. The trails always join all the outposts without closing a loop, so they form a tree.

Output

For every testcase print a single line with the number of trails on the longest route in the park.

Example

Input
3
7
1 2
1 3
2 4
3 5
4 6
5 7
4
1 2
1 3
1 4
1
Output
6
2
0

In the first testcase the longest route runs 6→4→2→1→3→5→7 and uses 6 trails. Notice that outpost 1 is not on either end of it - from outpost 1 nothing is more than 3 trails away. In the second testcase every trail meets at outpost 1, so any two of the others are 2 trails apart. In the third there is a single outpost and nowhere to walk.

Constraints

1≤t≤100
1≤n≤105
1≤u,v≤n and u=v
The sum of n over all testcases does not exceed 105

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit