CT45814

The Longest Trail Beneath Alabast

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

The clerk Wren has been asked to tally the donation drive. On the desk lies a long table of tunnels readings. Task: A tree has n nodes and n-1 unweighted edges. Print its diameter (in edges). Input format: First line: n. Next n-1 lines: u v. Output format: Print the diameter. Constraints: 1 <= n <= 3092; the input is a connected tree.

Example 1:

Input: 2389 45 116 1194 1263 572 646 655 899 756 868 23 159 53 69 391 1198 111 555 1264 2269 65 1980 1021 1332 207 1081 1247 1559 313 2174 238 2028 461 1151 61 428 67 2130 28 387 35 37 306 1466 1997 2160 784 1693 1448 1626 284 411 326 1608 85 426 73 1424 148 433 103 1077 521 1012 474 572 512 1839 800 1562 339 419 2 62 538 920 2377 2385 484 958 1340 1821 2 9 1004 1621 1414 1790 51 996 193 1634 796 2229 2264 2370 545 2231 754 1849 25 91 1242 1425 89 222 119 130 332 1434 141 386 1492 1555 1098 1997 722 2256 254 421 1481 2343 877 917 1891 2219 68 1402 1581 1773 1301 1312 1151 2150 641 1170 107 1998 54 160 61 67 156 490 455 1073 386 434 113 1579 895 943 719 2353 1799 2025 313 1191 836 1370 623 679 1252 1504 185 1140 406 1217 11 30 306 1611 1680 1753 535 1544 201 568 774 1880 268 617 719 1127 369 1298 …
Output: 30

Example 2:

Input: 2447 547 1904 87 111 318 2183 64 786 126 315 681 1122 51 1349 129 1147 8 52 738 1841 152 246 1268 1915 83 159 418 791 618 885 1203 1958 3 717 97 257 593 970 78 1410 603 1066 99 415 598 1438 253 1002 1105 2332 202 2132 85 366 30 144 1325 1496 772 1124 312 618 534 709 368 551 76 1878 56 61 241 332 2146 2403 684 1733 224 2427 250 287 147 1088 1443 1866 44 870 122 389 153 559 30 605 71 407 676 2035 443 586 895 1085 983 1440 940 948 217 240 265 441 1802 2390 240 322 147 186 1563 2443 326 606 616 699 441 1477 725 1480 1010 2190 146 329 182 1947 75 130 655 2104 276 1381 943 2378 266 1790 544 2413 1052 1166 241 2077 795 2272 105 215 370 1575 174 272 101 2031 580 1182 635 1302 446 779 66 957 1485 1536 485 967 128 909 51 1923 1423 1696 1006 1572 830 872 457 762 25 553 1082 1645 488 1183 4 14 4 396 1…
Output: 33

Example 3:

Input: 2117 704 840 120 1200 1055 2023 274 2091 320 2048 1249 2065 19 114 765 1256 867 1322 85 335 9 489 404 701 1055 1837 402 571 478 488 1041 1225 307 336 453 534 14 1878 832 1629 324 1089 306 1755 1106 1379 542 1746 1632 1796 321 2083 146 2107 343 1340 98 2113 1023 1236 1237 2080 363 1839 219 290 1086 1753 57 736 838 1417 823 1627 1133 1577 420 434 510 1159 556 675 416 495 1891 2117 932 1546 644 809 1117 1504 389 866 1267 1995 599 716 858 893 266 536 324 621 662 775 399 572 787 827 297 372 391 517 1493 1846 372 1581 118 184 712 1564 1321 1815 1167 1700 504 999 39 1041 163 967 573 906 245 767 83 279 40 1067 60 760 505 754 46 1396 686 1064 442 1785 644 1530 127 135 350 1743 1193 1261 49 802 221 227 336 577 351 710 173 1325 391 471 348 909 1180 1928 75 1882 1083 1195 185 1245 129 443 756 2025 393…
Output: 28

Constraints:

1 <= n <= 3092; the input is a connected tree.

Loading...
Test Cases:No test cases
No test cases available.
The Longest Trail Beneath Alabast - MEDIUM Coding Problem | CodeTikki