CT54621

Milo's Longest Trail Ledger

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Legend says the vault under the boar woods of Tuskmore opens only for a perfect tally of tunnels. Milo means to find out. 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 <= 2597; the input is a connected tree.

Example 1:

Input: 2270 357 982 1828 2103 513 1084 200 1317 1192 1840 282 445 736 993 615 2213 477 1790 52 1546 449 723 42 62 481 616 889 957 924 1203 440 1700 516 657 49 1485 229 311 15 185 48 150 1328 1877 333 1148 1041 1676 28 145 19 2061 162 1675 1410 1948 94 205 1149 1723 1114 1791 665 1817 1057 1232 175 193 150 173 363 746 890 1587 1491 1892 369 1735 119 227 125 887 568 680 1307 2146 502 2157 650 1993 1768 2136 1622 2147 543 1250 43 122 93 562 990 1200 600 758 71 431 1692 1786 935 1439 768 1711 832 988 1413 1543 55 217 627 914 1 2060 295 621 162 273 563 2221 1836 2238 1299 2088 644 2230 674 1133 472 635 551 767 631 878 671 881 219 349 67 341 381 932 144 867 851 1411 707 1696 348 606 639 750 405 1716 6 65 21 194 160 241 1100 1325 5 83 872 1136 1068 1771 125 1294 53 95 315 729 987 1719 151 647 302 677 67…
Output: 31

Example 2:

Input: 1095 272 579 322 346 916 985 330 413 115 132 123 468 483 1069 75 703 456 951 11 15 282 427 328 529 38 779 125 193 131 472 151 877 1001 1037 355 759 223 506 236 432 525 681 14 35 307 483 377 981 521 947 50 117 151 172 206 221 502 526 839 1026 310 533 289 348 179 908 79 133 8 42 25 184 383 591 31 385 757 944 217 1083 73 359 409 480 312 730 26 267 502 783 147 229 556 557 24 188 51 753 647 685 98 233 90 650 11 122 122 202 387 834 111 154 196 797 240 331 220 383 162 522 382 507 17 66 133 352 144 316 663 854 19 27 514 716 9 26 774 923 340 1043 1 239 222 342 9 20 252 435 135 807 268 272 28 45 88 796 244 747 91 150 88 1089 364 675 45 643 291 830 756 833 109 632 55 80 346 682 99 220 265 474 108 407 716 751 14 373 411 687 86 464 94 163 342 624 273 1088 1 31 119 888 513 531 19 677 190 349 710 837 124…
Output: 26

Example 3:

Input: 1104 22 59 382 940 476 587 188 221 585 706 30 62 57 1046 78 138 487 876 511 725 8 811 869 942 425 980 398 402 188 954 229 464 404 567 309 433 513 613 104 126 195 245 306 750 101 218 35 172 304 385 145 754 361 496 125 223 41 445 30 70 51 556 835 1048 917 1016 115 380 192 199 75 279 310 779 65 164 89 113 82 188 21 334 207 484 18 543 26 37 90 882 94 459 58 1039 40 378 21 813 53 1029 108 692 370 452 226 418 170 241 271 705 471 736 172 204 200 375 2 16 488 952 886 1092 125 197 142 451 84 925 12 45 2 105 4 8 57 85 150 460 435 661 897 926 4 252 317 970 552 639 471 531 237 401 164 995 252 305 212 773 59 1038 494 524 455 747 127 379 160 198 631 1012 8 301 880 962 17 88 730 808 91 576 206 488 526 534 44 360 85 263 84 483 497 786 82 121 24 968 249 323 461 727 746 966 218 691 53 182 69 123 117 636 9 2…
Output: 22

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.