CT45266

The Longest Trail Beneath Fungaldeep

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

The council of the mushroom groves of Fungaldeep pays by the answer, and Keeper intends to earn it. First: the full register of routes. 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 <= 3497; the input is a connected tree.

Example 1:

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

Example 2:

Input: 1016 505 659 107 398 4 30 552 844 180 201 249 577 388 977 42 379 5 11 497 712 115 416 41 1007 211 965 352 443 788 833 22 84 318 641 66 72 293 538 29 62 548 863 300 653 13 90 102 521 206 590 1 39 564 938 85 337 476 496 36 61 229 392 245 929 304 397 85 386 545 796 420 448 149 164 13 228 104 156 18 207 318 991 334 559 100 580 476 703 159 210 240 330 104 700 147 909 495 602 13 24 629 677 58 273 863 993 293 323 38 237 297 340 59 79 430 764 43 442 729 840 437 721 277 334 94 821 491 661 260 601 31 194 488 928 243 259 61 408 5 14 588 937 280 283 449 729 11 13 272 299 280 571 118 394 491 876 451 822 607 779 537 724 122 433 16 36 264 301 494 871 554 596 438 672 41 374 651 914 39 93 133 297 262 668 844 999 87 115 220 240 272 748 51 343 271 472 34 361 164 327 190 243 128 139 34 220 159 208 38 59 20 23…
Output: 28

Example 3:

Input: 211 55 56 19 25 150 196 46 111 28 64 3 27 81 141 89 160 7 8 60 139 30 74 1 2 107 203 34 48 62 162 29 150 69 81 45 67 17 143 185 189 39 118 3 26 12 15 39 57 2 7 21 43 42 135 42 69 15 20 14 123 73 112 23 78 9 35 5 6 28 71 57 195 48 177 27 32 24 44 5 94 92 142 81 101 74 114 18 185 100 133 35 47 8 23 3 19 132 200 46 60 16 62 85 167 22 28 38 77 72 91 181 187 97 161 66 175 24 39 5 21 55 61 84 199 28 159 79 137 119 191 4 12 148 152 9 84 61 154 6 13 167 198 39 72 54 105 128 171 15 53 3 16 70 115 8 9 67 202 171 184 118 121 108 130 19 169 43 104 3 41 3 34 16 42 19 52 80 119 141 197 152 153 91 128 66 100 33 97 9 30 54 176 181 209 5 172 106 165 46 120 4 86 7 22 79 80 7 95 9 70 11 87 21 58 63 73 2 110 34 36 130 211 62 146 2 3 55 201 15 46 87 102 31 138 10 103 64 148 50 210 2 10 75 134 180 186 55 59 2 6…
Output: 20

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
The Longest Trail Beneath Fungaldeep - MEDIUM Coding Problem | CodeTikki