CT39580

Scribe and the Longest Trail

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

In the silver mines of Lodebarrow, Scribe the customs officer keeps careful count of the routes. Approve the repair estimates, 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 <= 2543; the input is a connected tree.

Example 1:

Input: 748 352 574 422 427 395 567 119 516 400 537 417 433 393 480 351 605 72 171 70 732 195 263 215 250 26 39 178 639 110 138 350 565 79 720 262 407 124 244 237 422 64 676 68 111 158 454 42 538 46 267 158 704 565 609 133 330 104 121 95 112 184 523 301 656 372 619 78 200 307 743 506 671 624 653 107 215 109 232 12 420 451 455 71 78 267 389 182 415 57 426 294 416 91 328 159 601 224 410 194 431 245 499 27 257 304 402 621 630 26 371 21 207 223 351 181 405 424 741 273 505 44 156 372 397 11 469 597 642 141 593 4 166 194 396 256 526 8 62 72 148 511 708 93 170 72 515 160 376 184 203 87 481 91 133 228 334 586 736 51 452 23 625 77 270 1 2 317 457 82 174 250 316 280 395 52 251 82 201 7 626 261 688 595 738 4 322 18 100 5 734 231 453 27 30 296 344 85 228 7 23 54 107 262 286 1 58 331 611 245 530 1 12 452 460 5…
Output: 24

Example 2:

Input: 332 155 206 75 77 118 135 4 185 54 118 30 252 207 218 81 270 235 288 176 240 53 181 115 207 31 243 85 129 15 76 2 16 15 228 38 46 117 321 78 115 11 61 47 245 53 56 28 138 165 170 53 63 104 209 37 55 119 234 31 202 88 124 32 106 12 23 57 317 14 48 250 262 9 249 24 27 19 258 6 70 48 116 22 83 1 25 72 75 191 195 47 96 35 143 109 275 5 287 1 3 21 26 274 325 17 44 212 241 59 126 38 102 4 95 264 307 90 201 56 268 77 85 169 176 35 52 18 20 43 279 132 192 30 171 39 223 122 263 17 182 77 91 14 43 3 217 13 17 15 19 11 18 164 196 184 238 100 168 118 161 133 236 34 162 29 212 244 291 64 254 37 139 221 278 152 235 142 157 285 330 217 308 188 264 44 53 141 255 17 39 101 214 184 260 264 276 47 131 143 220 104 148 139 296 84 173 96 250 64 108 226 323 15 253 4 15 87 225 123 285 16 267 59 78 3 98 138 152 9 …
Output: 25

Example 3:

Input: 1414 92 735 731 1212 45 1028 905 1301 661 977 68 184 73 1087 207 670 340 727 121 219 948 993 34 1325 760 1318 438 699 458 578 516 675 32 178 34 476 80 82 188 482 75 1306 202 541 114 225 493 1377 457 856 37 49 196 1173 627 1103 216 359 81 1188 485 1391 301 634 325 367 874 1211 472 919 321 386 436 536 287 292 430 947 313 1230 1109 1374 447 644 222 268 261 965 59 61 938 1100 12 172 85 223 4 6 116 272 167 278 451 1072 315 374 823 922 33 175 162 165 261 1090 240 779 337 604 337 533 133 394 179 901 70 438 83 574 74 113 122 398 716 740 542 691 60 1205 286 569 700 1052 105 553 444 506 52 74 5 17 1025 1347 134 227 917 967 49 479 53 1060 9 121 1 21 317 862 1 12 466 607 124 260 16 18 753 863 101 136 765 1273 184 491 432 486 260 713 1040 1381 128 414 334 402 48 747 747 812 413 571 93 430 73 176 101 10…
Output: 27

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
Scribe and the Longest Trail - MEDIUM Coding Problem | CodeTikki