Ш
CityFrom, cityTo - arrays
Company - number
Size: a a a
Ш
N
С
(function() {
const input = `1 2
1 3
2 4
3 5
1 5
1`;
const makeNode = number => ({number, nodes: [], checked: false});
const refsByNumber = {};
const getNode = number => refsByNumber[number] || makeNode(number);
const getResult = companyNumber => {
const root = getNode(companyNumber);
root.checked = true;
const stack = root.nodes;
const result = [];
let c;
do {
c = 0;
const batch = [];
for (let i = 0; i < stack.length; i++) {
const node = stack.shift();
if (!node.checked) {
batch.push(node.number);
node.checked = true;
}
node.nodes.forEach(n => {
if (!n.checked) {
stack.push(n);
}
});
c += node.nodes.length;
}
if (batch.length > 0) {
result.push(batch);
}
} while (c > 0);
return result;
}
const lines = input.split("\n");
lines.forEach(line => {
const parts = line.split(" ");
if (parts.length === 2) {
const node1 = getNode(Number(parts[0]));
const node2 = getNode(Number(parts[1]));
node1.nodes.push(node2);
node2.nodes.push(node1);
refsByNumber[node1.number] = node1;
refsByNumber[node2.number] = node2;
} else if (parts.length === 1) {
console.log(refsByNumber);
console.log(getResult(Number(parts[0])));
}
});
}())
С
С
N
Ш
N
꧁岡
Ш
N
Ш
Ш
function solve(citiesCount, fromCities, toCities, companyCity) {
const result = new Set();
result.add(companyCity);
const roads = toRoads(fromCities, toCities);
let neighbours = findCityNeighbours(companyCity, roads);
while (true) {
const oldSize = result.size;
neighbours.sort(numbersComparator).forEach((city) => result.add(city));
if (oldSize === result.size) break;
neighbours = neighbours.map((neighbour) => findCityNeighbours(neighbour, roads)).flat();
}
return [...result].slice(1);
}
function toRoads(fromCities, toCities) {
const roads = [];
for (let i = 0; i < fromCities.length; i++) {
const from = fromCities[i];
const to = toCities[i];
roads.push([from, to]);
}
return roads;
}
function findCityNeighbours(city, roads) {
const neighbours = [];
roads.forEach((road) => {
if (road[0] === city) {
neighbours.push(road[1]);
} else if (road[1] === city) {
neighbours.push(road[0]);
}
});
return neighbours;
}
function numbersComparator(num1, num2) {
return num1 - num2;
}
Ш
function solve(citiesCount, fromCities, toCities, companyCity) {
const result = new Set();
result.add(companyCity);
const roads = toRoads(fromCities, toCities);
let neighbours = findCityNeighbours(companyCity, roads);
while (true) {
const oldSize = result.size;
neighbours.sort(numbersComparator).forEach((city) => result.add(city));
if (oldSize === result.size) break;
neighbours = neighbours.map((neighbour) => findCityNeighbours(neighbour, roads)).flat();
}
return [...result].slice(1);
}
function toRoads(fromCities, toCities) {
const roads = [];
for (let i = 0; i < fromCities.length; i++) {
const from = fromCities[i];
const to = toCities[i];
roads.push([from, to]);
}
return roads;
}
function findCityNeighbours(city, roads) {
const neighbours = [];
roads.forEach((road) => {
if (road[0] === city) {
neighbours.push(road[1]);
} else if (road[1] === city) {
neighbours.push(road[0]);
}
});
return neighbours;
}
function numbersComparator(num1, num2) {
return num1 - num2;
}
solve
N
Ш
N
N