CT55053

Gardener Counts the tunnels

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

At the opal reefs of Shimmerdeep, Gardener records each tunnels in a worn leather ledger. The guild wants answers, not guesses. 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 <= 3975; the input is a connected tree.

Example 1:

Input: 1362 310 837 192 207 113 199 574 879 224 693 504 1204 751 1009 178 571 754 1104 627 1224 352 470 251 1264 90 133 981 1309 194 333 164 415 157 563 995 1136 4 20 288 750 180 555 127 327 179 841 621 976 953 1119 509 1027 63 270 368 622 137 204 231 295 326 472 114 1338 332 1103 330 361 193 246 420 967 258 1068 617 1239 52 278 261 1283 320 1168 122 431 353 386 13 781 189 316 257 495 37 74 795 1143 26 28 533 960 377 842 817 919 435 454 937 1040 470 582 35 64 704 969 451 1285 33 665 30 60 793 982 167 1139 173 413 793 823 195 846 168 946 40 512 106 519 575 615 24 184 256 279 309 479 150 1006 22 58 716 1098 204 285 197 375 386 577 2 6 855 1105 363 517 1033 1094 696 936 123 1174 162 289 19 593 303 1268 7 1007 292 834 810 954 52 244 5 141 242 959 455 640 66 220 41 337 496 755 128 129 509 536 634 1220…
Output: 26

Example 2:

Input: 584 535 571 157 405 270 347 195 251 21 160 5 65 20 27 422 453 27 329 236 397 197 334 14 20 324 543 217 339 15 305 211 222 82 94 78 367 282 292 262 581 72 253 64 295 26 29 329 451 103 340 229 338 3 6 19 298 101 538 113 198 248 349 108 255 20 37 474 552 1 13 254 522 144 311 242 396 2 10 263 359 137 158 415 488 220 436 449 490 105 315 276 491 182 183 98 219 27 200 11 140 149 256 1 26 23 52 108 119 33 151 331 482 171 236 79 270 311 532 141 252 107 184 258 342 118 242 155 457 10 131 197 265 18 268 55 138 171 404 90 501 187 324 168 302 28 103 249 531 116 153 35 87 231 530 92 132 365 526 3 230 158 499 93 218 278 439 20 79 97 161 183 283 18 21 283 373 240 289 47 50 4 59 6 11 95 486 216 348 63 243 463 481 465 518 29 155 6 102 303 492 31 45 65 326 259 562 297 354 37 74 44 154 135 148 78 81 97 460 27…
Output: 23

Example 3:

Input: 1986 1055 1125 52 1977 479 665 341 979 856 1036 11 605 684 899 1608 1740 276 521 1557 1654 780 1110 56 101 332 844 122 206 574 745 777 1539 164 302 64 1330 7 121 244 446 658 1955 49 52 520 1247 1311 1418 159 764 200 679 983 1237 363 1042 835 1250 901 1287 155 411 412 1280 513 1191 862 1739 118 453 406 1404 1213 1810 833 1854 7 25 768 798 185 1265 253 935 5 68 720 900 245 541 738 1707 76 81 1323 1390 50 177 65 1123 1290 1548 49 825 25 490 708 1311 526 1632 41 1633 170 1014 961 1596 355 1616 236 1076 225 415 953 1056 1495 1752 323 535 15 1179 782 1899 1326 1823 429 867 557 1119 73 674 856 1793 171 364 1177 1560 1244 1807 260 505 1390 1409 450 1976 1541 1890 1367 1372 648 658 459 516 113 443 75 143 1016 1143 782 1458 169 192 306 577 1004 1527 36 1780 955 1650 545 756 1281 1781 63 384 659 1208…
Output: 29

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.