CT37750

The Longest Trail at Bloomhaven

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Jatin the valuer watches over the tulip beds of Bloomhaven's stores of canals. A careful computation stands between order and chaos. 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 <= 2629; the input is a connected tree.

Example 1:

Input: 2545 1412 2366 1302 2339 208 445 1471 1980 1503 1838 101 146 1047 1428 55 102 53 332 727 1081 1439 1490 296 889 257 441 2036 2165 72 173 846 2142 1167 2354 961 1617 619 2294 533 764 2 18 806 2526 66 124 31 43 1072 1398 20 31 222 254 22 23 478 1376 988 1541 1068 1783 1315 1879 696 829 63 1553 1296 2302 396 1443 154 235 1409 1611 497 2105 335 827 2216 2407 531 1509 253 540 1107 1296 412 1739 233 898 674 1368 177 2108 342 1030 209 1245 805 1019 9 145 328 1825 205 1515 1763 1826 1468 1670 1024 1171 1957 2242 825 845 294 602 14 1380 95 1040 239 365 328 1063 1036 1863 617 1719 1631 1861 798 852 688 2493 1822 2087 2107 2203 862 2063 129 310 589 1780 117 982 174 2082 124 838 300 2320 1956 2127 1401 1800 144 1771 158 1277 137 148 895 1061 424 1524 201 1657 373 1977 249 2299 221 668 772 2486 426 739…
Output: 29

Example 2:

Input: 1888 675 1159 1197 1766 108 170 1070 1709 600 757 150 1589 367 566 7 19 37 856 447 808 907 1193 45 572 1380 1741 38 1499 91 128 361 390 613 1440 111 159 632 661 1091 1458 971 1250 286 1013 49 84 37 1692 1153 1820 1307 1663 305 1343 1490 1746 615 1481 840 1090 10 451 614 972 86 277 172 192 1113 1224 1582 1794 22 153 697 836 121 197 797 1566 380 1333 302 1620 13 67 949 1446 692 879 1198 1399 40 435 321 1736 584 1110 311 750 428 1493 752 807 137 1084 263 1411 883 1834 258 1173 115 249 304 849 158 460 482 1592 803 1045 423 615 89 614 48 327 1377 1587 4 11 31 40 132 558 87 354 590 1672 170 1232 377 697 171 286 81 1584 66 1357 671 785 100 532 102 117 562 746 91 206 211 961 5 918 114 646 36 425 383 1608 706 1174 1059 1092 3 41 41 1036 774 971 247 355 379 567 101 372 636 840 1210 1745 1318 1388 47…
Output: 24

Example 3:

Input: 1902 288 458 371 1186 457 643 652 995 322 609 530 1098 146 1185 5 603 29 40 1503 1609 934 1126 1071 1717 1522 1899 134 1732 196 1239 199 291 598 1214 17 47 831 1146 956 1255 581 720 70 975 1115 1690 146 1171 349 737 259 838 613 875 637 1692 519 1148 461 679 36 409 656 898 660 960 443 865 584 1891 379 687 73 99 415 1730 253 364 297 826 837 1351 926 1611 165 1699 262 514 1735 1822 32 468 97 470 390 1854 1321 1486 702 860 470 1209 51 1741 238 314 804 964 427 1644 187 359 1097 1785 69 76 1040 1358 456 927 233 334 791 1479 184 942 596 1163 350 392 152 1138 1232 1633 176 1211 155 213 1110 1679 954 980 704 812 575 606 71 236 896 1250 1402 1419 351 509 1155 1669 1412 1446 486 925 12 166 228 554 283 1585 137 1373 78 519 123 706 664 1248 305 475 315 632 65 315 209 904 28 614 43 851 333 462 199 719 2…
Output: 31

Constraints:

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

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