CT46961

Vani's Longest Trail Ledger

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

It is audit day at the fox den of Rustwood. Vani spreads the corridors records across the table and picks up a sharp pencil. 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 <= 3950; the input is a connected tree.

Example 1:

Input: 511 300 440 116 232 179 263 22 362 187 270 201 312 12 64 101 286 3 9 78 251 297 437 293 363 26 213 75 124 88 397 17 18 82 294 6 379 70 495 421 438 249 482 26 273 21 23 134 405 196 215 183 293 96 243 143 233 38 73 34 90 105 255 99 186 10 413 142 347 31 499 5 175 232 412 282 292 171 220 250 488 127 144 2 8 105 361 40 282 11 59 279 510 56 219 93 302 17 22 8 10 87 408 206 382 12 309 33 41 24 252 74 100 89 261 44 75 15 330 334 394 17 72 401 507 61 132 46 450 45 83 21 29 76 291 268 422 69 229 97 498 119 237 156 167 21 114 113 176 53 113 152 486 331 423 36 322 54 406 181 446 448 497 61 239 49 57 19 503 22 51 122 484 35 88 344 364 32 142 3 242 200 271 2 38 94 269 10 264 38 474 16 262 78 195 110 464 130 284 133 164 174 392 16 258 406 430 193 247 228 352 2 86 91 101 416 501 318 456 62 79 420 508 19 …
Output: 22

Example 2:

Input: 2458 714 1953 600 926 497 1087 221 704 1849 2387 41 241 811 1339 279 328 27 1045 698 1631 245 693 1115 1784 178 1829 743 751 813 2265 316 1673 1765 1861 101 136 91 201 205 2161 48 535 1117 1823 775 2020 1124 1344 425 1763 686 1721 1441 2065 85 94 590 1241 896 2325 214 1151 503 1275 98 525 853 887 8 13 209 1377 1540 1704 254 272 85 788 277 651 215 1419 1774 2302 1265 1397 315 761 804 2273 391 759 58 425 463 1204 871 2429 10 32 27 416 20 2395 270 477 282 728 851 947 373 1762 805 2274 360 1249 464 1014 120 924 311 1674 317 319 22 67 161 1808 1877 2084 962 1542 181 237 217 349 454 1016 41 2187 47 469 586 606 1558 1772 494 818 311 318 994 1085 510 2335 760 1399 1053 1091 926 1424 184 675 1385 1700 2 439 790 1952 148 1603 951 1335 1016 2285 776 876 110 795 255 282 1218 1859 1847 2338 218 2101 11…
Output: 26

Example 3:

Input: 190 83 189 54 72 22 138 5 7 41 94 21 74 9 39 57 182 53 92 101 159 63 134 156 161 31 47 29 136 15 147 10 113 93 180 94 186 32 185 3 69 69 105 41 158 34 42 26 129 60 63 168 176 2 14 75 81 12 16 136 169 86 157 16 102 17 31 94 167 56 115 125 135 67 76 70 123 5 33 48 68 3 29 71 116 36 44 27 41 57 60 27 177 25 153 5 45 37 77 27 82 88 111 14 26 1 6 59 73 6 20 2 5 97 178 32 89 19 23 12 21 18 119 80 99 94 139 37 121 15 49 80 88 6 24 136 152 29 148 142 144 124 130 29 55 8 84 136 143 30 50 2 13 21 56 72 95 115 151 5 54 6 64 7 48 142 150 71 80 2 17 10 12 13 53 46 171 143 146 110 164 38 141 57 67 2 3 53 190 69 110 33 37 34 61 19 22 81 166 98 108 22 65 15 172 121 160 68 71 73 149 22 59 3 52 14 46 131 156 24 101 61 118 15 125 6 36 163 184 120 154 108 155 58 62 57 179 7 117 18 25 3 142 3 91 92 133 70 86 4…
Output: 17

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
Vani's Longest Trail Ledger - MEDIUM Coding Problem | CodeTikki