CT55264

The Longest Trail of Redrock

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

At the canyon post of Redrock, Lothar records each bridges in a worn leather ledger. The guild wants answers, not guesses. 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 <= 1691; the input is a connected tree.

Example 1:

Input: 26 4 13 13 26 7 18 2 23 19 24 16 20 1 22 2 5 9 11 4 9 5 10 2 3 8 14 6 19 1 17 14 21 5 7 6 8 8 16 1 6 1 2 4 15 1 12 18 25 1 4
Output: 9

Example 2:

Input: 683 113 462 166 388 285 570 246 311 188 291 32 134 2 11 81 163 64 496 390 436 50 211 9 332 89 210 73 143 186 466 7 35 147 641 150 573 2 85 8 91 199 656 100 408 455 477 5 19 327 529 590 661 226 396 321 469 331 652 145 615 223 642 97 350 96 177 101 137 145 325 44 292 51 105 125 225 197 367 43 73 283 559 518 606 190 376 4 173 415 540 98 494 224 518 368 474 142 233 328 450 132 675 385 536 213 354 64 83 81 296 53 426 92 125 124 218 467 682 237 397 311 603 22 32 95 171 144 238 546 584 8 10 54 108 413 609 8 22 121 349 32 44 296 580 18 25 375 679 448 556 370 643 9 47 18 31 396 636 1 200 372 433 172 569 82 245 30 651 88 334 402 415 160 669 68 110 83 257 366 535 11 61 46 51 171 522 88 202 69 244 101 122 49 674 72 312 424 620 143 250 8 263 373 628 162 516 75 92 239 315 372 501 61 205 453 552 166 394 …
Output: 26

Example 3:

Input: 486 306 334 3 29 193 362 275 397 5 19 254 314 422 440 19 22 104 122 98 111 26 240 79 163 148 258 20 23 13 17 24 206 52 64 48 241 38 127 46 56 373 385 34 309 46 50 73 145 304 344 38 95 101 162 46 105 12 60 244 300 381 411 24 201 134 230 82 202 4 38 91 223 15 409 184 449 85 252 18 231 61 115 382 443 80 169 11 415 119 214 3 160 51 62 91 434 66 146 426 466 21 85 315 442 34 49 106 398 312 391 155 270 31 67 1 9 69 155 51 197 219 328 14 74 47 187 215 216 187 421 38 125 69 299 63 165 75 132 367 444 220 237 4 5 51 305 49 167 274 430 272 352 221 277 342 374 310 429 175 205 158 424 35 390 11 96 127 396 33 61 134 278 37 53 10 238 27 347 41 144 134 193 18 68 67 88 76 84 86 364 39 140 36 228 80 188 68 138 31 183 136 468 257 361 96 109 23 123 150 170 349 474 1 43 108 227 59 104 231 459 49 224 275 310 357…
Output: 21

Constraints:

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

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