CT38401

Paige's Longest Trail Ledger

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Caravans arrive at the shipyard of Keelwind with crates of roads, and Paige the log keeper must account for every last one. 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 <= 1624; the input is a connected tree.

Example 1:

Input: 1613 154 1200 49 401 142 1287 41 80 500 794 572 1448 27 73 705 1463 994 1593 353 1486 353 783 1104 1475 242 1073 36 1364 179 886 647 1309 159 231 544 1115 281 482 139 302 770 1252 148 248 453 1117 1576 1596 765 1546 28 137 460 1052 448 599 344 577 677 752 251 476 24 170 534 890 542 1357 7 549 267 650 477 1369 118 383 2 8 373 1020 207 288 28 194 1394 1433 298 1339 260 1494 944 1408 104 144 14 174 1198 1412 420 694 615 973 448 1180 482 724 354 1361 24 39 25 208 11 46 222 233 261 334 41 165 10 23 609 1190 306 452 116 160 199 769 129 952 780 931 45 1042 366 717 74 363 756 1404 33 1413 612 1219 385 427 715 936 484 1244 126 261 432 1344 1066 1155 777 1103 673 1471 23 555 1 11 767 874 386 515 141 670 260 668 14 917 1060 1227 44 112 1143 1285 597 782 225 1096 242 338 364 402 907 943 698 1281 428 1…
Output: 25

Example 2:

Input: 635 73 302 19 167 247 316 18 307 192 390 7 97 14 51 92 125 57 137 38 69 20 25 2 4 90 361 30 168 142 178 39 48 36 576 31 147 160 281 147 323 49 128 283 352 1 63 284 510 304 324 125 215 349 556 18 31 547 583 62 86 53 111 314 449 53 347 318 328 605 627 14 70 8 28 37 133 315 558 42 258 140 396 95 99 3 89 177 397 163 531 15 322 7 604 117 238 116 139 576 591 265 415 601 606 209 366 276 363 2 3 87 368 244 468 180 532 19 440 16 172 29 145 380 480 23 33 323 614 123 594 133 473 103 126 442 574 123 134 98 288 21 177 493 522 9 154 272 276 415 538 173 515 189 509 6 233 263 376 19 365 39 141 126 235 15 301 82 200 110 403 77 619 203 425 583 600 14 259 206 423 34 55 33 140 215 520 52 80 60 164 88 162 14 153 530 540 25 289 92 195 450 618 14 101 110 124 230 340 582 607 9 47 85 628 113 343 33 115 265 299 121…
Output: 21

Example 3:

Input: 892 55 366 451 695 121 167 299 534 110 157 108 255 346 423 132 825 214 607 206 553 406 791 100 304 282 435 237 658 225 236 58 95 74 129 50 562 455 512 334 782 236 514 72 638 144 742 396 777 7 105 4 219 139 190 8 47 152 286 12 285 501 542 64 79 172 391 380 792 111 145 389 522 15 148 529 571 254 877 149 150 406 453 744 758 182 241 631 682 443 743 2 275 27 45 549 599 33 623 24 25 527 558 576 661 539 608 192 268 41 288 418 570 79 175 87 545 569 806 440 463 34 154 422 675 473 535 155 773 67 353 338 749 89 172 128 650 253 381 103 346 93 106 181 277 380 540 58 740 479 691 181 361 721 746 519 664 280 528 282 831 131 479 374 396 764 808 546 750 42 745 428 524 477 616 21 39 97 156 4 174 121 307 83 427 65 114 355 794 49 278 43 229 1 46 3 53 170 574 415 437 10 15 158 513 336 804 500 779 13 51 605 652 …
Output: 25

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
Paige's Longest Trail Ledger - MEDIUM Coding Problem | CodeTikki