CT43004

Surveyor's tunnels Longest Trail

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Legend says the vault under the opal reefs of Shimmerdeep opens only for a perfect tally of tunnels. Surveyor means to find out. 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 <= 1558; the input is a connected tree.

Example 1:

Input: 1
Output: 0

Example 2:

Input: 532 222 278 360 474 68 405 118 256 63 308 73 122 44 55 88 335 116 133 19 20 8 16 323 417 226 320 76 158 256 389 212 468 141 531 35 131 89 102 48 72 7 53 10 146 172 199 287 384 113 355 134 317 353 378 12 30 190 394 457 508 49 446 35 316 128 230 24 218 235 296 15 65 405 430 28 39 75 113 289 404 127 257 2 245 21 26 157 284 233 351 225 341 14 23 64 81 23 282 94 200 87 290 368 477 2 35 118 286 329 478 141 252 16 64 6 44 215 314 89 193 90 299 127 321 15 185 1 42 414 456 8 171 73 336 304 395 142 471 76 104 111 411 23 490 233 313 80 106 2 4 126 461 160 297 23 50 1 2 92 128 34 61 122 160 227 509 178 454 97 523 30 73 206 485 203 443 96 364 391 457 190 371 450 496 358 393 184 340 22 31 51 487 259 473 222 416 270 272 2 212 62 322 48 189 9 17 297 374 44 472 472 494 224 530 373 486 307 484 38 276 29 77 …
Output: 22

Example 3:

Input: 820 672 678 100 154 101 169 23 35 127 267 68 472 209 319 127 251 33 55 156 610 395 679 299 717 206 475 259 716 148 783 205 297 1 36 60 268 131 176 476 580 333 539 147 603 105 136 38 76 6 43 47 56 281 439 125 209 525 560 90 177 8 14 170 367 398 768 234 244 317 573 283 365 106 160 154 811 426 798 66 181 319 356 333 755 16 41 186 415 60 166 151 318 26 211 182 684 106 398 115 455 707 737 63 67 286 709 125 446 569 759 85 213 62 337 252 545 142 711 100 102 252 358 139 594 577 677 36 96 296 702 109 308 208 392 95 744 59 234 334 441 375 686 503 585 181 639 425 480 380 433 117 769 9 23 237 438 85 434 15 284 250 555 80 223 636 738 182 344 251 296 256 725 2 97 106 329 227 656 177 667 20 310 77 78 39 404 196 294 45 135 39 243 123 366 204 340 74 98 350 664 35 466 172 345 79 139 161 263 138 156 339 499 …
Output: 24

Constraints:

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

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