CT51445

Bright canals Longest Trail

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Caravans arrive at the heron rookery of Longlegs with crates of canals, and Manav the dispatch writer must account for every last one. 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 <= 3081; the input is a connected tree.

Example 1:

Input: 142 1 2 96 142 28 46 48 80 104 120 54 98 3 5 60 99 60 117 74 118 40 115 5 32 23 43 2 114 8 13 37 81 17 111 72 100 96 97 22 83 33 69 18 22 42 90 5 94 16 65 54 105 2 127 40 57 2 9 44 106 114 124 51 141 75 109 39 130 2 59 40 110 21 95 4 74 16 30 20 62 18 24 22 67 7 12 39 72 7 29 9 15 5 16 28 51 100 108 15 18 5 123 6 11 1 138 37 40 3 119 65 103 14 28 20 86 46 47 53 137 1 19 70 85 31 134 103 104 45 125 3 4 35 68 27 78 15 131 15 71 124 133 19 25 58 122 13 45 48 55 10 61 47 91 13 33 1 3 18 41 21 88 31 58 2 14 6 26 61 66 113 135 87 101 20 84 37 49 51 116 27 37 46 89 15 36 84 92 9 10 2 17 94 102 26 53 32 38 89 96 65 121 15 20 2 113 65 112 14 21 42 93 4 7 65 70 15 136 57 129 23 42 68 82 1 6 19 56 74 107 114 132 38 50 53 87 1 54 32 63 7 31 66 75 8 44 21 52 4 8 103 139 22 128 60 76 34 48 48 60 39 126 …
Output: 14

Example 2:

Input: 328 34 203 96 142 79 272 209 236 41 257 4 6 5 28 15 209 46 233 11 193 29 87 104 167 44 241 13 82 79 85 226 279 12 15 137 242 210 235 14 19 60 77 30 316 34 43 15 36 56 65 25 40 107 306 31 44 123 222 153 213 96 107 254 284 1 4 80 122 30 38 146 268 3 22 24 64 144 244 3 101 130 184 124 327 127 151 24 103 65 110 113 121 59 186 5 206 70 248 83 119 2 136 80 285 76 78 111 124 18 42 48 91 58 280 31 113 31 292 159 297 224 293 42 86 38 200 72 80 92 195 55 123 81 140 4 96 89 234 40 145 78 271 6 7 50 146 4 16 84 139 232 311 25 230 123 245 83 301 131 238 70 73 108 166 83 100 202 249 56 227 8 31 226 287 23 26 10 41 53 232 173 180 6 189 2 20 146 326 246 281 173 210 108 198 68 83 27 52 140 296 111 128 153 262 39 48 19 134 248 304 121 243 75 126 11 267 24 156 318 328 28 299 196 240 103 273 233 282 92 263 79…
Output: 17

Example 3:

Input: 1450 616 848 112 254 31 47 790 1429 227 248 774 869 916 1253 280 516 253 384 282 388 71 93 34 309 87 162 294 632 69 232 360 1042 253 529 66 127 451 472 804 949 61 888 209 759 52 236 541 745 453 1070 162 1372 241 1149 193 386 987 1274 743 1200 222 554 256 568 24 428 713 870 125 359 179 604 50 91 1013 1204 612 1058 110 400 712 1303 404 1319 544 750 545 706 1146 1324 947 1357 796 815 252 1001 454 483 444 546 1115 1275 312 421 394 1247 303 356 1000 1027 458 809 960 1170 258 977 597 613 44 1243 11 1318 776 1240 70 159 549 1312 267 285 182 321 535 845 150 839 853 1067 206 535 120 803 335 996 318 1398 140 361 139 967 96 533 100 1285 575 681 1345 1380 44 193 136 371 405 481 1046 1119 251 1378 103 784 32 64 222 303 90 189 465 824 986 1281 782 920 368 610 312 731 860 1294 745 755 330 1425 218 705 53…
Output: 25

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
Bright canals Longest Trail - MEDIUM Coding Problem | CodeTikki