CT39489

Cora and the Longest Trail

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Deep in the temple city of Sunspire, Cora studies rows of canals entries. One clean number will settle the guild's oldest argument. 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 <= 2962; the input is a connected tree.

Example 1:

Input: 1861 464 497 153 1805 129 163 537 605 724 973 1357 1406 148 312 359 895 71 85 675 880 123 1042 1103 1764 452 713 48 1484 186 629 765 1826 35 116 1500 1841 575 1082 621 1010 142 238 170 984 719 1151 227 270 675 1260 3 21 332 1352 5 27 204 1068 577 999 637 1525 305 1730 1281 1393 312 1464 21 42 4 19 99 1700 1493 1635 293 564 734 1823 906 1122 1 2 1500 1584 205 654 251 1205 376 1738 729 829 65 193 121 725 1212 1523 678 1192 124 179 414 771 829 1668 707 1638 432 1206 9 12 148 1244 199 298 548 1669 149 425 171 353 354 1800 317 405 1130 1673 605 912 322 753 608 1367 14 334 319 598 207 212 412 534 297 1198 325 1041 127 229 41 1171 244 1721 701 802 155 378 671 1246 650 1533 510 1218 1425 1821 173 390 356 1201 27 1812 243 469 150 156 1240 1339 2 20 606 1568 30 494 1460 1587 513 1420 122 614 50 64 4…
Output: 28

Example 2:

Input: 30 4 10 1 14 5 20 7 8 7 15 2 4 4 9 3 5 5 17 13 26 21 29 1 2 8 11 7 13 24 27 5 6 2 3 11 24 15 23 15 25 6 16 17 28 13 22 3 7 13 21 5 18 12 30 5 12 18 19
Output: 8

Example 3:

Input: 1291 486 777 355 973 124 1246 1003 1204 8 181 359 488 59 107 105 454 77 643 811 1077 365 1147 443 1258 32 367 82 119 445 595 862 1045 609 1082 287 443 400 1034 46 131 283 316 29 116 640 651 620 665 530 789 488 1278 439 1061 273 333 325 871 34 528 799 903 160 381 63 784 328 1288 1177 1291 190 1021 226 246 302 524 13 60 517 1032 40 508 393 706 13 18 345 1086 319 355 510 540 40 1125 208 245 487 675 171 981 87 92 351 492 14 505 73 259 246 731 197 260 178 186 184 203 161 379 73 315 434 921 7 50 1033 1094 398 565 213 363 365 901 258 760 617 1109 916 1248 82 158 82 301 121 343 280 855 350 964 527 537 151 275 450 960 51 415 7 20 758 1211 619 625 346 435 797 906 639 961 1220 1269 56 175 32 81 129 1135 177 188 282 1137 68 923 143 161 482 568 95 1091 149 194 526 1283 950 1237 138 631 245 1255 23 25 3…
Output: 27

Constraints:

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

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