const solution = (n, arr) => {
let answer = 'YES';
const dis = new Array(n + 1).fill('');
const visited = new Array(n + 1).fill(0);
const graph = Array.from(Array(n + 1), () => []);
for (let [a, b] of arr) {
graph[a].push(b);
graph[b].push(a);
}
let queue = [];
visited[1] = 1;
dis[1] = 'red';
queue.push(1);
while (queue.length) {
let cur = queue.shift();
for (let next of graph[cur]) {
if (!visited[next]) {
visited[next] = 1;
queue.push(next);
if (dis[cur] === 'red') {
dis[next] = 'blue';
} else {
dis[next] = 'red';
}
}
}
}
for (let i = 1; i <= n; i++) {
let tmp = dis[i];
if (graph[i].some((el) => dis[el] === tmp)) {
answer = 'NO';
}
}
return answer;
};