CT35131

The Longest Trail of the Sterling routes

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

It is audit day at the moonlit lagoon of Selene. Tailor spreads the routes 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 <= 3710; the input is a connected tree.

Example 1:

Input: 973 39 501 318 470 44 615 91 800 44 132 125 248 283 461 790 871 45 168 596 843 537 689 86 111 334 684 35 75 856 947 830 904 20 32 52 95 15 483 246 465 125 177 589 832 6 17 203 887 363 537 38 67 445 527 63 152 641 961 551 758 194 201 505 544 63 207 228 318 133 635 399 719 106 212 267 279 48 870 11 343 270 493 611 908 167 408 71 519 355 359 445 890 108 322 584 945 256 287 126 130 69 70 323 653 345 646 464 889 102 271 41 153 672 745 85 252 150 300 142 320 497 622 71 390 78 225 68 171 106 613 8 514 115 972 512 830 324 768 170 697 87 112 108 135 30 74 380 909 273 367 200 491 391 638 116 247 344 650 13 396 332 859 29 601 527 957 520 938 81 692 205 741 103 351 260 329 95 172 558 861 96 386 28 54 657 755 40 256 146 403 6 41 618 738 43 792 282 547 240 508 249 543 6 422 21 44 203 554 15 385 164 190 …
Output: 27

Example 2:

Input: 3593 3351 3482 557 2652 756 2777 354 560 503 554 832 1830 1048 2315 501 1249 347 1434 840 1912 801 823 933 3060 549 1612 1717 2354 1785 3315 236 320 754 2631 1432 2218 1253 2461 489 786 449 3267 539 1511 1375 1669 1183 1366 1583 1740 907 1086 2720 3512 1242 1593 1281 1413 1240 3420 381 579 1349 1497 1526 2371 19 56 140 546 144 155 1096 1812 339 2905 624 814 24 72 1228 2850 2158 2634 755 1429 224 935 1396 2177 82 683 1492 2375 554 837 445 922 2175 3584 160 3037 7 30 127 277 625 1821 612 3073 2134 2338 1288 2758 595 1046 564 1015 426 3179 3199 3556 619 2492 826 1197 1163 2600 11 256 1078 3022 92 1236 537 774 877 1817 917 1302 43 85 479 2256 2010 2156 1187 2840 1647 2170 679 2171 755 2665 1165 2715 60 181 6 10 289 1416 228 241 869 1857 322 2991 1165 1879 660 1744 1016 1112 1019 1679 21 33 71 …
Output: 31

Example 3:

Input: 1093 58 682 425 759 740 832 126 509 644 929 377 391 349 370 858 1032 38 51 15 22 90 267 268 917 127 377 118 855 208 968 153 333 120 150 30 57 750 770 41 376 18 52 233 851 403 617 324 598 361 842 268 650 432 872 127 536 148 240 52 142 274 658 651 780 777 1022 22 43 36 77 117 387 131 205 103 630 173 353 55 520 459 886 435 838 202 612 336 544 260 789 318 1028 42 339 313 729 14 218 247 835 10 17 30 96 457 969 21 149 164 897 87 987 226 1037 299 446 235 1049 745 812 27 599 53 72 436 463 93 543 155 217 100 923 836 852 47 71 612 847 260 439 189 351 390 936 426 790 72 454 79 226 274 695 753 805 98 178 110 577 696 714 185 900 406 563 63 84 162 183 36 153 56 101 177 385 169 444 573 721 518 595 381 895 908 927 31 214 846 1066 112 670 802 834 68 227 49 865 92 121 694 1070 387 499 210 568 395 999 279 39…
Output: 28

Constraints:

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

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