CT45262

The canals Longest Trail Puzzle

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Tanner the accountant watches over the crane marshes of Stillwater's stores of canals. A careful computation stands between order and chaos. 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 <= 3144; the input is a connected tree.

Example 1:

Input: 2143 1115 1614 427 1432 1052 1337 53 159 117 1728 385 841 677 713 235 269 19 1983 709 1051 1455 1832 1187 1430 531 1639 1116 1559 1068 1415 22 1640 249 1901 202 1461 1017 2043 425 1743 53 122 35 171 516 2075 534 591 1697 2079 595 821 35 246 30 2008 483 531 242 659 221 447 166 810 613 620 339 1113 249 1548 727 992 52 1677 160 616 243 550 464 879 526 679 2 3 97 373 1646 2121 201 615 819 827 1514 2030 54 736 212 239 1134 1227 339 2056 354 1135 36 103 493 662 392 1378 120 769 896 1286 406 895 361 584 690 1054 1013 1496 144 680 122 235 430 730 40 834 397 455 1217 1506 122 236 897 900 857 2054 462 1106 54 599 595 1022 18 1265 1553 1610 600 825 1009 1594 820 917 886 1004 778 2065 461 1475 131 1865 53 1350 373 831 188 242 466 651 1082 1746 1080 2135 630 682 418 434 129 135 11 190 108 1697 274 1006…
Output: 25

Example 2:

Input: 555 6 293 85 94 49 275 183 270 166 307 136 385 21 363 77 230 128 548 33 44 155 221 30 122 255 393 144 314 219 453 146 163 35 64 296 321 130 334 94 412 8 9 17 24 23 450 125 212 493 537 22 27 142 186 5 20 32 51 235 455 44 148 11 115 180 220 3 23 341 498 105 300 103 229 82 474 282 436 277 366 35 311 309 451 120 241 5 7 8 183 402 466 219 356 65 529 454 478 225 380 87 231 272 330 68 185 106 305 307 539 241 348 318 414 4 87 103 525 5 29 10 132 63 145 439 516 136 169 79 85 128 533 7 60 107 195 363 390 281 447 20 389 264 337 25 38 201 357 74 216 11 120 236 267 64 179 202 215 71 110 467 492 147 395 49 206 84 136 20 77 231 272 430 461 306 542 10 130 92 112 142 189 135 432 391 454 148 555 2 153 34 170 183 341 158 302 118 131 33 102 51 244 75 96 158 208 12 16 89 149 387 434 26 326 172 313 236 287 280 …
Output: 23

Example 3:

Input: 518 4 11 12 121 76 101 134 218 143 215 160 261 126 334 137 225 20 26 198 237 122 123 40 144 100 392 91 216 5 68 48 291 70 72 14 161 232 460 7 241 114 259 5 7 14 22 21 370 18 113 44 63 339 446 71 436 92 135 15 160 83 306 134 200 131 157 110 258 93 112 152 448 305 409 284 462 42 209 80 108 214 430 80 89 76 439 20 117 13 17 170 182 194 298 209 415 51 292 296 299 471 476 249 281 56 173 164 328 38 53 42 148 223 341 36 324 97 212 92 421 62 271 282 339 62 66 130 391 183 189 5 277 138 170 28 166 358 390 93 252 140 201 375 394 94 156 83 128 149 179 299 325 11 56 99 174 5 31 429 450 275 278 170 402 61 136 43 104 313 355 140 184 80 221 144 467 280 498 314 494 151 153 52 65 4 139 305 307 269 329 95 97 179 250 161 269 8 24 369 398 295 496 157 466 121 363 467 471 137 266 179 482 262 386 26 343 80 151 23…
Output: 21

Constraints:

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

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