CT53821

Sailor Counts the canals

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Sailor the reckoner watches over the obsidian flows of Blackglass'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 <= 1620; the input is a connected tree.

Example 1:

Input: 85 12 21 1 2 22 30 3 7 5 34 30 41 43 49 36 43 38 51 3 25 7 42 32 63 1 4 31 71 38 61 31 32 13 24 23 44 5 10 19 23 18 58 4 20 26 50 45 53 1 9 21 27 46 81 41 83 26 57 9 14 11 56 48 72 58 76 30 64 17 52 29 38 60 68 1 8 38 47 8 11 67 73 80 82 15 31 42 65 33 84 17 54 4 15 39 48 13 18 15 35 8 36 48 85 22 74 40 45 3 80 3 26 3 5 55 66 7 12 14 22 31 39 10 46 75 78 2 19 1 3 8 29 1 69 16 70 19 28 11 59 68 79 48 60 57 67 59 75 29 40 13 16 24 55 33 62 5 33 16 37 3 6 44 77 11 13 9 17
Output: 14

Example 2:

Input: 132 75 90 37 125 42 104 6 35 20 100 6 13 9 31 36 74 66 117 47 101 1 2 121 130 1 7 10 19 11 128 6 80 63 72 59 98 18 87 51 123 7 28 14 23 86 127 61 69 8 32 47 76 102 106 77 81 5 6 10 18 50 89 48 93 6 11 31 63 37 51 52 79 53 65 27 67 31 53 8 40 28 95 21 109 24 61 30 97 4 14 9 113 57 73 99 121 3 64 39 114 1 99 5 8 4 70 28 86 41 129 46 88 2 54 21 112 31 59 62 66 57 85 1 5 15 34 29 92 33 52 4 10 31 50 10 38 30 71 4 12 18 25 74 75 19 30 40 43 39 126 106 120 20 48 7 21 12 56 18 36 2 4 81 131 31 116 39 49 8 42 106 122 15 119 21 46 18 44 11 27 1 3 21 29 10 33 18 37 85 103 110 132 55 94 105 107 1 17 37 45 19 39 65 124 86 110 68 115 22 78 15 20 13 15 26 91 5 41 13 55 5 9 38 96 4 22 45 83 35 84 5 77 56 62 11 24 21 47 16 108 23 57 56 111 4 26 7 58 26 60 83 118 2 16 94 105 61 102 62 68 60 82
Output: 16

Example 3:

Input: 391 198 279 328 375 26 169 23 151 209 364 32 196 80 94 94 99 132 142 273 326 3 325 62 127 31 56 305 352 152 230 270 328 217 301 258 262 1 10 28 174 142 241 105 356 194 336 34 102 94 280 53 60 248 251 76 100 189 204 89 354 21 23 59 62 121 358 204 368 31 237 66 88 116 270 99 333 21 327 2 9 167 240 68 112 11 24 104 233 14 49 188 259 146 171 84 136 55 293 99 203 293 303 90 380 12 57 31 106 118 236 17 114 92 216 38 40 230 282 188 332 173 184 125 226 83 104 9 261 30 193 136 198 5 74 13 129 5 55 46 315 10 115 15 22 59 245 159 268 165 242 289 391 40 379 20 117 98 389 55 91 54 71 21 164 20 154 36 188 17 206 32 36 296 376 64 313 24 31 70 82 333 350 16 292 54 111 244 309 12 13 128 168 1 41 266 286 36 103 133 269 208 307 283 387 40 52 5 30 70 176 72 135 172 222 73 133 291 345 35 213 283 377 144 187 34…
Output: 23

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
Sailor Counts the canals - MEDIUM Coding Problem | CodeTikki