Chef and Trees

All submissions for this problem are available.
Read problems statements in Mandarin Chinese , Russian and Vietnamese
Chef recently read about trees. He already learnt that the trees are undirected, simple, connected graphs and has no cycles. He also knows that the tree (or graph) diameter is the largest distance of two nodes in the tree (or graph). He finds out about a challenging problem: Can we decrease the diameter of the tree if we add just a single edge? Unfortunately he is tired from a lot of reading, so could you help him to solve the problem?
Input
The first line of the input contains an integer T, denoting the number of test cases. The description of T test cases follows. The first line of each test contains an integer N, denoting the number of nodes in the tree. The next N  1 lines contain two space separated integers A_{i}, B_{i} denoting the tree edges. It's guaranteed that the input is a valid tree.
Output
For each test case, output a single line print "YES" if it is possible to add a tree an edge to decrease the diameter of the graph, otherwise print "NO".
Constraints
 1 ≤ T ≤ 30
 2 ≤ N ≤ 10000
 1 ≤ A_{i}, B_{i} ≤ N
Example
Input: 3 2 1 2 3 1 2 2 3 5 1 2 2 3 3 4 3 5 Output: NO YES YES
Explanation
Example case 1. In this example, we can use just cycle edges, or a parallel edge but these edges don't change the distance of the 12 nodes.
Example case 2. The original graph diameter is two because distance(1,3)=2, distance(1,2)=1 and distance(2,3)=1, if we connect 1,3 nodes then distance(1,3) will be 1 so the diameter decrease to 1.
Example case 3. The original graph diameter is three because dist(1,4)=dist(1,5)=3, if we connect 1,3 nodes then the diameter becomes 2.
Author:  iscsi 
Editorial  http://discuss.codechef.com/problems/MAXDIST 
Tags  iscsi , snck15el 
Date Added:  8062015 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.5, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 