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?
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 Ai, Bi denoting the tree edges. It's guaranteed that the input is a valid tree.
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".
- 1 ≤ T ≤ 30
- 2 ≤ N ≤ 10000
- 1 ≤ Ai, Bi ≤ N
Input: 3 2 1 2 3 1 2 2 3 5 1 2 2 3 3 4 3 5 Output: NO YES YES
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 1-2 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.
|Tags||iscsi , snck15el|
|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|
Fetching successful submissions
If you are still having problems, see a sample solution here.