CT35948

The Longest Trail of Rootwater

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

It is audit day at the mangrove channels of Rootwater. Admiral spreads the bridges records across the table and picks up a sharp pencil. 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 <= 3977; the input is a connected tree.

Example 1:

Input: 2682 420 2464 1823 2187 292 1101 206 1105 380 1764 620 632 1407 2190 72 534 1084 1446 1202 2615 316 1644 554 1218 100 122 227 918 1262 2090 924 1251 694 2026 35 82 9 1209 102 190 198 1851 81 83 263 995 174 724 568 1258 328 1637 2227 2681 1148 1664 260 817 1571 2422 513 909 368 2506 2092 2338 140 400 724 2039 575 891 55 182 190 320 16 124 597 1718 2663 2670 1667 1902 54 58 848 1597 901 1087 360 1545 609 1277 20 36 1132 1162 131 2042 190 252 1245 2012 1091 2281 290 798 599 1690 630 1015 1 1059 957 1429 112 954 205 951 187 830 1801 2029 1204 2155 269 373 124 244 1077 1144 171 2164 6 1649 467 938 522 855 1707 2299 1048 1077 582 1161 1018 1249 720 1307 25 29 181 776 135 381 69 991 59 147 744 1027 1549 1841 928 1375 38 989 562 715 15 394 220 1810 1232 2654 534 1759 82 368 203 346 1123 1487 84 10…
Output: 35

Example 2:

Input: 2144 486 1126 21 1775 99 1587 20 393 1503 1960 1 713 481 903 547 1112 1648 1763 44 50 485 1662 1674 2138 793 1570 415 1972 43 71 4 8 297 1721 742 1804 145 512 433 1546 3 5 520 2136 1243 1458 21 306 19 317 140 349 67 217 577 1123 311 929 65 1606 659 1015 324 503 1302 1769 745 970 539 1426 173 251 1821 2124 71 1215 1459 1815 972 1691 496 602 77 109 3 2025 623 1330 52 327 697 1959 271 2083 899 1634 345 1768 147 1280 232 1730 6 23 302 699 362 590 1869 2013 328 2055 1191 1853 854 2063 171 854 1203 1460 523 1194 385 644 53 285 186 704 36 318 1509 1702 541 1348 459 842 585 1840 131 1271 1068 1220 708 841 576 1018 88 545 720 1674 1450 1806 131 157 503 1816 948 992 1316 2060 39 45 220 1133 264 1275 288 559 410 1287 113 454 178 221 155 246 5 105 222 1186 42 223 1309 1400 207 303 1692 1751 756 1143 9…
Output: 30

Example 3:

Input: 992 377 676 509 585 8 37 192 774 158 239 578 606 129 759 639 667 820 904 471 679 54 396 152 441 427 787 178 549 7 8 644 977 126 590 439 540 21 404 226 823 229 717 504 865 108 164 439 462 7 896 177 891 122 292 181 957 442 955 327 567 69 578 155 438 52 62 204 208 79 340 146 177 588 753 250 317 49 94 20 281 112 179 680 814 706 761 774 893 342 557 92 114 847 868 115 484 613 924 14 207 86 391 4 34 283 479 49 400 295 668 264 614 301 530 74 674 25 59 63 318 11 90 227 501 90 93 33 226 46 448 9 528 128 293 138 225 189 255 517 661 45 319 134 892 330 719 66 783 76 162 151 378 126 449 363 495 321 390 543 577 69 872 46 227 365 948 154 336 490 790 669 694 222 795 130 192 564 629 517 579 318 364 203 403 665 672 861 937 5 17 7 68 27 373 274 379 542 646 286 848 635 916 33 416 95 260 181 256 321 517 359 459…
Output: 25

Constraints:

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

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