CT39060

The Longest Trail of the Verdant canals

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

The council of the mining town of Ironvein pays by the answer, and Harish intends to earn it. First: the full register of canals. 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 <= 3456; the input is a connected tree.

Example 1:

Input: 367 95 114 192 282 242 349 70 301 200 204 89 193 75 231 51 72 22 68 43 51 70 83 83 92 128 224 138 263 21 257 20 86 4 6 21 34 11 18 31 44 218 279 22 74 287 306 40 95 28 46 5 17 133 176 77 277 157 164 35 66 12 79 66 234 334 350 124 361 128 255 58 73 136 265 104 147 289 303 142 343 69 260 76 145 18 91 60 99 148 232 54 62 7 43 114 241 21 84 40 48 25 116 112 154 59 191 5 171 25 194 105 196 16 125 199 200 13 39 9 52 130 137 100 149 92 98 91 129 11 30 41 65 50 63 115 175 222 352 52 113 10 60 1 280 67 121 19 225 89 122 145 246 10 38 217 355 131 208 103 141 22 89 59 230 20 36 70 331 253 258 28 203 122 251 64 108 236 293 114 309 101 206 5 21 204 340 36 40 131 152 24 87 33 111 58 77 117 199 4 54 157 344 157 239 334 345 13 76 120 298 160 325 3 354 177 339 269 342 168 213 181 316 288 291 36 233 105 201…
Output: 20

Example 2:

Input: 1005 50 278 36 42 102 141 239 829 242 448 80 378 99 844 100 178 3 13 79 89 461 576 277 669 305 956 376 830 63 342 7 216 450 580 337 497 32 708 28 474 110 816 42 58 143 590 231 921 9 167 138 547 50 163 319 335 62 131 274 812 166 518 42 234 134 955 57 150 479 654 325 446 106 374 54 713 194 349 514 638 357 442 279 345 139 190 19 60 180 373 154 246 27 76 491 944 726 915 167 615 176 229 6 103 152 397 82 177 792 852 708 802 363 964 51 220 43 691 233 739 114 693 368 762 209 421 226 351 12 24 459 834 96 642 326 734 21 352 623 726 33 539 87 95 393 775 337 897 55 306 162 261 755 798 289 336 629 677 382 525 27 221 199 404 381 780 241 358 118 156 360 971 825 931 108 411 308 698 688 806 343 564 372 465 39 184 183 536 419 429 138 420 179 277 556 587 96 918 224 328 157 314 4 50 13 15 387 645 153 182 707 …
Output: 26

Example 3:

Input: 2962 153 259 1849 2859 1157 2261 170 1286 329 372 349 529 678 2937 19 246 228 2161 921 2111 611 962 117 406 786 1091 1619 1817 7 8 121 377 227 1211 106 176 645 971 272 293 1429 1525 85 148 643 1333 68 411 478 521 1675 1684 467 490 750 1252 90 532 256 2700 1457 1547 1952 1964 2002 2050 1026 1042 1159 2576 253 382 186 1253 329 2825 16 26 1810 2505 1175 1265 1995 2489 565 2563 796 978 280 309 23 1315 1414 1965 618 2041 328 465 905 2935 564 2134 418 1012 929 1296 80 166 480 619 6 11 1092 1334 419 1800 2017 2044 311 912 218 290 55 1437 23 69 29 54 163 265 158 1312 133 1900 1449 1883 93 96 228 268 314 2389 19 125 710 778 107 188 4 6 634 886 886 1011 637 1235 491 1378 476 2501 332 1928 1088 1237 1502 2714 477 2423 155 2673 1043 2016 311 473 603 1298 205 1511 2179 2770 61 2177 262 1194 128 764 662…
Output: 30

Constraints:

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

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