CT52187

Ulmar's Ledger of roads

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

In the gull cliffs of Screech Point, Ulmar the quartermaster keeps careful count of the roads. Inspect the delivered goods, every figure matters. 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 <= 3850; the input is a connected tree.

Example 1:

Input: 3527 888 2491 1259 2264 3076 3209 1282 1436 305 2786 15 3411 2465 3118 851 2647 223 2915 947 993 717 3198 551 1344 2237 3317 515 1632 299 2392 2964 3471 623 1307 887 1739 3478 3498 2635 3294 1134 2274 599 1327 1339 3274 82 3147 305 1957 514 1191 35 593 987 3063 155 212 19 177 1256 2683 449 2493 1155 2353 80 293 269 1732 150 627 381 1287 901 3092 256 1829 2282 2967 560 1883 602 2521 750 1567 721 883 234 3416 749 2544 998 1325 547 682 582 1266 36 143 2213 3241 596 818 516 2926 29 336 636 884 817 1861 1897 2333 335 439 378 462 8 881 487 540 1394 1681 243 487 12 2470 35 187 123 387 2049 2104 67 498 281 1664 2124 3328 977 2904 81 578 2417 3167 1516 1901 726 2986 841 863 55 2567 250 892 1145 1186 419 1153 398 1530 47 756 588 725 1791 1950 97 1069 268 866 2467 3305 123 308 19 1923 1046 2321 1186 …
Output: 28

Example 2:

Input: 1485 23 285 87 138 819 951 1176 1397 430 718 429 477 711 1412 654 1244 175 782 26 27 20 318 193 284 630 826 24 156 131 142 668 1379 787 842 166 978 98 180 17 87 6 11 357 1003 1213 1353 341 468 135 562 632 660 37 228 157 288 613 1134 21 620 49 464 11 37 203 958 44 582 253 1058 246 675 384 588 93 884 1038 1176 121 1485 226 1404 1067 1225 693 1174 354 650 336 548 342 1383 432 579 233 1229 49 67 517 694 152 595 253 1206 516 1081 783 1310 43 62 207 337 159 1298 57 984 676 1162 423 1180 276 938 447 780 13 693 501 1262 488 1088 512 531 153 466 294 474 132 928 39 65 340 791 358 367 467 1179 49 74 487 1331 127 183 11 16 907 1396 537 640 116 487 119 576 854 936 173 207 722 1399 146 220 130 861 90 892 57 66 112 955 5 29 416 535 367 1148 256 823 12 953 826 1103 153 282 532 690 17 26 1115 1296 441 519 …
Output: 26

Example 3:

Input: 2633 1057 1717 42 174 1113 1650 1611 2565 1864 2487 99 480 705 1090 579 740 297 1829 162 702 651 2301 493 909 216 1459 917 1646 1005 1831 1294 2438 1508 2617 1291 2540 311 2213 374 2024 362 741 1595 2358 1048 2537 491 2032 997 1173 257 1030 553 2099 1447 1521 226 686 236 347 349 421 601 1322 1664 1701 165 1627 276 662 125 1101 177 238 549 681 1215 2015 83 362 259 304 331 485 415 2088 446 1321 127 189 31 66 87 920 2026 2567 14 1249 1733 2612 222 263 1675 2056 409 2520 617 834 373 642 2064 2398 57 594 51 805 2280 2453 573 2298 1237 2268 885 1988 530 1670 1659 2589 2018 2490 444 531 502 1995 74 121 71 74 794 1805 16 141 1151 2100 1489 2374 434 645 900 1026 504 2542 31 34 542 731 600 1921 360 879 897 1216 170 538 5 14 1188 1697 550 1182 1041 1923 618 716 1869 2256 1296 2376 610 1414 1052 1998 …
Output: 33

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
Ulmar's Ledger of roads - MEDIUM Coding Problem | CodeTikki