CT40172

Surveyor's corridors Reckoning

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Surveyor found an old ledger of corridors in the granary attic at the granite steps of Stonehallow. The margin notes pose a precise question. 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 <= 3555; the input is a connected tree.

Example 1:

Input: 2012 140 326 126 639 860 1051 1432 1889 72 298 27 337 224 1874 12 1937 846 1105 1247 1546 1157 1667 595 715 320 1195 864 1714 796 812 317 534 699 1298 111 798 793 1645 88 201 1706 1878 1068 1509 136 239 1212 1493 957 1526 21 1625 280 1541 423 753 240 1310 678 761 148 334 131 195 35 250 643 1231 295 345 1231 1279 733 1327 190 748 1033 1635 262 1106 971 1390 226 1430 442 1295 447 1956 91 481 81 374 260 653 405 927 257 996 506 646 94 113 811 1080 627 1202 42 295 184 306 529 603 2 6 222 346 82 240 477 960 342 399 53 58 596 970 847 983 719 1344 593 1420 1129 1888 433 1421 832 898 39 75 487 1127 1213 1596 43 46 577 719 906 1436 13 33 470 1485 285 366 1519 1853 325 1968 136 1930 96 1142 355 575 1045 1652 241 813 188 2003 116 307 432 609 1441 1788 743 1676 718 730 101 107 724 808 161 179 92 143 59…
Output: 27

Example 2:

Input: 1640 254 1369 496 1080 30 1180 326 1357 795 872 148 1083 14 58 177 1042 57 1228 39 89 1073 1589 5 1410 853 1495 343 378 65 84 697 1281 255 359 133 801 36 44 108 710 350 809 531 768 667 803 734 1316 43 48 477 1000 104 1156 830 875 216 1251 266 1093 11 424 81 1170 378 1183 143 659 623 881 108 118 141 224 539 1604 80 407 379 444 215 324 681 825 892 980 417 423 45 389 244 1229 772 1485 1011 1367 68 130 173 385 139 1335 17 31 155 439 852 1167 721 727 1118 1192 161 1271 1253 1365 1194 1215 236 720 376 581 576 1527 8 50 220 677 838 1426 730 1159 255 1035 336 913 206 288 103 137 205 460 217 1153 1139 1309 21 90 443 943 99 164 980 1139 165 267 276 1289 43 47 358 1191 61 796 77 892 884 907 793 1197 72 115 267 590 630 748 108 696 529 1487 771 827 43 180 562 1041 296 1340 600 829 81 591 241 258 33 68 …
Output: 26

Example 3:

Input: 1587 420 432 399 1509 22 446 1296 1346 200 410 526 1538 264 1015 103 214 787 1408 539 818 1231 1259 14 32 202 1256 1118 1166 992 1090 694 1276 242 1569 67 168 35 385 257 333 586 1530 581 1046 586 1279 1102 1356 270 631 888 1377 1091 1267 455 1397 421 1570 310 1044 2 4 374 434 2 3 1076 1101 377 1321 1341 1395 443 920 110 684 61 139 340 493 708 1427 1216 1433 180 352 429 470 664 748 124 672 826 1209 244 559 116 713 27 336 53 616 1203 1539 300 710 198 1189 329 542 1143 1586 575 771 24 902 19 49 291 781 740 957 3 206 916 996 1049 1127 45 1173 316 382 404 752 342 972 368 443 174 217 781 1188 816 1528 122 166 115 1239 469 975 435 480 536 1125 384 479 112 123 5 892 1124 1394 726 817 97 256 21 456 277 393 504 528 105 241 20 452 168 750 28 219 5 95 82 111 230 279 745 918 70 648 288 1294 53 103 3 51…
Output: 25

Constraints:

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

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