CT46373

Captain Counts the tunnels

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Legend says the vault under the rookery of Crowhaven opens only for a perfect tally of tunnels. Captain 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 <= 1058; the input is a connected tree.

Example 1:

Input: 177 72 132 101 127 106 118 113 159 10 121 39 65 29 109 5 104 46 56 50 102 2 52 92 157 3 79 44 108 2 3 75 155 21 81 68 73 31 86 18 55 140 171 37 84 29 40 17 116 37 44 61 151 7 10 23 72 45 150 57 111 13 30 8 144 19 34 90 161 17 67 62 135 70 175 87 145 40 110 139 153 2 36 13 17 23 59 50 85 15 22 132 137 24 46 20 45 17 78 18 20 61 173 5 6 3 37 111 152 12 87 36 41 38 148 77 142 59 156 20 27 41 93 21 126 12 48 1 75 2 7 5 11 5 128 114 158 33 68 89 131 9 168 17 122 25 39 28 77 19 51 13 91 24 99 1 4 35 167 9 53 21 32 10 16 121 130 28 76 78 92 4 101 11 125 40 100 77 146 3 120 30 138 15 28 60 140 11 95 37 113 42 98 3 43 118 162 82 166 91 134 43 61 113 177 11 54 168 169 23 33 36 103 18 119 58 63 141 154 29 71 131 133 22 47 39 170 7 35 3 96 4 31 88 147 79 114 9 123 60 106 18 50 2 5 21 25 55 82 15 60 34…
Output: 17

Example 2:

Input: 628 50 82 307 611 121 333 242 249 149 177 4 7 15 42 208 483 86 305 41 601 46 510 173 301 258 565 130 260 39 392 11 232 47 76 77 189 257 298 62 78 375 380 289 329 46 47 87 411 153 563 35 359 5 590 133 552 80 423 350 477 1 2 76 228 224 338 391 575 288 336 209 218 5 72 395 559 362 391 190 206 7 136 220 407 244 514 110 434 222 626 9 12 9 61 288 352 302 594 6 271 164 408 194 315 6 13 186 292 243 303 125 242 486 570 12 40 100 293 56 236 75 268 126 583 236 426 300 467 191 524 164 456 3 8 17 373 171 178 9 310 45 224 115 541 164 296 15 131 284 397 44 534 288 538 108 507 221 316 199 587 139 297 236 511 570 598 52 420 43 84 33 35 300 367 104 616 109 398 16 259 184 595 87 151 55 92 266 566 83 465 195 489 212 551 169 440 3 4 72 496 21 382 57 114 103 265 4 5 42 517 195 546 129 152 148 416 20 29 94 125 1…
Output: 23

Example 3:

Input: 674 163 233 161 194 210 645 20 269 542 560 307 321 382 454 323 386 33 127 397 629 4 5 5 19 11 32 302 583 109 262 117 161 337 576 14 16 175 207 101 182 292 547 277 409 104 654 37 100 169 300 106 404 372 636 1 4 367 504 179 552 555 570 200 422 32 102 328 520 533 572 143 297 82 99 62 126 165 411 102 276 6 20 411 474 308 565 79 83 413 452 131 596 137 314 16 247 47 59 59 439 187 428 251 368 662 669 151 319 36 146 211 241 123 531 166 361 528 555 460 540 24 310 39 239 465 563 5 667 142 272 146 273 377 434 39 66 317 631 175 189 173 181 116 656 400 475 137 266 44 62 318 620 97 165 420 601 277 626 324 430 149 406 71 115 405 456 133 494 372 421 13 39 427 511 166 188 92 244 47 150 47 256 404 533 34 260 98 227 10 12 214 528 148 218 10 125 2 628 28 542 40 220 559 640 439 649 182 280 225 263 105 432 117 …
Output: 24

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
Captain Counts the tunnels - MEDIUM Coding Problem | CodeTikki