CT52396

Bhavi's Ledger of canals

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Bhavi the inventory keeper watches over the clockwork quarter of Gearford'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 <= 3286; the input is a connected tree.

Example 1:

Input: 2042 679 978 494 1507 1635 1773 5 7 8 80 601 663 268 507 1 5 177 613 302 409 358 826 20 37 17 63 273 1266 439 877 1556 1702 249 897 1419 1526 136 175 1031 1392 122 177 1303 1576 498 747 397 932 536 935 423 625 968 989 70 1662 1262 1497 451 581 37 219 956 1181 853 1372 177 279 611 1424 256 1891 110 327 773 779 102 615 166 196 11 516 783 1566 907 1599 12 257 443 554 114 167 1714 1840 350 388 699 1508 336 469 62 174 1326 1736 666 1102 58 245 52 236 289 374 37 218 17 143 82 274 51 191 269 387 934 1158 182 189 1 2 48 101 133 1810 223 1179 385 728 933 1082 279 400 1451 1856 675 1376 193 1633 245 1244 274 1704 86 164 59 300 15 403 1165 1371 893 1696 325 1193 87 673 613 1722 1388 1639 567 894 1006 1481 89 1886 295 303 22 596 493 772 2 8 923 1062 25 146 22 893 358 1025 138 1359 512 1797 130 292 798…
Output: 31

Example 2:

Input: 1394 381 533 668 1320 57 396 534 1077 914 1210 774 828 679 1090 18 38 35 61 250 290 54 262 224 739 222 275 286 1188 115 1085 73 1348 886 1254 248 513 417 637 1175 1233 419 728 31 887 446 473 16 96 1168 1298 318 691 291 1001 223 495 204 1288 778 815 3 173 77 545 421 1164 244 308 438 636 111 261 525 1249 89 671 2 12 546 1036 87 501 404 963 30 1390 486 1331 5 120 72 78 130 238 595 1167 587 706 167 1044 758 1106 935 1112 441 514 335 973 777 1304 127 214 744 1202 598 961 226 1393 179 226 25 237 527 653 337 1339 146 285 487 705 223 1377 38 652 207 220 147 400 1217 1323 1001 1363 13 813 18 302 671 921 148 185 97 167 239 484 105 1199 273 1222 374 986 180 296 183 595 972 1104 887 1045 1073 1174 550 594 3 193 220 935 347 964 562 1316 397 1002 184 693 225 503 581 779 159 294 331 1307 895 1040 701 923…
Output: 26

Example 3:

Input: 420 97 195 96 183 34 72 191 339 117 161 131 209 151 304 14 17 183 191 146 213 19 43 403 406 46 65 25 26 16 159 256 399 244 261 10 31 272 282 231 237 50 69 2 14 94 139 42 78 138 141 225 228 49 419 254 415 37 54 162 290 75 273 158 226 9 51 127 165 7 92 117 361 73 150 254 334 74 231 108 157 40 82 4 156 22 131 58 97 10 57 10 197 51 230 43 74 158 166 55 71 42 125 64 313 3 23 260 295 2 27 242 349 120 275 37 164 277 291 371 388 29 85 204 340 132 208 22 59 204 283 133 312 59 288 3 247 23 411 63 167 15 128 91 243 89 263 9 175 96 297 54 236 79 93 65 122 125 126 277 347 20 211 6 9 95 221 153 178 17 66 214 293 162 204 66 321 57 229 50 272 331 394 31 102 92 112 109 142 54 84 186 279 53 337 308 350 4 11 136 232 49 383 82 343 2 88 169 329 2 25 184 362 113 223 116 152 1 3 8 15 1 2 19 135 203 418 11 42 1 7…
Output: 17

Constraints:

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

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