CT35856

Yusuf's Ledger of routes

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Yusuf the surveyor watches over the cotton rows of Whitewick's stores of routes. A careful computation stands between order and chaos. 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 <= 3372; the input is a connected tree.

Example 1:

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

Example 2:

Input: 504 25 30 300 302 177 424 92 189 355 478 178 188 119 378 89 174 110 140 19 118 7 11 28 257 50 77 325 326 119 141 179 299 15 60 104 251 15 176 320 502 30 54 39 71 302 371 287 324 195 338 172 362 125 249 31 222 1 130 12 307 51 102 8 19 230 460 199 248 29 116 5 135 352 390 169 380 199 233 389 406 1 4 55 446 219 317 155 192 429 436 45 430 16 36 74 92 15 237 400 490 82 284 14 306 215 459 39 51 271 353 10 442 69 82 28 95 105 209 16 273 71 154 125 276 55 481 178 319 231 356 203 374 71 151 20 58 179 234 34 142 139 495 99 208 10 219 192 470 386 465 46 340 224 242 162 384 131 137 43 61 220 270 334 444 163 246 76 115 274 504 47 417 14 28 6 33 1 17 304 392 320 372 246 260 89 123 286 346 130 203 9 74 3 243 15 18 363 501 12 314 226 431 399 443 89 93 68 315 411 435 208 290 60 114 72 278 80 336 16 32 97 4…
Output: 21

Example 3:

Input: 2105 1102 1547 292 1455 1259 1491 948 1628 935 1234 1346 1531 1034 1552 871 1356 454 673 958 1068 268 1480 149 894 123 531 682 1051 69 178 101 1116 727 1422 820 1122 98 2072 669 1321 595 1053 185 291 1144 1391 1262 1674 303 955 84 1581 182 225 1221 1476 796 1075 906 1028 1218 1808 944 1680 947 1230 937 2042 728 975 608 668 1234 1460 695 1921 89 939 16 42 613 718 1024 1188 663 772 212 270 222 1587 675 1130 65 145 320 1121 718 1775 440 987 616 880 83 778 607 841 634 749 519 674 555 1499 577 1488 237 512 1593 1634 488 584 57 452 1122 1915 30 139 640 779 668 884 471 499 1109 1497 549 927 1913 1959 111 356 44 1048 791 1292 166 1314 700 1097 816 1780 30 235 435 1752 715 1753 1580 1724 218 560 202 1319 259 1843 65 378 1220 1425 1430 1484 578 596 1201 1374 969 1069 578 1522 13 40 453 1630 745 808 …
Output: 31

Constraints:

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

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