CT53102

The Longest Trail of Will-o-Wisp

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Tina inherited the workshop at the fen lights of Will-o-Wisp along with its curious bookkeeping system for routes. Time to make sense of it. 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 <= 1097; the input is a connected tree.

Example 1:

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

Example 2:

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

Example 3:

Input: 1026 558 738 615 801 67 631 805 888 699 791 400 978 103 491 132 370 129 366 3 16 98 752 421 894 512 836 202 669 139 237 196 958 328 879 50 843 599 694 173 451 46 549 523 580 794 914 157 489 550 941 187 887 492 772 229 823 52 358 146 710 998 1009 630 713 184 559 350 528 186 187 659 963 505 632 179 188 86 93 133 407 430 867 74 335 33 83 9 35 748 765 682 1020 144 167 833 844 28 36 416 579 295 883 3 175 18 79 285 292 19 473 214 350 451 819 482 1006 118 424 30 551 306 401 170 854 664 920 336 682 76 604 675 926 251 438 214 285 209 939 153 172 13 251 128 205 927 965 883 954 374 980 13 932 49 461 578 922 569 998 109 133 644 912 213 244 26 54 98 1014 89 798 27 50 252 287 498 923 113 388 835 897 26 28 242 453 72 80 168 702 178 505 206 805 448 607 40 132 301 851 352 764 505 777 137 616 68 114 296 603…
Output: 25

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
The Longest Trail of Will-o-Wisp - MEDIUM Coding Problem | CodeTikki