CT38985

Piper's Longest Trail Ledger

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

At the coral atoll of Pearldeep, Piper records each roads in a worn leather ledger. The guild wants answers, not guesses. 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 <= 1680; the input is a connected tree.

Example 1:

Input: 343 195 197 152 340 44 135 139 190 2 14 2 7 188 201 141 144 20 51 148 189 102 208 193 266 139 268 23 35 97 269 57 114 293 299 21 42 52 179 45 194 18 330 1 334 74 229 18 173 200 264 12 123 121 342 116 136 99 241 165 218 122 147 5 23 34 79 21 67 55 156 18 119 21 117 61 199 242 308 67 154 148 280 107 287 57 88 49 91 116 169 23 48 167 258 133 138 176 339 44 126 49 161 85 186 20 44 105 140 71 111 4 152 31 49 3 235 47 54 2 41 49 329 168 254 24 223 6 32 27 206 9 106 259 307 31 38 4 75 57 198 48 276 53 68 63 103 145 313 2 37 153 183 81 242 99 200 277 318 125 271 169 234 140 212 38 100 55 273 48 142 195 301 10 82 43 46 228 295 148 170 18 89 20 74 266 337 144 262 114 248 185 244 228 230 2 55 41 83 23 93 240 288 14 285 33 233 33 192 39 94 312 338 23 98 53 164 206 221 41 66 128 172 113 316 164 302 131…
Output: 19

Example 2:

Input: 763 33 664 171 385 350 690 358 625 38 63 384 425 198 601 344 759 276 762 293 433 242 429 148 246 100 174 47 217 394 669 53 139 584 658 305 389 3 571 97 279 250 357 57 501 212 329 119 322 271 414 144 155 118 212 70 171 54 416 441 673 17 115 238 466 131 406 24 725 346 538 380 542 27 348 211 396 1 12 92 704 385 678 355 738 158 177 195 450 9 46 189 265 52 268 39 257 151 262 338 688 5 19 53 161 20 27 752 760 119 670 71 376 5 172 16 589 63 222 243 368 8 9 134 692 56 80 9 340 448 659 271 289 9 254 430 756 387 638 92 682 203 308 77 594 110 232 10 469 113 175 115 219 271 489 228 440 431 522 481 586 338 642 203 240 26 209 381 753 226 700 59 253 502 544 95 142 360 737 98 297 91 411 25 549 4 553 364 475 302 504 41 167 12 448 85 229 275 374 69 108 55 105 366 665 301 630 19 40 322 616 707 757 54 508 106…
Output: 23

Example 3:

Input: 238 11 109 38 161 8 163 104 127 2 3 162 204 83 94 77 114 146 158 18 73 97 232 10 43 109 122 42 70 8 42 86 159 18 228 87 133 19 53 80 172 162 170 25 38 54 82 7 185 111 148 9 31 4 62 1 4 46 174 43 224 96 196 69 143 40 150 103 115 1 7 6 10 141 164 19 75 9 63 153 156 114 152 28 32 5 51 119 182 18 34 98 119 41 191 3 202 65 188 64 76 127 157 21 23 236 238 106 116 4 9 82 134 152 219 122 151 65 96 39 107 30 84 6 28 2 15 58 92 115 187 11 145 76 192 28 160 224 229 38 48 11 101 93 208 97 181 16 74 8 17 34 77 20 25 10 12 105 108 28 39 109 153 145 169 86 95 12 178 35 64 41 213 109 147 65 195 19 207 180 189 139 223 17 22 100 166 110 197 3 11 128 137 43 98 1 21 89 176 59 167 4 212 48 50 98 221 17 20 1 6 133 231 1 89 125 225 93 111 8 29 122 198 126 135 38 87 109 183 3 16 9 27 113 201 14 126 111 140 27 60 …
Output: 20

Constraints:

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

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