CT46319

Tanya and the Longest Trail

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

It is audit day at the clockwork quarter of Gearford. Tanya spreads the bridges 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 <= 1061; the input is a connected tree.

Example 1:

Input: 855 24 27 164 207 142 195 284 697 214 367 236 758 244 298 567 786 816 849 157 343 409 817 99 189 95 304 208 830 778 851 71 495 6 23 436 836 262 292 90 110 150 355 132 256 404 554 476 699 130 376 57 612 256 800 88 219 402 519 106 128 1 55 517 602 42 43 54 239 589 795 178 209 667 696 31 38 132 169 39 75 459 493 22 53 11 199 213 232 128 422 2 569 8 99 147 659 562 585 431 671 217 731 136 143 1 3 80 187 122 228 129 439 123 134 136 252 6 35 133 460 101 124 418 455 115 446 139 263 59 328 8 129 36 46 484 703 57 92 21 97 240 296 320 729 170 432 26 571 70 78 32 353 121 461 573 642 7 44 68 407 17 87 313 329 548 616 151 264 76 101 243 411 5 50 50 141 219 716 13 56 281 299 73 375 394 479 115 391 466 608 304 459 137 332 671 680 628 658 52 743 400 681 169 504 162 567 71 180 35 62 246 384 336 505 113 177 …
Output: 24

Example 2:

Input: 191 7 64 23 162 29 144 56 86 49 115 7 34 29 71 20 70 88 154 86 95 28 158 54 93 73 164 60 88 32 37 31 94 6 40 41 62 55 68 56 132 87 116 16 23 7 98 147 169 140 145 6 8 94 160 87 114 81 184 15 18 71 83 89 125 61 186 122 175 103 133 26 76 8 30 123 139 3 12 7 15 51 87 18 105 6 79 43 109 22 24 4 63 18 39 7 73 113 159 33 53 1 25 28 36 5 80 47 92 40 75 35 78 29 45 22 135 86 156 29 47 1 51 20 72 164 185 74 104 52 177 75 97 60 121 60 66 23 74 13 29 20 89 107 134 111 178 85 91 6 59 51 82 33 118 1 2 134 150 42 65 9 84 13 96 17 58 93 151 15 46 1 3 32 61 5 52 1 6 6 180 33 90 110 112 34 49 25 27 184 189 26 42 97 99 6 7 50 170 14 69 27 56 44 117 50 165 90 111 6 119 18 26 8 157 51 100 32 50 50 108 35 85 115 146 92 137 161 181 41 101 10 22 92 138 85 130 29 32 4 5 4 48 98 152 134 142 65 182 34 55 13 54 13 35…
Output: 17

Example 3:

Input: 236 15 23 37 200 2 15 26 61 11 19 140 146 107 189 2 13 8 28 40 82 30 125 198 233 18 101 14 50 77 205 94 117 55 198 9 164 6 17 107 123 69 154 3 4 23 62 21 70 93 225 197 206 20 100 81 138 107 155 74 97 161 187 4 104 2 12 26 38 49 90 85 214 13 33 71 222 25 106 12 43 39 80 6 29 89 219 103 181 48 64 199 235 19 34 43 102 30 121 89 111 51 91 55 105 53 112 28 153 28 37 151 204 2 86 31 88 56 118 21 221 56 109 35 85 43 44 31 87 95 149 60 139 136 156 91 170 6 129 4 58 67 177 77 176 27 122 56 65 21 96 211 220 37 47 128 208 106 108 48 92 12 63 13 83 5 11 18 51 4 8 51 94 60 203 92 213 5 231 112 192 29 30 18 27 35 54 25 194 86 162 1 107 45 48 8 9 9 184 74 172 6 110 44 56 11 103 17 24 53 74 1 18 59 152 62 186 21 178 3 45 5 185 3 7 39 116 31 35 1 120 154 175 75 89 21 46 147 210 165 202 76 78 59 77 1 31 31 …
Output: 15

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
Tanya and the Longest Trail - MEDIUM Coding Problem | CodeTikki