CT52471

The Longest Trail at Winterholt

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Deep in the snowberry thicket of Winterholt, Librarian studies rows of bridges entries. One clean number will settle the guild's oldest argument. 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 <= 2559; the input is a connected tree.

Example 1:

Input: 406 116 129 127 130 7 91 123 295 116 278 51 92 3 32 176 355 227 254 2 21 151 196 30 125 255 269 161 259 217 388 3 26 81 149 137 147 355 397 7 218 87 284 159 216 29 65 171 249 91 375 2 127 42 370 47 319 148 377 30 386 74 322 60 156 38 106 61 87 37 323 149 391 368 382 62 203 148 194 34 38 23 188 134 227 252 306 25 39 110 118 24 40 112 305 53 170 78 243 69 198 23 226 1 145 30 100 88 353 57 90 4 199 59 253 8 14 14 18 154 399 167 297 40 102 285 294 54 93 256 334 157 358 9 15 80 103 98 101 226 240 61 98 359 398 250 346 264 327 195 205 140 173 45 225 14 59 5 19 59 304 88 97 352 406 126 389 55 128 235 367 13 25 4 231 106 157 316 405 350 354 11 30 5 152 183 351 222 271 80 119 23 61 127 329 247 312 49 311 57 113 185 309 35 115 31 77 17 51 1 3 26 43 42 339 34 42 7 8 143 232 334 340 258 310 228 404 22…
Output: 25

Example 2:

Input: 1271 401 870 342 1179 72 467 181 336 364 1097 121 522 18 726 741 907 746 779 146 866 486 1239 55 58 297 385 500 1000 328 460 131 515 60 473 194 273 620 697 484 1007 87 1103 557 774 731 766 567 841 524 527 396 994 114 119 949 954 150 309 139 150 737 1184 76 421 20 1096 40 47 222 1212 85 124 11 202 177 311 179 230 10 243 609 1221 110 967 53 105 26 111 235 474 183 659 553 1251 514 888 944 1172 108 550 175 654 409 1196 290 483 567 896 335 786 301 1157 37 1183 576 987 404 706 205 323 515 1202 304 543 107 148 401 1015 35 164 354 377 167 744 206 461 118 1266 94 561 360 1199 294 525 1123 1151 558 1237 217 594 650 956 2 37 98 256 340 443 646 1177 393 924 304 428 72 291 241 471 180 597 643 1165 45 261 206 596 576 977 33 90 303 1009 119 1068 683 855 894 1263 555 785 59 121 282 928 65 69 46 1056 17 29…
Output: 31

Example 3:

Input: 154 145 153 24 54 43 48 11 114 2 28 8 67 16 90 99 102 14 95 66 124 132 135 46 73 16 31 11 21 2 99 39 118 19 75 17 22 6 19 2 11 59 143 29 49 67 133 23 32 122 130 3 63 50 121 78 80 36 134 123 151 48 127 11 27 80 145 27 66 5 35 5 41 41 93 4 15 69 123 96 111 27 103 25 85 45 101 52 104 135 149 3 5 15 24 10 25 112 116 51 58 37 94 47 56 59 69 27 112 28 46 28 113 60 146 2 57 8 13 6 8 5 10 6 9 127 150 60 147 48 72 15 125 119 144 28 39 1 2 104 117 18 23 16 29 35 115 93 100 1 18 23 26 33 37 1 70 46 84 74 97 85 92 44 86 22 61 33 38 9 14 7 55 12 36 53 88 18 131 107 129 132 138 27 47 53 82 115 141 34 105 43 60 20 51 28 91 31 77 11 107 14 43 24 78 91 122 27 53 24 30 1 6 133 152 36 79 126 154 65 68 27 106 107 139 81 96 10 12 16 83 16 34 57 120 48 59 1 3 6 7 78 81 5 17 64 109 50 65 29 140 50 62 1 132 25 14…
Output: 17

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
The Longest Trail at Winterholt - MEDIUM Coding Problem | CodeTikki