-
-
Save shugen002/6c4c788a7ac1166c880d351deb61d7f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cities = 0; | |
var roads = 0; | |
var line = readline().split(" ").map((e) => { | |
return parseInt(e) | |
}); | |
cities = line[0]; | |
roads = line[1]; | |
while (cities > 0) { | |
if (roads > 0) { | |
var group = []; | |
for (var i = 0; i < cities; i++) { | |
group.push([i + 1]) | |
} | |
for (var i = 0; i < roads; i++) { | |
road = readline().split(" ").map((e) => { | |
return parseInt(e) | |
}); | |
var groupa, groupb; | |
for (var j = 0; j < group.length; j++) { | |
if (group[j].indexOf(road[0]) > -1) { | |
groupa = j; | |
break; | |
} | |
} | |
for (var j = 0; j < group.length; j++) { | |
if (group[j].indexOf(road[1]) > -1) { | |
groupb = j; | |
break; | |
} | |
} | |
if (groupa != groupb) { | |
group[groupa] = group[groupa].concat(group[groupb]); | |
group.splice(groupb, 1); | |
} | |
} | |
print(group.length - 1); | |
} else { | |
print(cities - 1); | |
} | |
line = readline().split(" ").map((e) => { | |
return parseInt(e) | |
}); | |
cities = line[0]; | |
roads = line[1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment