CT49757

Asha's tunnels Longest Trail

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

In the ferry landing of Crosswater, Asha the surveyor keeps careful count of the tunnels. Prepare the expedition manifest, every figure matters. 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 <= 2334; the input is a connected tree.

Example 1:

Input: 233 7 210 80 160 55 65 74 85 115 133 55 223 190 209 8 58 19 92 96 113 69 151 87 129 122 192 72 81 65 75 1 154 47 121 5 7 9 57 120 169 156 166 8 13 17 126 192 208 65 67 85 183 75 98 49 59 38 49 80 139 70 221 60 91 19 232 100 152 38 175 77 118 26 107 11 47 51 111 3 84 10 12 9 74 6 48 121 177 39 116 44 80 20 136 4 104 1 95 1 5 73 161 131 215 3 27 31 38 24 45 184 195 154 212 26 76 27 69 136 226 1 2 106 171 78 108 2 163 57 204 54 156 117 145 3 9 5 41 149 194 35 127 111 120 170 201 80 174 1 33 131 196 91 131 48 66 15 16 81 220 5 18 94 146 1 4 3 30 12 132 5 125 21 71 126 150 1 10 46 56 5 8 24 99 72 143 32 148 37 189 33 207 4 36 127 137 69 97 104 141 17 28 67 218 83 109 119 225 23 140 35 200 38 42 52 55 135 173 34 178 20 87 10 23 15 157 4 17 22 222 59 172 133 213 37 142 34 52 173 184 36 115 8 20 6…
Output: 18

Example 2:

Input: 1553 751 1092 55 826 852 1073 354 1081 714 1381 455 773 334 589 612 907 338 377 65 132 500 767 1026 1156 260 1395 474 884 518 722 681 1417 450 1101 773 1539 5 99 47 66 918 1161 888 972 229 465 27 336 28 425 76 807 470 512 30 70 662 1028 173 482 408 822 116 999 589 812 89 161 209 335 92 584 170 192 212 889 51 127 498 577 316 1367 1298 1377 419 1271 48 173 808 993 1359 1515 600 825 20 146 387 742 542 1136 220 1121 215 679 3 198 437 1079 1067 1355 640 657 860 1283 2 1297 11 533 694 1449 634 1404 7 246 43 847 147 347 957 998 34 361 799 1023 849 1327 26 253 28 396 635 824 181 341 330 430 136 782 909 1487 143 182 5 14 176 487 97 678 277 320 95 806 1334 1474 875 1177 579 714 26 38 18 890 419 690 1197 1242 51 62 404 460 1185 1247 31 1151 1304 1357 1181 1235 1 633 365 796 1107 1211 31 978 773 1476 …
Output: 27

Example 3:

Input: 2235 280 390 307 695 528 1993 964 1874 1071 1436 1126 1565 18 34 325 1161 702 1395 46 304 203 714 52 989 571 814 1222 1578 1264 1336 482 1086 93 1399 551 1536 387 1995 72 698 64 786 45 785 1307 1526 98 1904 1561 2150 361 794 35 269 15 230 15 945 1042 1287 122 345 253 1219 762 940 5 10 563 2153 517 779 221 267 739 1493 1839 2047 708 1367 191 200 1402 1528 140 1949 2016 2021 791 1775 137 1121 1470 1549 223 238 100 2219 53 909 179 213 1141 1887 82 301 226 1156 384 511 1287 1580 563 2157 69 1133 11 1682 1762 1863 2 183 1965 2030 133 219 337 933 629 2229 82 731 125 1077 459 655 461 467 79 475 300 2062 130 601 380 904 258 1513 73 181 630 2102 1356 1585 1017 2171 484 1596 1767 2227 311 2206 611 973 40 760 1722 1808 454 916 650 1148 1493 1626 1142 2174 530 1015 1376 2119 405 719 217 1068 175 1711 …
Output: 30

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
Asha's tunnels Longest Trail - MEDIUM Coding Problem | CodeTikki