CT51620

Qadir and the Longest Trail

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

The dispatch writer Qadir has been asked to plan the harvest festival. On the desk lies a long table of routes readings. 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 <= 2147; the input is a connected tree.

Example 1:

Input: 1988 998 1260 1127 1456 613 1477 935 1550 190 1584 500 693 412 1818 64 532 1136 1522 456 551 536 1911 353 435 1195 1687 148 205 650 677 854 1733 904 1930 521 795 505 1446 203 1848 372 647 366 417 944 999 451 1637 847 1120 10 53 488 1521 388 480 540 631 69 1160 493 1203 1121 1291 802 1944 394 1367 352 1691 241 860 291 1838 214 1340 158 306 12 26 1069 1710 107 393 879 1771 13 994 416 460 21 797 58 283 246 264 650 1406 64 144 61 803 525 1067 679 701 459 1700 44 699 932 1269 58 136 796 1244 553 1250 446 800 1430 1837 268 573 1118 1792 341 1928 136 1255 668 1145 209 361 382 562 24 1174 97 1392 531 918 201 558 2 5 363 770 8 152 65 116 773 1308 1039 1983 557 1073 561 586 461 561 136 1561 1626 1663 4 55 184 1207 873 1596 904 1165 54 257 141 628 66 401 454 1097 155 1776 287 858 224 1102 264 1735 54…
Output: 27

Example 2:

Input: 238 122 217 23 145 7 121 118 231 71 164 135 174 126 130 94 178 73 80 17 39 3 33 63 81 88 128 196 225 225 238 46 129 20 75 68 158 187 198 74 184 39 57 90 102 26 50 128 180 2 3 3 6 128 204 20 190 22 89 13 16 1 4 76 221 162 181 47 48 40 176 7 19 83 202 37 116 65 107 12 20 7 13 89 97 90 109 59 226 3 32 139 140 20 37 19 179 9 60 73 79 49 162 168 234 55 146 65 114 14 51 21 73 55 68 17 34 56 135 19 45 34 66 68 83 59 228 106 139 9 161 52 236 57 115 9 29 85 203 128 143 101 117 9 15 42 44 6 7 24 36 3 8 49 111 197 200 51 149 18 113 135 154 8 11 209 232 183 186 62 235 112 156 40 88 35 76 34 169 18 49 15 98 29 47 59 105 67 211 181 197 2 43 5 100 33 160 53 167 46 72 51 86 91 175 13 40 68 222 11 52 30 93 88 144 157 172 90 155 32 223 106 153 52 55 23 220 9 42 14 18 24 84 5 53 6 61 64 108 98 182 29 41 118 …
Output: 19

Example 3:

Input: 240 49 67 83 194 136 170 36 60 95 112 52 53 81 82 121 234 50 229 116 205 158 230 128 176 91 188 73 164 23 202 40 225 46 72 17 106 65 75 11 18 31 110 52 145 103 130 3 71 211 215 94 171 112 201 109 175 37 57 25 178 79 193 33 168 177 199 10 172 1 36 45 65 11 51 10 58 23 32 140 180 58 158 45 73 31 35 7 9 50 146 44 190 28 63 22 41 90 211 53 64 75 86 123 235 43 90 58 125 20 96 93 139 143 144 1 2 73 159 60 128 158 162 13 222 24 207 184 206 36 104 37 179 46 87 196 219 4 25 18 169 125 197 22 77 16 114 37 156 116 165 7 56 1 5 17 33 6 191 66 81 149 200 129 143 51 232 193 218 121 150 18 166 196 209 5 111 126 181 147 203 67 129 65 79 66 120 1 3 173 182 51 153 139 223 6 38 5 16 12 167 123 177 3 12 119 132 20 117 45 80 52 198 27 61 5 11 14 148 3 20 56 186 39 154 42 107 14 24 126 163 11 237 2 8 1 85 38 11…
Output: 18

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
Qadir and the Longest Trail - MEDIUM Coding Problem | CodeTikki