CT40973

Wren's roads Longest Trail

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Morning light hits the wheat plains of Amberfield as Wren unrolls the day's manifest of roads. 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 <= 3931; the input is a connected tree.

Example 1:

Input: 3663 1234 2266 142 462 538 1407 1142 2883 730 2276 837 3042 21 31 663 2891 2066 2171 1012 1254 2139 2821 862 1141 1724 2628 1458 1921 1440 3292 399 1968 216 636 129 990 380 553 84 129 2444 3188 993 2149 1841 2816 1415 1604 296 677 1321 2133 1404 1637 88 643 1197 2761 682 3571 333 2176 1431 1465 3 35 626 706 516 856 103 2622 730 922 697 2178 2714 3560 1101 2605 82 252 246 1102 677 2762 479 1608 401 405 204 564 1083 2515 257 2358 713 923 1093 1726 1 1210 1462 2835 2236 3601 1470 3215 1752 3397 685 2581 211 955 1085 1354 322 1073 66 1052 88 111 1334 3521 955 1278 2318 2424 1982 2113 1027 1035 80 1638 1345 2018 301 1083 370 725 1189 2596 1576 1643 1484 2511 1241 1517 649 2134 1129 1281 787 1505 2184 2239 353 364 653 883 680 846 3056 3306 338 1209 1385 1685 3137 3618 1638 3423 2353 2730 485 135…
Output: 32

Example 2:

Input: 2753 255 366 188 1912 103 2166 393 1373 163 2436 1146 2746 930 1166 809 2084 140 1531 231 2537 515 668 822 1856 117 835 850 1038 1169 1812 1163 1740 800 968 520 1348 1082 1802 46 57 391 2553 922 1161 113 904 2126 2501 793 1391 40 75 965 1497 170 277 259 1050 684 1755 295 395 68 604 48 2374 229 839 207 1208 549 1833 337 1238 1406 2012 1208 1655 632 1708 56 276 1144 1155 8 34 462 2605 2371 2547 643 963 516 670 595 1313 769 2429 1242 2378 960 1303 58 356 1428 1560 539 1727 376 1756 678 1197 756 1301 504 1293 89 308 12 23 163 2648 872 2111 28 496 1231 1406 185 1996 993 1253 1733 2572 242 1431 198 990 2180 2739 39 96 356 2064 2316 2427 38 850 315 1141 349 756 508 1114 134 2365 810 1702 99 1529 355 588 769 1697 1566 2535 2391 2555 344 498 111 113 222 689 1920 2209 117 1473 552 1327 55 148 475 61…
Output: 31

Example 3:

Input: 3914 713 2626 1504 3712 904 2957 644 1433 621 829 1120 1656 64 400 3125 3196 1078 2966 523 2872 139 1830 624 712 516 1453 274 738 1415 1451 2341 3850 302 303 402 1169 2826 3120 176 1542 1344 1893 16 240 653 2010 1479 2237 1761 3188 393 1141 3444 3719 26 2715 1555 1641 1059 3766 2974 3823 895 3239 1446 1939 913 2980 972 1997 1306 2472 716 1424 1428 2168 1001 2716 1953 2355 1293 1432 112 605 203 1304 821 905 900 1904 1054 3854 1144 2103 534 2927 53 3811 526 2127 185 248 1516 1806 1194 1505 178 2169 584 985 3310 3830 1378 2787 293 2683 762 3571 177 229 489 1234 1403 3586 1743 2318 347 2372 18 2820 2168 2592 146 3177 293 2521 775 1781 1985 2035 1092 1652 206 1662 803 3792 568 2240 121 540 2357 2462 384 2384 2018 2974 1424 1633 1736 1742 467 2264 1628 3242 395 477 138 590 1906 3651 355 753 620 …
Output: 30

Constraints:

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

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