CT46468

Wynne's Ledger of bridges

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Wynne inherited the customs house at the smokehouses of Brineport along with its curious bookkeeping system for bridges. Time to make sense of it. 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 <= 1231; the input is a connected tree.

Example 1:

Input: 243 4 6 6 86 35 120 3 91 70 121 14 23 5 119 15 136 34 183 16 21 138 157 121 179 67 100 35 226 5 7 2 9 10 79 134 218 17 110 130 131 1 3 4 107 123 177 11 12 56 231 46 65 227 234 157 195 84 173 59 189 1 201 130 233 2 46 1 22 92 98 14 109 5 48 22 26 55 92 26 35 26 148 115 153 20 42 110 243 154 160 28 49 17 168 130 185 44 214 34 54 27 135 118 159 157 194 26 43 3 40 180 220 140 206 28 90 93 165 125 129 44 163 104 170 136 140 55 202 10 38 51 83 9 158 62 156 49 149 143 230 14 28 223 235 21 53 38 47 60 169 7 8 4 14 5 33 76 103 41 75 54 142 68 106 12 27 7 13 23 36 136 181 133 216 4 16 99 164 216 228 39 60 66 108 63 175 26 70 125 147 110 139 95 114 1 11 48 87 135 229 103 127 174 209 2 44 8 10 85 146 159 180 39 51 16 61 16 200 19 198 9 166 82 97 1 18 103 134 80 96 18 137 37 68 203 225 57 113 29 192 11…
Output: 18

Example 2:

Input: 367 187 241 52 169 92 315 168 180 42 299 64 72 154 328 96 195 161 283 231 242 12 160 79 178 54 265 24 75 97 125 98 159 103 137 8 60 17 131 27 189 46 177 227 324 17 21 24 134 93 237 7 325 47 171 14 61 8 39 50 185 86 207 43 59 235 291 5 36 22 89 69 277 94 267 199 219 51 360 22 278 108 117 287 331 37 46 28 30 135 346 242 307 12 269 191 290 192 231 47 151 15 201 33 53 122 261 271 285 285 314 48 310 150 186 2 147 157 240 6 45 324 345 215 220 14 19 53 63 304 311 76 119 154 276 17 20 109 132 67 179 150 168 70 161 239 248 263 352 219 326 12 34 26 271 268 349 108 233 54 351 176 200 5 74 4 124 4 174 218 300 14 188 130 317 86 108 170 249 242 350 45 109 49 223 97 135 1 66 12 90 153 156 24 69 27 215 19 22 17 206 1 82 229 353 11 296 124 275 21 355 57 268 188 339 75 130 27 42 16 85 63 111 57 319 133 167 …
Output: 23

Example 3:

Input: 542 17 25 69 108 191 287 335 438 55 131 104 367 128 335 408 467 67 378 59 137 40 92 349 534 75 104 111 328 167 214 63 177 185 310 271 380 170 266 399 522 80 436 45 61 79 278 58 489 9 44 496 528 148 455 94 208 292 326 23 45 37 197 45 421 28 33 491 520 192 193 87 225 3 65 245 383 39 184 113 210 271 358 10 27 238 307 80 114 19 34 445 518 244 481 42 176 109 234 4 133 17 23 11 94 331 418 318 434 123 168 10 14 54 136 14 77 38 157 75 111 303 505 274 422 244 350 158 243 8 42 334 398 360 428 322 461 10 435 37 217 42 397 258 432 36 40 270 466 120 299 172 396 89 173 8 64 352 477 74 482 67 361 174 343 153 230 30 43 101 164 183 203 243 541 36 70 99 179 40 54 14 59 77 245 447 475 172 253 52 318 1 21 210 292 217 274 232 277 21 81 110 340 3 4 202 374 89 385 46 89 9 296 405 459 50 69 153 165 146 485 132 13…
Output: 26

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
Wynne's Ledger of bridges - MEDIUM Coding Problem | CodeTikki