CT39443

The Opal canals of Xavi

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Xavi the surveyor watches over the marble forum of Argentium'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 <= 2954; the input is a connected tree.

Example 1:

Input: 188 54 151 5 13 3 4 1 30 18 23 40 61 159 178 61 110 32 33 130 142 3 87 22 55 106 168 36 41 124 131 3 119 77 166 105 109 113 175 5 106 92 116 30 174 111 117 15 17 62 98 127 129 128 176 142 180 24 51 119 143 31 100 25 153 6 25 129 165 2 46 37 88 6 7 144 157 16 42 77 182 43 48 21 40 44 89 20 31 52 69 28 43 43 158 129 152 23 141 50 132 71 113 7 29 91 173 46 93 10 85 29 76 57 154 29 39 22 187 6 12 72 138 102 148 3 45 114 130 15 91 48 67 10 121 122 140 123 156 12 14 47 177 11 27 131 160 51 79 95 133 77 188 1 10 50 186 125 127 61 83 55 125 42 52 32 84 45 66 1 37 3 36 53 60 51 65 51 107 55 86 7 21 2 6 75 77 43 111 16 34 114 172 1 2 54 80 66 68 31 146 64 90 81 184 132 155 5 135 2 15 24 150 66 124 21 96 27 54 10 179 64 115 95 120 22 28 69 102 81 105 47 70 10 161 36 62 28 81 45 71 81 92 34 183 20 59 …
Output: 19

Example 2:

Input: 288 64 287 148 175 7 49 174 246 55 102 107 244 26 31 179 226 147 202 103 248 20 48 64 72 84 153 53 61 9 41 239 278 77 132 9 67 25 178 16 74 199 266 1 3 69 173 3 4 56 155 8 43 94 124 89 254 56 236 39 63 71 223 69 273 165 239 70 79 4 46 226 235 65 118 12 38 157 160 18 92 50 126 18 229 127 191 17 117 200 211 126 139 44 188 16 66 4 5 63 78 22 40 21 91 13 259 38 77 3 6 114 148 126 196 79 150 3 10 29 238 16 20 21 208 2 76 33 65 10 59 34 268 63 209 96 106 1 26 56 81 251 277 139 245 121 247 49 149 79 84 1 22 40 137 70 154 99 158 55 242 173 274 142 270 45 50 122 159 143 170 215 271 20 24 81 251 39 138 25 44 190 198 83 125 132 134 134 205 45 83 5 11 145 183 7 15 155 194 74 225 178 269 6 86 20 121 7 9 13 19 107 157 157 171 121 147 27 161 22 162 29 47 37 233 10 73 6 142 46 53 144 258 51 206 29 37 78 1…
Output: 17

Example 3:

Input: 2468 451 835 959 1630 966 2140 56 366 463 895 924 1622 269 576 911 1104 41 147 715 2215 3 49 942 1907 14 531 927 1575 1154 1659 1368 1881 890 1658 144 582 401 1441 144 172 341 1932 87 754 544 621 52 2043 204 694 826 1958 158 283 342 498 26 90 1652 1740 549 1025 759 1618 1080 1564 50 546 445 2129 621 882 1389 2396 953 995 400 559 1076 1472 812 1639 154 1359 33 101 604 646 506 924 476 637 291 483 86 606 861 902 761 991 530 2250 719 2022 186 881 230 2351 54 2311 1312 1391 136 159 1196 1298 410 1476 1665 2176 1515 1879 1139 1374 347 678 546 1266 83 251 1005 1860 190 1625 30 364 1017 1110 1746 2347 479 537 676 1089 379 1550 1821 2032 79 908 588 932 32 178 1052 1186 585 1023 233 1611 309 313 189 229 1399 1943 465 681 230 2333 153 354 741 1816 114 1037 742 1903 445 2131 459 1281 390 848 35 1123 7…
Output: 31

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
The Opal canals of Xavi - MEDIUM Coding Problem | CodeTikki