CT50549

Baron's bridges Longest Trail

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Morning light hits the trading post of Goldmeadow as Baron unrolls the day's manifest of bridges. The tally will decide the next shipment. 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 <= 2626; the input is a connected tree.

Example 1:

Input: 1755 344 1467 38 108 520 741 609 1010 222 1279 1158 1307 1192 1521 209 284 318 718 382 1055 570 1754 116 1650 828 1540 578 809 213 408 1020 1718 120 329 31 695 1292 1576 76 143 650 703 507 628 1325 1422 1057 1086 469 1284 383 609 29 1275 1201 1604 892 1329 166 177 441 740 765 1101 113 670 52 74 212 512 17 112 349 456 76 1655 219 502 544 1339 369 648 359 1365 128 330 166 194 406 542 177 263 797 1568 96 223 713 747 329 559 445 793 402 1256 10 28 249 650 121 242 162 215 62 119 139 222 656 1714 418 963 611 619 216 464 367 531 79 608 901 1263 833 1614 5 280 16 449 73 77 1245 1528 341 907 1413 1586 36 967 467 822 201 378 409 1024 935 1277 1684 1699 881 1398 808 919 1338 1574 416 1338 64 445 686 1537 1082 1169 357 413 683 799 405 771 336 500 606 1083 641 807 600 1741 8 1318 739 1208 982 1312 178 …
Output: 27

Example 2:

Input: 90 2 14 6 13 29 43 6 7 24 25 14 59 41 42 2 45 44 71 61 72 30 36 8 31 2 3 3 18 12 75 8 34 14 29 18 33 14 17 11 76 4 69 44 88 23 28 3 12 2 49 10 44 1 4 1 19 19 39 25 62 31 51 37 60 3 11 75 89 21 54 6 30 25 38 14 73 16 70 6 16 51 86 3 6 11 22 42 65 6 24 25 37 11 56 35 40 23 53 37 63 15 27 17 61 4 32 11 48 22 50 42 79 33 41 28 80 19 21 4 23 35 83 56 67 33 74 13 20 17 68 4 10 45 66 18 46 2 77 4 15 34 64 24 47 55 82 38 58 60 81 65 84 8 57 2 9 16 78 22 87 30 52 6 8 5 55 22 26 4 5 1 2 24 85 10 35 7 90
Output: 12

Example 3:

Input: 1852 814 1477 500 1662 5 111 272 626 6 406 224 1161 538 1027 238 381 597 884 35 1497 438 575 322 462 691 842 657 1651 26 671 180 1208 5 78 103 993 1236 1724 152 1374 972 986 333 732 334 1142 367 844 135 450 143 203 124 156 1262 1546 223 1448 350 1523 381 643 258 361 59 645 347 488 407 814 767 1264 31 1080 290 318 302 495 744 903 772 1011 657 1563 205 1840 17 424 182 337 907 1482 203 387 100 564 197 519 11 159 2 18 548 705 463 873 328 1285 1009 1487 1252 1592 1234 1674 36 612 805 966 482 544 469 915 14 103 6 39 256 1371 1143 1639 1066 1179 79 380 1324 1346 316 569 421 727 360 1418 634 1045 110 1290 350 693 512 1149 440 1439 1571 1582 29 779 13 89 285 1328 80 378 1105 1352 434 619 1042 1556 969 1433 535 583 27 84 39 47 272 770 11 1540 919 1529 3 124 197 269 237 629 61 209 591 878 1052 1216 1…
Output: 25

Constraints:

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

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