CT38547

Nandini and the Separate Camps

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Nandini found an old ledger of canals in the loom house attic at the desert outpost of Sandspire. The margin notes pose a precise question. Task: An undirected graph has n nodes and m edges. Print the number of connected components. Input format: First line: n and m. Next m lines: u v. Output format: Print the component count. Constraints: 1 <= n <= 2647; 0 <= m <= 5294; 1 <= u,v <= n.

Example 1:

Input: 1309 2752 850 954 213 962 295 1245 372 1265 610 1002 68 107 160 1081 203 997 809 1287 79 1032 747 1221 235 409 317 443 497 1205 234 420 364 589 56 1216 348 1091 541 1121 141 364 161 182 90 1270 1060 1235 376 537 151 721 708 994 133 372 474 1017 943 1133 452 643 935 996 32 170 1216 1304 627 1129 37 1096 830 965 664 1223 258 1299 72 443 249 421 167 169 7 609 48 1213 313 326 19 397 708 1292 524 1224 10 467 201 726 130 1249 587 1284 714 1251 382 1193 1057 1139 571 648 138 332 168 277 265 406 73 923 508 1217 429 843 251 855 160 875 356 1042 730 1091 405 660 145 1174 597 1162 356 1065 56 1000 22 353 643 851 632 967 589 943 293 988 672 866 567 657 128 1070 45 527 545 753 389 1219 815 1090 594 761 1025 1123 74 1183 896 1079 1034 1232 273 799 188 681 267 607 653 967 719 927 159 533 1198 1295 729 12…
Output: 20

Example 2:

Input: 1714 597 1 220 358 831 542 819 930 1321 68 448 11 1320 196 687 1053 1068 1036 1506 1027 1613 359 396 890 1303 664 1634 353 1383 93 1132 963 997 260 1294 499 1166 706 721 1630 1672 768 1504 715 976 585 1435 1 856 180 1033 1239 1599 205 767 1448 1693 443 1449 513 1534 762 1378 902 1292 624 933 444 1275 1276 1533 757 1123 1037 1160 556 1711 532 878 1353 1356 1251 1336 12 1093 118 681 725 1473 176 1252 484 843 400 949 181 728 386 1306 175 1408 415 1450 159 1062 1017 1537 177 229 350 741 879 1007 559 1614 243 1298 1162 1302 439 1204 215 249 266 571 717 909 346 560 1006 1109 500 966 951 1512 1278 1318 1181 1342 174 1667 17 194 377 1626 3 116 350 1510 176 1451 367 915 792 1253 159 646 67 164 847 1178 10 1570 554 781 305 1509 102 1376 1381 1575 1477 1700 500 985 351 861 322 1598 382 399 848 1180 2…
Output: 1117

Example 3:

Input: 405 922 384 396 198 385 272 295 61 326 5 239 181 198 119 124 93 338 139 260 31 367 190 337 219 371 34 43 242 329 7 400 148 249 199 394 194 372 41 50 187 399 236 337 36 226 155 156 134 181 11 376 268 371 1 225 68 217 61 287 96 178 30 226 103 319 41 202 26 60 57 341 175 365 162 219 118 232 322 377 174 259 186 232 221 351 285 403 120 273 39 85 81 127 103 284 309 312 64 162 24 56 77 228 31 111 194 342 40 333 251 305 306 354 85 204 120 330 314 402 34 283 276 363 106 356 256 387 298 343 12 81 21 170 79 336 44 150 127 369 56 206 313 329 171 212 220 332 258 268 152 164 147 221 234 252 100 177 42 285 285 299 207 272 114 196 142 368 75 267 33 68 330 377 40 370 197 343 49 255 163 282 79 373 21 319 69 358 110 120 346 357 105 251 277 325 114 392 95 171 195 217 258 301 43 253 258 361 199 350 140 200 80 …
Output: 4

Constraints:

1 <= n <= 2647; 0 <= m <= 5294; 1 <= u,v <= n.

Loading...
Test Cases:No test cases
No test cases available.
Nandini and the Separate Camps - MEDIUM Coding Problem | CodeTikki