CT41914

Kavi's bridges Reckoning

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Kavi runs the lighthouse at the ferry landing of Crosswater. Today the books show a list of bridges, and the numbers must be worked out before sundown. 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 <= 2288; the input is a connected tree.

Example 1:

Input: 814 18 128 502 800 254 474 329 344 87 133 7 30 164 494 613 646 186 424 357 450 406 738 69 110 356 783 58 199 52 360 232 616 214 255 54 773 268 337 304 599 96 349 560 730 53 186 420 755 287 416 322 631 10 493 379 519 214 280 33 145 303 535 45 86 732 777 164 485 76 138 660 789 103 617 223 663 305 339 43 222 23 291 300 810 11 12 566 662 37 368 90 435 420 625 97 332 53 94 514 581 296 678 397 472 112 651 179 601 337 455 260 398 458 757 56 59 162 326 314 798 368 521 39 131 266 522 249 504 44 481 286 289 146 181 295 353 208 249 80 687 503 622 418 467 55 217 220 709 9 15 158 226 103 547 119 373 283 808 290 630 76 463 694 797 32 155 10 175 164 812 13 139 212 328 101 149 36 331 481 707 308 327 248 775 189 387 56 645 9 223 255 415 199 727 259 595 11 21 49 556 49 362 54 58 356 583 6 37 48 65 423 492 1…
Output: 23

Example 2:

Input: 30 1 7 2 9 19 22 13 18 8 23 19 30 6 15 9 27 1 4 2 8 16 29 21 26 4 5 4 10 14 20 9 14 7 13 2 3 10 17 16 21 20 28 5 19 22 24 8 11 1 6 1 2 7 12 1 16 9 25
Output: 10

Example 3:

Input: 1885 961 1487 919 1864 4 6 1317 1542 335 509 794 908 168 463 667 1657 854 1775 1500 1533 108 352 982 1877 599 637 99 447 969 1845 123 398 1460 1829 528 1254 1014 1684 1079 1439 262 298 132 1313 1413 1463 766 1024 26 592 431 1616 985 1543 64 334 1387 1520 356 574 1031 1283 55 676 1098 1299 735 1062 111 217 48 101 744 1726 67 1271 64 154 1038 1445 1387 1466 405 1122 103 712 52 742 494 850 346 479 364 502 22 56 38 315 315 1833 1201 1226 16 433 119 769 36 79 387 1598 307 1431 577 724 539 1081 516 1056 1324 1815 202 512 1131 1150 745 911 438 1561 101 209 227 319 338 738 983 1688 182 597 515 730 366 1812 142 1265 30 1301 1077 1832 723 756 135 1580 568 1403 737 1029 133 225 646 773 456 1269 981 1022 349 1449 2 19 122 355 1639 1766 106 1187 1042 1492 484 747 464 1777 29 1795 1358 1685 499 723 209 …
Output: 26

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
Kavi's bridges Reckoning - MEDIUM Coding Problem | CodeTikki